-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
81 lines (70 loc) · 2.63 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
72
73
74
75
76
77
78
79
80
81
import cv2
import time
from src.HandTracking import *
from src.Spotify import *
from src.TokenValidator import *
def volume(upCount, fingersUp, spotify):
if upCount == 4 and fingersUp[1] == 0:
volume = spotify.getVolume()+10
print("Increasing volume to:")
print(volume)
spotify.setVolume(volume if volume <= 100 else 100)
return True
elif upCount == 4 and fingersUp[3] == 0:
volume = spotify.getVolume()-10
print("Decreasing volume to:")
print(volume)
spotify.setVolume(volume if volume >= 0 else 0)
return True
return False
def playNext(upCount, fingersUp, spotify):
if upCount == 1 and fingersUp[1] == 1:
print("Skipping to next song")
spotify.playNext()
time.sleep(0.2)
return True
return False
def playPrevious(upCount, fingersUp, spotify):
if upCount == 2 and fingersUp[1] == 1 and fingersUp[2] == 1:
print("Skipping to previous song")
spotify.playPrevious()
time.sleep(0.2)
return True
return False
def playPause(upCount, fingersUp, spotify):
if upCount == 4 and fingersUp[2] == 0:
spotify.playPause()
return True
return False
def main():
w, h = 1280, 720
capture = cv2.VideoCapture(0)
capture.set(3, w)
capture.set(4, h)
detector = HandDetector(max_num_hands = 1, min_detection_confidence=0.75, min_tracking_confidence=.5)
validator = TokenValidator()
spotify = Spotify(validator)
area = 0
while True:
success, img = capture.read()
img = detector.findHands(img)
lmList, bbox = detector.findPosition(img, draw=False, bounding=True)
if len(lmList) != 0:
area = (bbox[2]-bbox[0]) * (bbox[3]-bbox[1]) // 100
if area > 150:
fingersUp, upCount = detector.fingersUp()
if playPause(upCount, fingersUp, spotify): pass
elif playNext(upCount, fingersUp, spotify): pass
elif playPrevious(upCount, fingersUp, spotify): pass
elif volume(upCount, fingersUp, spotify): pass
cv2.imshow("Spotify Gestures", img)
if cv2.waitKey(1) == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
with open(os.path.join(os.path.dirname(__file__), "data", "VARS.json"), "r") as f: VARS = json.load(f)
if VARS["SPOTIFY_USER"] == "" or VARS["SPOTIFY_PWD"] == "" or VARS["USER_AGENT"] == "" or VARS["SCREEN_WIDTH"] == "" or VARS["SCREEN_HEIGHT"] == "":
print("Please run setup.py as program data has not been initialized")
else:
main()