-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCV_Airdrum.py
162 lines (141 loc) · 6.24 KB
/
CV_Airdrum.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
# onemotion.com
import cv2
import numpy as np
import pyautogui
import imutils
def Press(key):
pyautogui.press(key)
cap = cv2.VideoCapture(0);
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = imutils.resize(frame, height=700, width=900)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lowred = np.array([131, 90, 106])
highred = np.array([255, 255, 255])
lowblue = np.array([40, 150, 116])
highblue = np.array([255, 255, 255])
red_mask = cv2.inRange(hsv, lowred, highred)
blue_mask = cv2.inRange(hsv, lowblue, highblue)
# image/frame, start_point, end_point, color, thickness
cv2.rectangle(frame, (0, 0), (200, 150), (255, 0, 0), 1)
cv2.putText(frame, 'RIDE', (70, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3, cv2.LINE_AA)
cv2.rectangle(frame, (210, 0), (430, 150), (0, 0, 255), 1)
cv2.putText(frame, 'RIDE BELL', (245, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3, cv2.LINE_AA)
cv2.rectangle(frame, (440, 0), (650, 150), (255, 0, 0), 1)
cv2.putText(frame, 'HITHAT close', (445, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3, cv2.LINE_AA)
cv2.rectangle(frame, (660, 0), (900, 150), (0, 0, 255), 1)
cv2.putText(frame, 'CRASH', (730, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3, cv2.LINE_AA)
cv2.rectangle(frame, (0, 160), (50, 370), (255, 0, 0), 1)
cv2.putText(frame, 'SNARE', (10, 290), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3, cv2.LINE_AA)
cv2.rectangle(frame, (0, 380), (50, 570), (0, 0, 255), 1)
cv2.putText(frame, 'SNARE RIM', (10, 500), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3, cv2.LINE_AA)
cv2.rectangle(frame, (850, 160), (900, 370), (255, 0, 0), 1)
cv2.putText(frame, 'HIT HAT', (770, 290), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3, cv2.LINE_AA)
cv2.rectangle(frame, (850, 380), (900, 570), (0, 0, 255), 1)
cv2.putText(frame, 'HIT HAT OPEN', (670, 500), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3, cv2.LINE_AA)
cv2.rectangle(frame, (0, 580), (200, 700), (255, 0, 0), 1)
cv2.putText(frame, 'TOM HI', (50, 640), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3, cv2.LINE_AA)
cv2.rectangle(frame, (210, 580), (430, 700), (0, 0, 255), 1)
cv2.putText(frame, 'TOM MID', (250, 640), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3, cv2.LINE_AA)
cv2.rectangle(frame, (440, 580), (650, 700), (255, 0, 0), 1)
cv2.putText(frame, 'TOM LOW', (480, 640), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3, cv2.LINE_AA)
cv2.rectangle(frame, (660, 580), (900, 700), (0, 0, 255), 1)
cv2.putText(frame, 'KICK', (740, 640), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3, cv2.LINE_AA)
# for the red Object
contours, hierachy = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
# startpoint, endpoint, color, thickness
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
print((x, y))
if x > 0 and y > 0 and x < 200 and y < 150:
Press('7') # RIDE
break
if x > 210 and y > 0 and x < 430 and y < 150:
Press('8') # RIDE BELL
break
if x > 440 and y > 0 and x < 650 and y < 150:
Press('6') # HIT HAT CLOSE
break
if x > 660 and y > 0 and x < 900 and y < 150:
Press('9') # CRASH
break
if x > 0 and y > 160 and x < 50 and y < 370:
Press('2') # SNARE
break
if x > 0 and y > 380 and x < 50 and y < 570:
Press('3') # SNARE RIM
break
if x > 850 and y > 160 and x < 900 and y < 370:
Press('4') # HIT HAT
break
if x > 850 and y > 380 and x < 900 and y < 570:
Press('5') # HIT HAT OPEN
break
if x > 0 and y > 580 and x < 200 and y < 700:
Press('q') # TOM HI
break
if x > 210 and y > 580 and x < 430 and y < 700:
Press('w') # TOM MID
break
if x > 440 and y > 580 and x < 650 and x < 700:
Press('e') # TOM LOW
break
if x > 660 and y > 580 and x < 900 and y < 700:
Press('1') # HIT HAT OPEN
break
break
# for the blue Object
contours, hierachy = cv2.findContours(blue_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
# startpoint, endpoint, color, thickness
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
print((x, y))
if x > 0 and y > 0 and x < 200 and y < 150:
Press('7') # RIDE
break
if x > 210 and y > 0 and x < 430 and y < 150:
Press('8') # RIDE BELL
break
if x > 440 and y > 0 and x < 650 and y < 150:
Press('6') # HIT HAT CLOSE
break
if x > 660 and y > 0 and x < 900 and y < 150:
Press('9') # CRASH
break
if x > 0 and y > 160 and x < 50 and y < 370:
Press('2') # SNARE
break
if x > 0 and y > 380 and x < 50 and y < 570:
Press('3') # SNARE RIM
break
if x > 850 and y > 160 and x < 900 and y < 370:
Press('4') # HIT HAT
break
if x > 850 and y > 380 and x < 900 and y < 570:
Press('5') # HIT HAT OPEN
break
if x > 0 and y > 580 and x < 200 and y < 700:
Press('q') # TOM HI
break
if x > 210 and y > 580 and x < 430 and y < 700:
Press('w') # TOM MID
break
if x > 440 and y > 580 and x < 650 and x < 700:
Press('e') # TOM LOW
break
if x > 660 and y > 580 and x < 900 and y < 700:
Press('1') # HIT HAT OPEN
break
break
cv2.imshow("frame", frame)
# cv2.imshow("res", res)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()