/
finger_count.py
101 lines (84 loc) · 3.18 KB
/
finger_count.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
# From https://github.com/madhav727/medium
import numpy as np
import cv2 as cv
import os
import time
import pyaudiotest as pa
def skinmask(img):
hsvim = cv.cvtColor(img, cv.COLOR_BGR2HSV)
lower = np.array([0, 48, 80], dtype = "uint8")
upper = np.array([20, 255, 255], dtype = "uint8")
skinRegionHSV = cv.inRange(hsvim, lower, upper)
blurred = cv.blur(skinRegionHSV, (2,2))
ret, thresh = cv.threshold(blurred,0,255,cv.THRESH_BINARY)
return thresh
def getcnthull(mask_img):
__, contours, hierarchy = cv.findContours(mask_img, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = max(contours, key=lambda x: cv.contourArea(x))
hull = cv.convexHull(contours)
return contours, hull
def getdefects(contours):
hull = cv.convexHull(contours, returnPoints=False)
defects = cv.convexityDefects(contours, hull)
return defects
# cap = cv.VideoCapture(0) # '0' for webcam
# while cap.isOpened():
while 1:
# _, img = cap.read()
os.system('fswebcam -r 224x224 --no-banner /home/pi/Desktop/frame.jpg')
time.sleep(0.1)
img = cv.imread('/home/pi/Desktop/frame.jpg', cv.IMREAD_COLOR)
# cv.imshow("img", img)
# cv.waitKey()
# cv.destroyAllWindows()
# try:
mask_img = skinmask(img)
contours, hull = getcnthull(mask_img)
cv.drawContours(img, [contours], -1, (255,255,0), 2)
cv.drawContours(img, [hull], -1, (0, 255, 255), 2)
defects = getdefects(contours)
if defects is not None:
cnt = 0
for i in range(defects.shape[0]): # calculate the angle
s, e, f, d = defects[i][0]
start = tuple(contours[s][0])
end = tuple(contours[e][0])
far = tuple(contours[f][0])
a = np.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = np.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = np.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) # cosine theorem
print("start: ", start, "end: ", end, "far:", far, "a", a, "b", b, "c", c, "angle", angle)
if angle <= np.pi / 2: # angle less than 90 degree, treat as fingers
cnt += 1
cv.circle(img, far, 4, [0, 0, 255], -1)
if cnt > 0:
cnt = cnt+1
cv.putText(img, str(cnt), (0, 50), cv.FONT_HERSHEY_SIMPLEX,1, (255, 0, 0) , 2, cv.LINE_AA)
print("finger cnt: ", cnt)
# Audio
curr_x = start[0] # hand position
x_max = 640 # Screen coords
key_range = 49 # Range of notes to be mapped/used
key_max = 57 # on piano/tone generator
root = int(curr_x / x_max * (key_max - key_range) + key_range) # Conversion factor for x-cord and 88 keys on piano
if (root in [49, 50, 51, 52, 53]):
#I chord - A
root = 49
print('hi')
elif (root in [54, 55]):# IV Chord - D
root = 54
elif (root in [56, 57]):
# V Chord - E
root = 56
pa.play_note(root)
cv.imshow("img", img)
# cv.waitKey(0)
# cv.destroyAllWindows()
# except:
# print('hi')
# pass
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()