-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobj.py
More file actions
219 lines (184 loc) · 7.62 KB
/
obj.py
File metadata and controls
219 lines (184 loc) · 7.62 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import cv2 as cv
import numpy as np
import os
cap = cv.VideoCapture(0) # open webcam for capture
openingKernel = cv.getStructuringElement(cv.MORPH_RECT, (10, 10)) # Make a kernel for closing (noise removal)
openingKernelRed = cv.getStructuringElement(cv.MORPH_RECT, (2, 2))
# define range of blue color in HSV
# Range for green in HSV
# lower = 40 40 40
# upper = 80 255 255
# TODO: Tweek these values to detect all ranges of color properly
# Values calculated from: https://alloyui.com/examples/color-picker/hsv
# Limits for green range
lower_green = np.array([37, 80, 40])
upper_green = np.array([95, 255, 255])
# Limits for blue range
lower_blue = np.array([100, 80, 50])
upper_blue = np.array([125, 255, 255])
# Limits for yellow range
lower_yellow = np.array([20, 80, 40])
upper_yellow = np.array([35, 255, 255])
# Limits for red
lower_red = np.array([170, 80, 40])
upper_red = np.array([180, 255, 255])
while True:
_, frame = cap.read() # Take each frame
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # Take each frame
# Threshold the HSV image to get only desired colors
maskGreen = cv.inRange(hsv, lower_green, upper_green)
maskBlue = cv.inRange(hsv, lower_blue, upper_blue)
maskYellow = cv.inRange(hsv, lower_yellow, upper_yellow)
maskRed = cv.inRange(hsv, lower_red, upper_red)
# # Clear noise from mask by opening it, then apply gaussian blur
maskGreen = cv.morphologyEx(maskGreen, cv.MORPH_OPEN, openingKernel)
maskGreen = cv.GaussianBlur(maskGreen, (5, 5), 0)
maskBlue = cv.morphologyEx(maskBlue, cv.MORPH_OPEN, openingKernel)
maskBlue = cv.GaussianBlur(maskBlue, (5, 5), 0)
maskYellow = cv.morphologyEx(maskYellow, cv.MORPH_OPEN, openingKernel)
maskYellow = cv.GaussianBlur(maskYellow, (5, 5), 0)
maskRed = cv.morphologyEx(maskRed, cv.MORPH_OPEN, openingKernelRed)
maskRed = cv.GaussianBlur(maskRed, (5, 5), 0)
# Using threshold incase the image is not binary, can be removed
_, threshGreen = cv.threshold(maskGreen, 100, 255, cv.THRESH_BINARY)
_, threshBlue = cv.threshold(maskBlue, 100, 255, cv.THRESH_BINARY)
_, threshYellow = cv.threshold(maskYellow, 100, 255, cv.THRESH_BINARY)
_, threshRed = cv.threshold(maskRed, 100, 255, cv.THRESH_BINARY)
_,contoursGreen, _ = cv.findContours(threshGreen, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
_,contoursBlue, _ = cv.findContours(threshBlue, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
_,contoursYellow, _ = cv.findContours(threshYellow, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
_,contoursRed, _ = cv.findContours(threshRed, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Bitwise-AND mask and original image
mask = cv.bitwise_or(maskBlue, maskGreen)
mask = cv.bitwise_or(mask, maskYellow)
mask=cv.bitwise_or(mask, maskRed)
res = cv.bitwise_and(frame, frame, mask=mask)
numGreen = len(contoursGreen)
numRed=len(contoursRed)
numBlue=len(contoursBlue)
numYellow=len(contoursYellow)
os.system('clear')
if(numBlue>5):
print("Bottle: True")
else:
print("Bottle:False")
if(numRed>100):
print(numRed)
approx=[]
for cont in contoursRed:
if cv.contourArea(cont)>5000:
arc_len=cv.arcLength(cont, True)
approx=cv.approxPolyDP(cont,0.15*arc_len,True)
#print(len(approx))
if(len(approx)==4):
print("Box: True")
else:
print("Box: False")
#print("Box: True")
else:
print("Box:False")
if(numGreen>4):
print(numGreen)
circles = cv.HoughCircles(maskGreen, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=10, maxRadius=100)
#print circles
# ensure at least some circles were found
if circles is not None:
print("Ball: True")
# convert the (x, y) coordinates and radius of the circles to integers
#circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
#for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle in the image
# corresponding to the center of the circle
#cv2.circle(output, (x, y), r, (0, 255, 0), 4)
#cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
#time.sleep(0.5)
#print "Column Number: "
#print x
#print "Row Number: "
#print y
#print "Radius is: "
#print r
else:
print("Ball:False")
else:
print("Ball:False")
if(numYellow>2):
print(numYellow)
circles = cv.HoughCircles(maskYellow, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=10, maxRadius=100)
#print circles
# ensure at least some circles were found
if circles is not None:
print("Disc: True")
# convert the (x, y) coordinates and radius of the circles to integers
#circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
#for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle in the image
# corresponding to the center of the circle
#cv2.circle(output, (x, y), r, (0, 255, 0), 4)
#cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
#time.sleep(0.5)
#print "Column Number: "
#print x
#print "Row Number: "
#print y
#print "Radius is: "
#print r
else:
print("Disc:False")
else:
print("Disc:False")
# for c in contoursGreen:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 700:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "green ball", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# for c in contoursBlue:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 1000:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "bottle", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# for c in contoursYellow:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 1000:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "yellow disk", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# for c in contoursRed:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 1000:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "red box", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# # Show the image and the result
# cv.imshow('res', res)
# cv.imshow('blue', maskBlue)
# cv.imshow('green', maskGreen)
# cv.imshow('yellow', maskYellow)
# cv.imshow('red', maskRed)
# cv.imshow('res', res)
# EXIT if 'esc' key is pressed
k = cv.waitKey(5) & 0xFF
if k == 27:
break
cap.release() # Release the webcam
cv.destroyAllWindows()# Destroy all the windows