-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathreal_time_gesture_detection.py
302 lines (251 loc) · 9.94 KB
/
real_time_gesture_detection.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#! /usr/bin/env python3
import copy
import cv2
import numpy as np
from keras.models import load_model
from phue import Bridge
from soco import SoCo
import pygame
import time
# General Settings
prediction = ''
action = ''
score = 0
img_counter = 500
# pygame.event.wait()
class Volume(object):
def __init__(self):
self.level = .5
def increase(self, amount):
self.level += amount
print(f'New level is: {self.level}')
def decrease(self, amount):
self.level -= amount
print(f'New level is: {self.level}')
vol = Volume()
# Turn on/off the ability to save images, or control Philips Hue/Sonos
save_images, selected_gesture = False, 'peace'
smart_home = True
# Philips Hue Settings
bridge_ip = '192.168.0.103'
b = Bridge(bridge_ip)
on_command = {'transitiontime': 0, 'on': True, 'bri': 254}
off_command = {'transitiontime': 0, 'on': False, 'bri': 254}
# Sonos Settings
sonos_ip = '192.168.0.104'
sonos = SoCo(sonos_ip)
gesture_names = {0: 'Fist',
1: 'L',
2: 'Okay',
3: 'Palm',
4: 'Peace'}
model = load_model('/Users/brenner/project_kojak/models/VGG_cross_validated.h5')
def predict_rgb_image(img):
result = gesture_names[model.predict_classes(img)[0]]
print(result)
return (result)
def predict_rgb_image_vgg(image):
image = np.array(image, dtype='float32')
image /= 255
pred_array = model.predict(image)
print(f'pred_array: {pred_array}')
result = gesture_names[np.argmax(pred_array)]
print(f'Result: {result}')
print(max(pred_array[0]))
score = float("%0.2f" % (max(pred_array[0]) * 100))
print(result)
return result, score
# parameters
cap_region_x_begin = 0.5 # start point/total width
cap_region_y_end = 0.8 # start point/total width
threshold = 60 # binary threshold
blurValue = 41 # GaussianBlur parameter
bgSubThreshold = 50
learningRate = 0
# variableslt
isBgCaptured = 0 # bool, whether the background captured
triggerSwitch = False # if true, keyboard simulator works
def remove_background(frame):
fgmask = bgModel.apply(frame, learningRate=learningRate)
kernel = np.ones((3, 3), np.uint8)
fgmask = cv2.erode(fgmask, kernel, iterations=1)
res = cv2.bitwise_and(frame, frame, mask=fgmask)
return res
# Camera
camera = cv2.VideoCapture(0)
camera.set(10, 200)
while camera.isOpened():
ret, frame = camera.read()
frame = cv2.bilateralFilter(frame, 5, 50, 100) # smoothing filter
frame = cv2.flip(frame, 1) # flip the frame horizontally
cv2.rectangle(frame, (int(cap_region_x_begin * frame.shape[1]), 0),
(frame.shape[1], int(cap_region_y_end * frame.shape[0])), (255, 0, 0), 2)
cv2.imshow('original', frame)
# Run once background is captured
if isBgCaptured == 1:
img = remove_background(frame)
img = img[0:int(cap_region_y_end * frame.shape[0]),
int(cap_region_x_begin * frame.shape[1]):frame.shape[1]] # clip the ROI
# cv2.imshow('mask', img)
# convert the image into binary image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0)
# cv2.imshow('blur', blur)
ret, thresh = cv2.threshold(blur, threshold, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Add prediction and action text to thresholded image
# cv2.putText(thresh, f"Prediction: {prediction} ({score}%)", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255))
# cv2.putText(thresh, f"Action: {action}", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255)) # Draw the text
# Draw the text
cv2.putText(thresh, f"Prediction: {prediction} ({score}%)", (50, 30), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255))
cv2.putText(thresh, f"Action: {action}", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255)) # Draw the text
cv2.imshow('ori', thresh)
# get the contours
thresh1 = copy.deepcopy(thresh)
_, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
length = len(contours)
maxArea = -1
if length > 0:
for i in range(length): # find the biggest contour (according to area)
temp = contours[i]
area = cv2.contourArea(temp)
if area > maxArea:
maxArea = area
ci = i
res = contours[ci]
hull = cv2.convexHull(res)
drawing = np.zeros(img.shape, np.uint8)
cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2)
cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3)
cv2.imshow('output', drawing)
# Keyboard OP
k = cv2.waitKey(10)
if k == 27: # press ESC to exit all windows at any time
break
elif k == ord('b'): # press 'b' to capture the background
bgModel = cv2.createBackgroundSubtractorMOG2(0, bgSubThreshold)
b.set_light(6, on_command)
time.sleep(2)
isBgCaptured = 1
print('Background captured')
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load('/Users/brenner/1-05 Virtual Insanity.mp3')
pygame.mixer.music.set_volume(vol.level)
pygame.mixer.music.play()
pygame.mixer.music.set_pos(50)
pygame.mixer.music.pause()
elif k == ord('r'): # press 'r' to reset the background
time.sleep(1)
bgModel = None
triggerSwitch = False
isBgCaptured = 0
print('Reset background')
elif k == 32:
# If space bar pressed
cv2.imshow('original', frame)
# copies 1 channel BW image to all 3 RGB channels
target = np.stack((thresh,) * 3, axis=-1)
target = cv2.resize(target, (224, 224))
target = target.reshape(1, 224, 224, 3)
prediction, score = predict_rgb_image_vgg(target)
if smart_home:
if prediction == 'Palm':
try:
action = "Lights on, music on"
# sonos.play()
pygame.mixer.music.unpause()
# Turn off smart home actions if devices are not responding
except ConnectionError:
smart_home = False
pass
elif prediction == 'Fist':
try:
action = 'Lights off, music off'
b.set_light(6, off_command)
# sonos.pause()
pygame.mixer.music.pause()
except ConnectionError:
smart_home = False
pass
elif prediction == 'L':
try:
action = 'Volume down'
# sonos.volume -= 15
vol.decrease(0.2)
pygame.mixer.music.set_volume(vol.level)
except ConnectionError:
smart_home = False
pass
elif prediction == 'Okay':
try:
action = 'Volume up'
# sonos.volume += 15
vol.increase(0.2)
pygame.mixer.music.set_volume(vol.level)
except ConnectionError:
smart_home = False
pass
elif prediction == 'Peace':
try:
action = ''
except ConnectionError:
smart_home = False
pass
else:
pass
if save_images:
img_name = f"./frames/drawings/drawing_{selected_gesture}_{img_counter}.jpg".format(
img_counter)
cv2.imwrite(img_name, drawing)
print("{} written".format(img_name))
img_name2 = f"./frames/silhouettes/{selected_gesture}_{img_counter}.jpg".format(
img_counter)
cv2.imwrite(img_name2, thresh)
print("{} written".format(img_name2))
img_name3 = f"./frames/masks/mask_{selected_gesture}_{img_counter}.jpg".format(
img_counter)
cv2.imwrite(img_name3, img)
print("{} written".format(img_name3))
img_counter += 1
elif k == ord('t'):
print('Tracker turned on.')
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
# Select Region of Interest (ROI)
r = cv2.selectROI(frame)
# Crop image
imCrop = frame[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])]
# setup initial location of window
r, h, c, w = 250, 400, 400, 400
track_window = (c, r, w, h)
# set up the ROI for tracking
roi = imCrop
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
# Setup the termination criteria, either 10 iteration or move by at least 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
while (1):
ret, frame = cap.read()
if ret == True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
# apply meanshift to get the new location
ret, track_window = cv2.CamShift(dst, track_window, term_crit)
# Draw it on image
pts = cv2.boxPoints(ret)
pts = np.int0(pts)
img2 = cv2.polylines(frame, [pts], True, (0, 255, 0), 2)
cv2.imshow('img2', img2)
k = cv2.waitKey(60) & 0xff
if k == 27: # if ESC key
break
else:
cv2.imwrite(chr(k) + ".jpg", img2)
else:
break
cv2.destroyAllWindows()
cap.release()