Skip to content

Commit 1c9ea49

Browse files
committed
added shape detection tutorial
1 parent 20e8753 commit 1c9ea49

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2929
- [How to Make an Image Classifier in Python using Keras](https://www.thepythoncode.com/article/image-classification-keras-python). ([code](machine-learning/image-classifier))
3030
- [How to Use Transfer Learning for Image Classification using Keras in Python](https://www.thepythoncode.com/article/use-transfer-learning-for-image-flower-classification-keras-python). ([code](machine-learning/image-classifier-using-transfer-learning))
3131
- [How to Perform Edge Detection in Python using OpenCV](https://www.thepythoncode.com/article/canny-edge-detection-opencv-python). ([code](machine-learning/edge-detection))
32+
- [How to Detect Shapes in Images in Python](https://www.thepythoncode.com/article/detect-shapes-hough-transform-opencv-python). ([code](machine-learning/shape-detection))
3233
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
3334
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
3435

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [How to Detect Shapes in Images in Python](https://www.thepythoncode.com/article/detect-shapes-hough-transform-opencv-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`.
4+
- To detect lines of the image `monitor.jpg`:
5+
```
6+
python shape_detector.py monitor.jpg
7+
```
8+
- To detect lines of live camera:
9+
```
10+
python live_shape_detector.py
11+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import cv2
4+
5+
cap = cv2.VideoCapture(0)
6+
7+
while True:
8+
_, image = cap.read()
9+
# convert to grayscale
10+
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
11+
# perform edge detection
12+
edges = cv2.Canny(grayscale, 30, 100)
13+
# detect lines in the image using hough lines technique
14+
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 60, np.array([]), 50, 5)
15+
# iterate over the output lines and draw them
16+
for line in lines:
17+
for x1, y1, x2, y2 in line:
18+
cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 3)
19+
cv2.line(edges, (x1, y1), (x2, y2), (255, 0, 0), 3)
20+
# show images
21+
cv2.imshow("image", image)
22+
cv2.imshow("edges", edges)
23+
if cv2.waitKey(1) == ord("q"):
24+
break
25+
26+
cap.release()
27+
cv2.destroyAllWindows()
52.5 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
opencv-python
2+
matplotlib
3+
numpy
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import cv2
4+
import sys
5+
6+
# read the image
7+
image = cv2.imread(sys.argv[1])
8+
9+
# convert to grayscale
10+
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
11+
12+
# perform edge detection
13+
edges = cv2.Canny(grayscale, 30, 100)
14+
15+
# detect lines in the image using hough lines technique
16+
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 60, np.array([]), 50, 5)
17+
# iterate over the output lines and draw them
18+
for line in lines:
19+
for x1, y1, x2, y2 in line:
20+
cv2.line(image, (x1, y1), (x2, y2), color=(20, 220, 20), thickness=3)
21+
22+
# show the image
23+
plt.imshow(image)
24+
plt.show()

0 commit comments

Comments
 (0)