-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_data_collection.py
89 lines (66 loc) · 2.21 KB
/
live_data_collection.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
from supabase import create_client, Client
import time
from datetime import datetime, date
import os
from dotenv import load_dotenv
from OCR.detect_screen_text import detect_text
from ultralytics import YOLO
# ml imports
import torch
import torchvision
import torchaudio
import cv2
from music_integration.spotify_functions import get_current_track, create_spotify_oauth
load_dotenv()
model_verson = "v5"
model = YOLO(f"attention_detection/model_iterations/{model_verson}/weights/best.pt")
supabase_url = os.getenv("SUPABASE_URL")
supabase_key = os.getenv("SUPABASE_API_KEY")
supabase: Client = create_client(supabase_url=supabase_url, supabase_key=supabase_key)
sp_oauth = create_spotify_oauth()
auth_url = sp_oauth.get_authorize_url()
print(f"Please go to this URL and authorize the app: {auth_url}")
token_info = sp_oauth.get_access_token()
for samples in range(1000):
print("DETECT ATTENTION")
cam = cv2.VideoCapture(0)
time.sleep(0.11) # give a bit of time to sleep to avoid glare
_, img = cam.read()
results = model(img)[0]
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
attention = f"{results.names[int(class_id)].upper()}"
print("DETECT TEXT")
text_reading = detect_text()
print("DETECT SONG")
info = get_current_track(token_info["access_token"])
if info:
name = info["name"]
artists = info["artists"]
track_id = info["id"]
else:
name = "No song playing"
artists = "No song playing"
track_id = "No song playing"
timestamp = str(datetime.now())
print("INSERT")
rresponse = (
supabase.table("Text+Attention+Spotify")
.insert(
{
"timestamp": timestamp,
"text-reading": text_reading,
"attention-class": attention,
"class-score": round(score, 6),
"model-version": f"attention-detection-{model_verson}",
"track-title": name,
"artist": artists,
"track-id": str(track_id),
}
)
.execute()
)
delay = 15
print(f"wait {delay} seconds")
time.sleep(delay)
print("resume collection")