-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifa_using_fingers.py
217 lines (181 loc) · 7.77 KB
/
fifa_using_fingers.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
import cv2
import imutils
import numpy as np
from sklearn.metrics import pairwise
import key_press as kp
import time
bg = None
def move(val):
if(val==1):
kp.PressKey(0xCD)
time.sleep(.3)
kp.ReleaseKey(0xCD)
elif(val==2):
kp.PressKey(0xD0)
time.sleep(.3)
kp.ReleaseKey(0xD0)
elif(val==3):
kp.PressKey(0xC8)
time.sleep(.3)
kp.ReleaseKey(0xC8)
def passs(val):
if(val==1):
kp.PressKey(0x1E)
time.sleep(.3)
kp.ReleaseKey(0x1E)
elif(val==2):
kp.PressKey(0x39)
time.sleep(.3)
kp.ReleaseKey(0x39)
elif(val==3):
kp.PressKey(0x1F)
time.sleep(.3)
kp.ReleaseKey(0x1F)
def run_avg(image, accumWeight):
global bg
# initialize the background
if bg is None:
bg = image.copy().astype("float")
return
# compute weighted average, accumulate it and update the background
cv2.accumulateWeighted(image, bg, accumWeight)
#-------------------------------------------------------------------------------
# Function - To segment the region of hand in the image
#-------------------------------------------------------------------------------
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
#-------------------------------------------------------------------------------
# Function - To count the number of fingers in the segmented hand region
#-------------------------------------------------------------------------------
def count(thresholded, segmented):
# find the convex hull of the segmented hand region
chull = cv2.convexHull(segmented)
# find the most extreme points in the convex hull
extreme_top = tuple(chull[chull[:, :, 1].argmin()][0])
extreme_bottom = tuple(chull[chull[:, :, 1].argmax()][0])
extreme_left = tuple(chull[chull[:, :, 0].argmin()][0])
extreme_right = tuple(chull[chull[:, :, 0].argmax()][0])
# find the center of the palm
cX = (extreme_left[0] + extreme_right[0]) / 2
cY = (extreme_top[1] + extreme_bottom[1]) / 2
# find the maximum euclidean distance between the center of the palm
# and the most extreme points of the convex hull
distance = pairwise.euclidean_distances([(cX, cY)], Y=[extreme_left, extreme_right, extreme_top, extreme_bottom])[0]
maximum_distance = distance[distance.argmax()]
# calculate the radius of the circle with 80% of the max euclidean distance obtained
radius = int(0.8 * maximum_distance)
# find the circumference of the circle
circumference = (2 * np.pi * radius)
# take out the circular region of interest which has
# the palm and the fingers
circular_roi = np.zeros(thresholded.shape[:2], dtype="uint8")
# draw the circular ROI
cv2.circle(circular_roi, (int(cX), int(cY)), radius, 255, 1)
# take bit-wise AND between thresholded hand using the circular ROI as the mask
# which gives the cuts obtained using mask on the thresholded hand image
circular_roi = cv2.bitwise_and(thresholded, thresholded, mask=circular_roi)
# compute the contours in the circular ROI
(_, cnts, _) = cv2.findContours(circular_roi.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# initalize the finger count
count = 0
# loop through the contours found
for c in cnts:
# compute the bounding box of the contour
(x, y, w, h) = cv2.boundingRect(c)
# increment the count of fingers only if -
# 1. The contour region is not the wrist (bottom area)
# 2. The number of points along the contour does not exceed
# 25% of the circumference of the circular ROI
if ((cY + (cY * 0.25)) > (y + h)) and ((circumference * 0.25) > c.shape[0]):
count += 1
return count
#-------------------------------------------------------------------------------
# Main function
#-------------------------------------------------------------------------------
if __name__ == "__main__":
# initialize accumulated weight
accumWeight = 0.5
# get the reference to the webcam
camera = cv2.VideoCapture(0)
# region of interest (ROI) coordinates
top, right, bottom, left = 75, 500, 225, 650
top1, right1, bottom1, left1 = 75, 50, 225, 200
# initialize num of frames
num_frames = 0
# calibration indicator
calibrated = False
# keep looping, until interrupted
while(True):
# get the current frame
(grabbed, frame) = camera.read()
# resize the frame
frame = imutils.resize(frame, width=700)
# flip the frame so that it is not the mirror view
frame = cv2.flip(frame, 1)
# clone the frame
clone = frame.copy()
# get the height and width of the frame
(height, width) = frame.shape[:2]
# get the ROI
roi = frame[top:bottom, right:left]
roi1 = frame[top1:bottom1, right1:left1]
# convert the roi to grayscale and blur it
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
gray1 = cv2.cvtColor(roi1, cv2.COLOR_BGR2GRAY)
gray1 = cv2.GaussianBlur(gray1, (7, 7), 0)
# to get the background, keep looking till a threshold is reached
# so that our weighted average model gets calibrated
if num_frames < 30:
run_avg(gray, accumWeight)
run_avg(gray1, accumWeight)
if num_frames == 1:
print("[STATUS] please wait! calibrating...")
elif num_frames == 29:
print("[STATUS] calibration successfull...")
else:
hand = segment(gray)
hand1 = segment(gray1)
if ((hand is not None)):
(thresholded, segmented) = hand
cv2.drawContours(clone, [segmented + (right, top)], -1, (0, 0, 255))
fingers = count(thresholded, segmented)
if(0<fingers<4):
move(fingers)
cv2.putText(clone, str(fingers), (70, 45), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
if((hand1 is not None)):
(thresholded1, segmented1) = hand1
cv2.drawContours(clone, [segmented1 + (right1, top1)], -1, (0, 0, 255))
fingers1 = count(thresholded1, segmented1)
if(0<fingers1<4):
passs(fingers1)
cv2.putText(clone, " "+str(fingers1), (70, 45), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
# draw the segmented hand
cv2.rectangle(clone, (left, top), (right, bottom), (0,0,255), 2)
cv2.rectangle(clone, (left1, top1), (right1, bottom1), (0,0,255), 2)
# increment the number of frames
num_frames += 1
# display the frame with segmented hand
clone = cv2.resize(clone, (500,clone.shape[0]), interpolation = cv2.INTER_AREA)
cv2.imshow("Video Feed", clone)
# observe the keypress by the user
keypress = cv2.waitKey(1) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord("q"):
break
# free up memory
camera.release()
cv2.destroyAllWindows()