-
Notifications
You must be signed in to change notification settings - Fork 1
/
frets.py
132 lines (108 loc) · 3.07 KB
/
frets.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
#Called by the server to start a session
#Defines the basic flow of execution of each session
#inlcudes configuring the neck, playing and saving
import lib
import cv2
import time
import play
vc = cv2.VideoCapture(0)
def init():
#Initialize and configure
configure()
start()
cv2.destroyAllWindows()
def configure():
time.sleep(3)
ret, frame = vc.read()
if ret:
hsvframe = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
fingerImages = lib.filterFingers(hsvframe)
fingerPositions = lib.getPositions(fingerImages)
lowerPos = lib.getLowerBlob(hsvframe)
lib.initNeck(fingerPositions[0],lowerPos)
time.sleep(1)
#To get the strum direction and timing
def start():
ret, frame = vc.read()
# Timer for saving strum timings and saving music
start = time.time()
gap = 0
prevStrum = ""
song = []
prevTime = 0.00
elapsed = 0.00
# Motion detection flags
prevPos = 0
direction = 0
up = 0
down = 0
firstframe = 1
while ret:
# Change color space for better detection
hsvframe = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
fingerImages = lib.filterFingers(hsvframe)
cv2.imshow('red', fingerImages[0])
cv2.imshow('green', fingerImages[1])
cv2.imshow('blue', fingerImages[2])
fingerPositions = lib.getPositions(fingerImages)
# Detect the mode of playback
mode = lib.getMode(fingerPositions)
distance = lib.getDistance(fingerPositions)
# Show the mode on screen
cv2.putText(frame, mode , (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 4, 255)
cv2.imshow('preview', frame)
# Find the postion of lower strumming hand
lowerPos = lib.getLowerBlob(hsvframe)
# Motion detection for lower hand
if prevPos != 0:
disp = lowerPos[0][1] - prevPos
direction = direction + disp
if direction < -200:
up = 1
direction = 0
if direction > 200:
down = 1
direction = 0
if firstframe == 1:
firstframe = 0
prevPos = lowerPos[0][1]
# Perform the playback and append the strum in song
if down == 1:
if gap == 1:
elapsed = time.time() - start
song.append([prevStrum, elapsed])
gap == 0
if prevTime != 0.00:
song.append([prevStrum, (elapsed-prevTime)])
else:
song.append([prevStrum, 0.00])
prevTime = elapsed
play.play(lib.getPattern(mode,distance,'down'))
if gap == 0:
start = time.time()
prevStrum = lib.getPattern(mode,distance, 'down')
gap = 1
down = 0
else:
if up == 1:
if gap == 1:
elapsed = time.time() - start
song.append([prevStrum, elapsed])
gap == 0
if prevTime != 0.00:
song.append([prevStrum, (elapsed-prevTime)])
else:
song.append([prevStrum, 0.00])
prevTime = elapsed
play.play(lib.getPattern(mode,distance,'up'))
if gap == 0:
start = time.time()
prevStrum = lib.getPattern(mode,distance,'up')
gap = 1
up = 0
ret, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: #Ends the session and save the song. Press ESCAPE key
play.save('muse', song, 0.05, [0, prevTime, 0.1])
break
init()