-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_cv_main.py
124 lines (108 loc) · 5.24 KB
/
spotify_cv_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
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
import cv2
import json
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from cvzone.HandTrackingModule import HandDetector
import time
import math
with open('params.json', 'r') as file:
config_data = json.load(file)
with open(".cache", "r") as f:
cached_token_info = json.load(f)
print(f"Token expiration: {cached_token_info['expires_at']}")
desired_fps = 30
delay = int(1000 / desired_fps)
auth_manager = SpotifyOAuth(
client_id=config_data.get("CLIENT_ID"),
client_secret=config_data.get("CLIENT_SECRET"),
redirect_uri="http://localhost:3000",
scope="user-read-playback-state user-library-modify user-modify-playback-state user-library-read app-remote-control user-read-private user-read-email playlist-read-private user-library-modify streaming user-read-recently-played user-modify-playback-state user-read-playback-state "
)
sp = spotipy.Spotify(auth_manager=auth_manager)
cap = cv2.VideoCapture(0)
detector = HandDetector(
staticMode=False, maxHands=2, modelComplexity=1, detectionCon=0.8, minTrackCon=0.5
)
finger_info_timer = time.time()
finger_info_interval = 3
while True:
try:
success, img = cap.read()
hands, img = detector.findHands(img, draw=True, flipType=True)
if time.time() - finger_info_timer >= finger_info_interval:
finger_info_timer = time.time()
if hands:
hand1 = hands[0]
if hand1["type"] == "Right":
pos = detector.fingersUp(hand1)
print(f"Right Hand Finger Positions: {pos}")
if pos == [0, 0, 0, 0, 0] or pos == [1, 0, 0, 0, 0]:
if auth_manager.is_token_expired(
sp.auth_manager.get_access_token()
):
sp.auth_manager.refresh_access_token(
sp.auth_manager.get_access_token()["refresh_token"]
)
current_playback = sp.current_playback()
if current_playback and current_playback["is_playing"]:
try:
sp.pause_playback()
print("Song paused successfully!")
except Exception as e:
print(f"Error pausing playback: {e}")
else:
print("Song is not currently playing.")
elif pos == [1, 1, 1, 1, 1] or pos == [0, 1, 1, 1, 1]:
if auth_manager.is_token_expired(
sp.auth_manager.get_access_token()
):
sp.auth_manager.refresh_access_token(
sp.auth_manager.get_access_token()["refresh_token"]
)
current_playback = sp.current_playback()
if current_playback and not current_playback["is_playing"]:
try:
sp.start_playback()
print("Song resumed successfully!")
except Exception as e:
print(f"Error resuming playback: {e}")
else:
print("Song is already playing.")
thumb_tip = hand1["lmList"][4]
index_tip = hand1["lmList"][8]
distance = math.dist(thumb_tip, index_tip)
print(distance)
threshold =70
if distance>threshold:
volume = int((distance - 50) / 2)
volume = max(0, min(100, volume))
print(f"Pinch Distance: {distance}, Adjusted Volume: {volume}")
sp.volume(volume)
if hand1["type"] == "Left":
pos2 = detector.fingersUp(hand1)
print(f"Left Hand Finger Positions: {pos2}")
if pos2 == [0, 0, 0, 0, 0]:
sp.previous_track()
elif pos2 == [1, 1, 1, 1, 1] or pos2 == [0, 1, 1, 1, 1]:
sp.next_track()
elif (
pos2 == [1, 0, 0, 0, 0]
or pos2 == [1, 0, 0, 0, 1]
or pos2 == [0, 0, 0, 0, 1]
):
current_Track = sp.current_playback()
if current_Track:
track_id = current_Track["item"]["id"]
sp.current_user_saved_tracks_add(tracks=[track_id])
print(
f"added current track to your music library.\n"
)
current_Track = sp.current_playback()
print(
f"\ncurrent song: {current_Track['item']['name']}\n"
)
print(" ")
# cv2.imshow("Image", img)
cv2.waitKey(1)
except Exception as e:
print(f"An error occurred: {e}")