-
Notifications
You must be signed in to change notification settings - Fork 0
/
YellowShapeDetector.py
40 lines (32 loc) · 1.12 KB
/
YellowShapeDetector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cv2
class ShapeDetectorYellow:
def __init__(self):
pass
def detect(self, c):
# initialize the shape name and approximate the contour
shape = "unidentified"
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
# if the shape is a triangle, it will have 3 vertices
if len(approx) == 3:
shape = "Yellow triangle- PLANE B"
# if the shape has 4 vertices, it is either a square or
# a rectangle
elif len(approx) == 4:
# compute the bounding box of the contour and use the
# bounding box to compute the aspect ratio
(x, y, w, h) = cv2.boundingRect(approx)
ar = w / float(h)
# a square will have an aspect ratio that is approximately
# equal to one, otherwise, the shape is a rectangle
#shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"
shape = "Yellow Rectangle - PLANE E"
# if the shape is a pentagon, it will have 5 vertices
elif len(approx) == 5: #pentagon
shape = "Yellow Rectangle - PLANE E"
# otherwise, we assume the shape is a circle
else:
shape = "Yellow Circle"
# return the name of the shape
#print shape
return shape