Skip to content

Commit 8423202

Browse files
committed
Created Shape Recognition
Created a simple program that detects rectangles and squares, the program also outlines the shapes.
1 parent afe4a36 commit 8423202

File tree

5 files changed

+72
-48
lines changed

5 files changed

+72
-48
lines changed

Face_Recognition/README.md

-48
This file was deleted.

Shape_Recognition/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# Shape Recognition using OpenCV
3+
4+
5+
## 🛠️ Description
6+
This is a shape recognition program that detects and outlines the rectangle and square shapes.
7+
8+
## ⚙️ Languages or Frameworks Used
9+
<!--Remove the below lines and add yours -->
10+
Open Command Prompt and use the following command to install the required modules:
11+
12+
```sh
13+
pip install opencv-python
14+
```
15+
16+
17+
## 🌟 How to run
18+
Place and rename your image as 'shape.jpg' within the Shape_Recognition folder. After doing that, you can
19+
just open a terminal in the folder where your script is located and run the following command:
20+
21+
22+
Feel free to remove the pre-existing shape.jpg to your wishes and replace it with yours :)
23+
```sh
24+
python main.py
25+
```
26+
27+
## 📺 Demo
28+
29+
![demoimage](demo.png)
30+
31+
## 🤖 Author
32+
[Osmiuth](https://github.com/Osmiuth)
33+
34+
## References
35+
36+
https://opencv24-python-tutorials.readthedocs.io/_/downloads/en/stable/pdf/
37+
38+
## Acknowledgement
39+
40+
Special thanks to [AnishLohiya](https://github.com/AnishLohiya) for his work on Face Recognition as inspiration to make this simple program.

Shape_Recognition/demo.png

12.6 KB
Loading

Shape_Recognition/main.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import cv2
2+
3+
IMAGE = cv2.imread('shape.jpg');
4+
GREYSCALE = cv2.cvtColor(IMAGE, cv2.COLOR_BGR2GRAY)
5+
h, w, channels = IMAGE.shape
6+
7+
PARAM, PARAM2 = cv2.threshold(GREYSCALE, 50, 255, 0)
8+
9+
CONTOURS, _ = cv2.findContours(PARAM2, 1, 1)
10+
11+
RECTANGLES = 0;
12+
SQUARES = 0;
13+
14+
for cnt in CONTOURS:
15+
x1,y1 = cnt[0][0]
16+
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
17+
if len(approx) == 4:
18+
x, y, w, h = cv2.boundingRect(cnt)
19+
ratio = float(w)/h
20+
if ratio >= 0.9 and ratio <= 1.1:
21+
IMAGE = cv2.drawContours(IMAGE, [cnt], -1, (0,255,0), 3)
22+
SQUARES = SQUARES + 1
23+
else:
24+
IMAGE = cv2.drawContours(IMAGE, [cnt], -1, (0,255,0), 3)
25+
RECTANGLES = RECTANGLES + 1
26+
27+
cv2.putText(IMAGE, 'Number of SQUARES: ' + str(SQUARES), (h+500, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
28+
cv2.putText(IMAGE, 'Number of RECTANGLES: ' + str(RECTANGLES), (h+500, 200), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
29+
30+
cv2.imshow("Shapes", IMAGE)
31+
cv2.waitKey(0)
32+
cv2.destroyAllWindows()

Shape_Recognition/shape.jpg

17 KB
Loading

0 commit comments

Comments
 (0)