-
Notifications
You must be signed in to change notification settings - Fork 4
/
FinalProjDriver.py
250 lines (192 loc) · 7.43 KB
/
FinalProjDriver.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import cv2 as cv
import numpy as np
from collections import deque
import copy
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from pathlib import Path
import pandas as pd
cap = cv.VideoCapture(0)
#red, orange, yellow, green, blue, purple, pink
#i have RGB
#it goes BGR
#colors = [(38, 38, 255)]
colors = [(38, 38, 255), (19, 148, 249), (17, 250, 250), (17, 250, 48), (255, 207, 47), (255, 47, 165), (248, 107, 253)]
colorIndx = 0
redPts = [deque(maxlen=1000)]
orangePts = [deque(maxlen=1000)]
yellowPts = [deque(maxlen=1000)]
greenPts = [deque(maxlen=1000)]
bluePts = [deque(maxlen=1000)]
purplePts = [deque(maxlen=1000)]
redIndx = 0
orangeIndx = 0
yellowIndx = 0
greenIndx = 0
blueIndx = 0
purpleIndx = 0
def find_histogram(clt):
numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
(hist, _) = np.histogram(clt.labels_, bins=numLabels)
hist = hist.astype("float")
hist /= hist.sum()
return hist
def plot_colors2(hist, centroids):
bar = np.zeros((50, 300, 3), dtype="uint8")
startX = 0
for(percent, color) in zip(hist, centroids):
endX = startX + (percent * 300)
cv.rectangle(bar, (int(startX), 0), (int(endX), 50), color.astype("uint8").tolist(), -1)
startX = endX
return bar, color, percent
while(1):
_, frame = cap.read()
jamDraw = frame.copy()
blackImg = np.zeros((512, 512, 3), np.uint8)
flippedIMG = cv.flip(frame, 0)
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
lowerRed = np.array([90, 100, 100])
upperRed = np.array([100, 255, 255])
mask = cv.inRange(hsv, lowerRed, upperRed)
kernel = np.ones((5, 5), np.uint8)
dilate = cv.dilate(mask, kernel, iterations=2)
ret, thresh = cv.threshold(dilate, 15, 275, cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
#parameters: input, contours to be passed in, draw all contours (-1) or index to a specific one, color, thickness
img = cv.drawContours(frame, contours, -1, (0, 255, 0), 3)
center = None
img2 = None
#drawing the colors to choose from
points = [redPts, orangePts, yellowPts, greenPts, bluePts, purplePts]
for i in range(len(points)):
for j in range(len(points[i])):
for k in range(1, len(points[i][j])):
if points[i][j][k-1] is None or points[i][j][k] is None:
continue
img2 = cv.line(frame, points[i][j][k - 1], points[i][j][k], colors[i], 23)
#clear
img = cv.rectangle(frame, (0, 60), (80, 90), (255, 255, 255), -1)
#red
img = cv.rectangle(frame, (80, 60), (160, 90), colors[0], -1)
#orange
img = cv.rectangle(frame, (160, 60), (240, 90), colors[1], -1)
#yellow
img = cv.rectangle(frame, (240, 60), (320, 90), colors[2], -1)
#green
img = cv.rectangle(frame, (320, 60), (400, 90), colors[3], -1)
#blue
img = cv.rectangle(frame, (400, 60), (480, 90), colors[4], -1)
#purple
img = cv.rectangle(frame, (480, 60), (560, 90), colors[5], -1)
#take picture
img = cv.rectangle(frame, (570, 390), (650, 420), (255, 255, 255), -1)
#img = cv.putText(frame, "Picture", (580, 410), cv.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3, cv.LINE_AA)
if len(contours) > 0:
M = cv.moments(thresh)
if (M['m00'] > 0):
# calculate x,y coordinate of center
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 700, 700
cv.circle(frame, (cX, cY), 5, (255, 255, 255), -1)
center = cX, cY
if center[1] <= 90:
if 0 <= center[0] <= 80:
redPts = [deque(maxlen=1000)]
orangePts = [deque(maxlen=1000)]
yellowPts = [deque(maxlen=1000)]
greenPts = [deque(maxlen=1000)]
bluePts = [deque(maxlen=1000)]
purplePts = [deque(maxlen=1000)]
redIndx = 0
orangeIndx = 0
yellowIndx = 0
greenIndx = 0
blueIndx = 0
purpleIndx = 0
elif 80 <= center[0] <= 160:
colorIndx = 0
elif 160 <= center[0] <= 240:
colorIndx = 1
elif 240 <= center[0] <= 320:
colorIndx = 2
elif 320 <= center[0] <= 400:
colorIndx = 3
elif 400 <= center[0] <= 480:
colorIndx = 4
elif 480 <= center[0] <= 560:
colorIndx = 5
elif center[1] >= 390:
if center[0] >= 570:
cv.imwrite('jamDrawImg.jpg', img2)
drawing = cv.imread('jamDrawImg.jpg')
drawing = drawing[90:390, 0:700]
drawing = cv.cvtColor(drawing, cv.COLOR_BGR2RGB)
drawing = drawing.reshape((drawing.shape[0] * drawing.shape[1], 3))
clt = KMeans(n_clusters=3)
clt.fit(drawing)
hist = find_histogram(clt)
bar, color, percent = plot_colors2(hist, clt.cluster_centers_)
val1 = hist[0]
val2 = hist[1]
val3 = hist[2]
a1 = min(val1, val2, val3)
a3 = max(val1, val2, val3)
a2 = (val1 + val2 + val3) - a1 - a3
ranks = ([a1, a2, a3])
tempo = ranks[0]
valence = ranks[1]
dance = ranks[2]
print(dance)
upperDance = dance + 0.002
lowerDance = dance - 0.002
upperValence = valence + 0.002
lowerValence = valence - 0.002
upperTempo = (tempo * 1000) + 10
lowerTempo = (tempo * 1000) - 10
data_path = Path('.') / 'Data'
data_files = list(data_path.glob("*.xlsx"))
file = data_files[0]
df = pd.read_excel(str(file))
df_dance = df[(df.danceability >= lowerDance) & (df.danceability <= upperDance)]
finalSong_df = df_dance[0:1]
song_title = finalSong_df['title']
artist = finalSong_df['artist']
danceDataVal = finalSong_df['danceability']
print(song_title, artist, danceDataVal)
plt.axis("off")
plt.imshow(bar)
plt.show()
break
else:
if colorIndx == 0:
redPts[redIndx].appendleft(center)
elif colorIndx == 1:
orangePts[orangeIndx].appendleft(center)
elif colorIndx == 2:
yellowPts[yellowIndx].appendleft(center)
elif colorIndx == 3:
greenPts[greenIndx].appendleft(center)
elif colorIndx == 4:
bluePts[blueIndx].appendleft(center)
elif colorIndx == 5:
purplePts[purpleIndx].appendleft(center)
else:
redPts.append(deque(maxlen=1000))
redIndx += 1
orangePts.append(deque(maxlen=1000))
orangeIndx += 1
yellowPts.append(deque(maxlen=1000))
yellowIndx += 1
greenPts.append(deque(maxlen=1000))
greenIndx += 1
bluePts.append(deque(maxlen=1000))
blueIndx += 1
purplePts.append(deque(maxlen=1000))
purpleIndx += 1
cv.imshow("Frame", frame)
k = cv.waitKey(5) & 0xff
if k == 27:
break
cv.destroyAllWindows()