-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (51 loc) · 2.1 KB
/
main.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
import cv2, funcs
def main():
cv2.namedWindow("preview")
handSpace = 0.33333333333
keyMult = 2**(1 / 12)
minF = 261.62
maxF = 523.25
vc = cv2.VideoCapture(0)
while True:
rval, frame = vc.read()
if frame is not None:
frame = cv2.flip(frame, 1)
h, w, pix = frame.shape
crop1, percLeft, numLeft = funcs.findHand(
frame[0:2 * h * handSpace, 0:w * handSpace])
crop2, percRight, numRight = funcs.findHand(
frame[0:2 * h * handSpace, 2 * w * handSpace:w])
frame[0:2 * h * handSpace, 0:w * handSpace] = crop1
frame[0:2 * h * handSpace, 2 * handSpace * w:w] = crop2
keys = [
"C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B"
]
if int(12 * (1 - percLeft) / keyMult) < 12:
key = keys[int(12 * (1 - percLeft) / keyMult)]
else:
key = keys[11]
instrument = "None"
if numLeft == 2:
instrument = "Guitar"
if numLeft == 3:
instrument = "Piano"
if numLeft == 4:
instrument = "Flute"
if numLeft == 5:
instrument = "Sine"
if instrument == "None":
key = "--"
amplitude = 1.000001 * (1 - percRight)
cv2.putText(frame, instrument, (225, 350), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 1)
cv2.putText(frame, key, (225, 400), cv2.FONT_HERSHEY_SIMPLEX, 2,
(255, 255, 255), 2)
cv2.circle(frame, (375, 360), int(30 * amplitude), [0, 0, 255], 1)
cv2.circle(frame, (375, 360), int(26 * amplitude), [0, 0, 255], -1)
cv2.circle(frame, (375, 360), int(20 * amplitude), [100, 100, 255], -1)
cv2.circle(frame, (375, 360), int(15 * amplitude), [200, 200, 255], -1)
cv2.imshow("preview", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == '__main__':
main()