Repository navigation
Expand file tree
/
Copy pathColorDetection.py
More file actions
246 lines (194 loc) · 9.54 KB
/
Copy pathColorDetection.py
File metadata and controls
246 lines (194 loc) · 9.54 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#importing Modules
import cv2
import numpy as np
from getkey import getkey, keys
import time
import threading
from queue import Queue
q = Queue()
def play_video(): #function that plays a mp4 video file
print('entering play video function')
filePath = None
while q.empty():
pass
filePath = q.get()
cap = cv2.VideoCapture(filePath)
print('filepath :' + filePath)
while True:
while q.empty():
cap = cv2.VideoCapture(filePath)
print('got the file')
if cap.isOpened()== False:
print("Oops! Error opening file.")
#read until end of video
while cap.isOpened() and q.empty():
ret, frame = cap.read()
if ret == True:
# Display the resulting frame
cv2.imshow('Frame', frame)
cv2.waitKey(1)
if cap:
cap.release()
filePath = q.get()
print('filepath :' + filePath)
cap.release()
cv2.destroyAllWindows()
def play_visualEffect(first_color, second_color=None): # list all cases and play visual effects
if first_color == "red":
if second_color == "blue":
print("play red+blue")
q.put('movies/_red + blue.mp4')
elif second_color == 'yellow':
print('play red + yellow')
q.put('movies/_red + yellow.mp4')
else:
print("play red")
q.put('movies/_red.mp4')
elif first_color == "blue":
if second_color == "red":
print("play blue+red")
q.put('movies/_blue + red.mp4')
elif second_color == "yellow":
print("play blue + yellow")
q.put('movies/_blue + yellow.mp4')
else : #second color is yellow
print("play blue")
q.put('movies/_blue.mp4')
else: #first color is yellow
if second_color == "red":
print("play yellow+red")
q.put('movies/_yellow + red.mp4')
elif second_color == "blue":
print("play yellow+blue")
q.put('movies/_yellow + blue.mp4')
else:
print("play yellow")#second color is yellow
q.put('movies/_yellow.mp4')
def recognize_balls():
first_color = None
second_color = None
blue_detected = False
red_detected = False
yellow_detected = False
print("Welcome to the game!")
# Capture video from webcam
cam = cv2.VideoCapture(1)
# ret, img = cam.read()
if cam.isOpened():
boolean, img = cam.read()
else:
boolean = False
counter = 1
while boolean:
time.sleep(0.1)
boolean, img = cam.read()
if counter % 10 == 0:
counter+=1
else:
counter+=1
continue
# if cv2.waitKey(20) == ord('r'):
# reset = True
# print "Reseting the game."
#converting frame(img) from BGR (Blue-Green-Red) to HSV (hue-saturation-value)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) # mask of blue
img_blue = cv2.bitwise_and(hsv, hsv, mask = mask_blue) # filtered img of blue component
lower_red = np.array([0, 70, 50])
upper_red = np.array([10, 255, 255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
lower_red = np.array([170, 70, 50])
upper_red = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower_red, upper_red) #2 masks for 2 ranges of color red
mask_red = mask1 + mask2 # join the masks
img_red = cv2.bitwise_and(hsv, hsv, mask = mask_red) # filtered img of red component
lower_yellow = np.array([20,100,100])
upper_yellow = np.array([30,255,255])
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow) # mask of yellow
img_yellow = cv2.bitwise_and(hsv, hsv, mask = mask_yellow) # filtered img of yellow component
# 2) morphological operations
img_blue_processed = cv2.dilate(img_blue, None, iterations=2)
img_blue_processed = cv2.erode(img_blue_processed, None, iterations=2)
gray_img_blue = cv2.cvtColor(img_blue_processed, cv2.COLOR_BGR2GRAY) #converts the img to 1 channel
img_red_processed = cv2.dilate(img_red, None, iterations=2)
img_red_processed = cv2.erode(img_red_processed, None, iterations=2)
gray_img_red = cv2.cvtColor(img_red_processed, cv2.COLOR_BGR2GRAY) #converts the img to 1 channel
img_yellow_processed = cv2.dilate(img_yellow, None, iterations=2)
img_yellow_processed = cv2.erode(img_yellow_processed, None, iterations=2)
gray_img_yellow = cv2.cvtColor(img_yellow_processed, cv2.COLOR_BGR2GRAY) #converts the img to 1 channel
# 3) contour detection
contours_blue = cv2.findContours(gray_img_blue.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
contours_blue = sorted(contours_blue, key=cv2.contourArea, reverse=True)
contours_red = cv2.findContours(gray_img_red.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
contours_red = sorted(contours_red, key=cv2.contourArea, reverse=True)
contours_yellow = cv2.findContours(gray_img_yellow.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
contours_yellow = sorted(contours_yellow, key=cv2.contourArea, reverse=True)
#4) filtering contours by size
contours = [contours_blue, contours_red, contours_yellow]
for x in contours:
if x:
area = cv2.contourArea(x[0])
rect = cv2.minAreaRect(x[0])
entroid = (0,0)
if area > 2000: #only coutours of the size of a t-shirt should be drawn out
#get coordinates of the centroid of the largest valid contour
box = cv2.boxPoints(rect)
pt_upperleft = box[0]
pt_lowerright = box[2]
center_x = (pt_upperleft[0] + pt_lowerright[0])/2
center_y = (pt_upperleft[1] + pt_lowerright[1])/2
centroid = (center_x, center_y) #This coordinate should be stored as a global variable. but where should it be declared?
box = np.int0(box)
#duration = time.time() - startTime
if True: #delay interval
if x == contours_blue:
cv2.drawContours(img, [box], 0, (0, 0, 255), 1) #draw out the contour in rectangle
cv2.putText(img, 'blue', (pt_upperleft[0], pt_upperleft[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 1, cv2.LINE_AA)
blue_detected = True
if x == contours_red:
cv2.drawContours(img, [box], 0, (255, 0, 0), 1) #draw out the contour in rectangle
cv2.putText(img, 'red', (pt_upperleft[0], pt_upperleft[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 1, cv2.LINE_AA)
red_detected = True
if x == contours_yellow:
cv2.drawContours(img, [box], 0, (180, 180, 0), 1) #draw out the contour in rectangle
cv2.putText(img, 'yellow', (pt_upperleft[0], pt_upperleft[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (180, 180, 0), 1, cv2.LINE_AA)
yellow_detected = True
def get_color(): # get the current color
resultColor = None
if blue_detected:
resultColor = "blue"
if red_detected:
resultColor = "red"
if yellow_detected:
resultColor = "yellow"
return resultColor
def color_detected(): # returns true if a color is detected
return red_detected or blue_detected or yellow_detected
if red_detected or blue_detected or yellow_detected:
print("A ball is thrown in, processing...")
#color update
if not first_color and color_detected:
first_color = get_color()
print ("First color detected: " + first_color)
play_visualEffect(first_color)
elif first_color and not second_color and color_detected:
second_color = get_color()
print("Second color detected: " + second_color)
play_visualEffect(first_color, second_color)
elif first_color and second_color and color_detected:
new_color = get_color()
first_color = second_color
second_color = new_color
print ("New color detected: " + new_color)
play_visualEffect(first_color, second_color)
blue_detected = False
red_detected = False
yellow_detected = False
print("Ready for another ball!")
#cv2.namedWindow('webcam')
#cv2.imshow('webcam', img)
thread1 = threading.Thread(target=recognize_balls)
thread1.start()
play_video()