-
Notifications
You must be signed in to change notification settings - Fork 13
/
psychicCCTV.py
227 lines (187 loc) · 7.35 KB
/
psychicCCTV.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
import numpy as np
import imutils
import time
import cv2
import os
from pathlib import Path
import PySimpleGUIQt as sg
import gallery
import media_player
import copy
import moviepy.editor as mp
from spleeter.separator import Separator
import copy
i_vid = 'Enter path to input video'
o_vid = 'Enter path to output video'
o_sound = 'Enter path to output'
yoloModelPath = Path().parent.absolute()
yoloModelPath = str(yoloModelPath) + "/yoloModel/"
sg.ChangeLookAndFeel('LightGrey')
layout1 = [
[sg.Text('Perform YOLO Object Detection', size=(50,1), font=('Any',18),text_color='#1c86ee' ,justification='left')],
[sg.Text('Path to input video'), sg.In(i_vid,size=(40,1), key='input'), sg.FileBrowse(size=(75, 30))],
[sg.Text('Path to output video'), sg.In(o_vid,size=(40,1), key='output'), sg.FileSaveAs(size=(75, 30))],
[sg.Text('Confidence'), sg.Slider(range=(0,10),orientation='h', resolution=1, default_value=5, size=(15,15), key='confidence'), sg.T(' ', key='_CONF_OUT_')],
[sg.Text('Threshold'), sg.Slider(range=(0,10), orientation='h', resolution=1, default_value=3, size=(15,15), key='threshold'), sg.T(' ', key='_THRESH_OUT_')],
[sg.Text(' '*8), sg.Checkbox('Write output video to disk', key='_DISK_')],
[sg.OK(size=(100, 30)), sg.Stretch()],
]
layout2 = [[sg.Text('Extract Audio from different sources', size=(50,1), font=('Any',18),text_color='#1c86ee' ,justification='left')],
[sg.Text('Path to input video'), sg.In(i_vid,size=(40,1), key='inputSound'), sg.FileBrowse(size=(75, 30))],
[sg.Text('Path to output sound tracks'), sg.In(o_sound,size=(40,1), key='outputSound'), sg.FileSaveAs(size=(75, 30))],
[sg.Button('Extract Sound', size=(100, 30))]]
layout = [[sg.Column(layout1, key='-COLYOLO-'), sg.Column(layout2, visible=False, key='-COLSound-')],
[sg.Frame(layout=[[sg.Button('YOLO', size=(50, 30)),
sg.Button('Sound', size=(60, 30)),
sg.Button('YOLO Saved Frames', size=(200, 30)),
sg.Button('Exit', size=(50, 30))],
], title='Options', title_color='red', relief=sg.RELIEF_SUNKEN)
]]
win = sg.Window('Psychic CCTV',
default_element_size=(21,1),
text_justification='right',
auto_size_text=False).Layout(layout)
layoutVis = 'YOLO'
while True:
event, values = win.Read()
if event in 'YOLO Sound':
win[f'-COL{layoutVis}-'].update(visible=False)
layoutVis = event
win[f'-COL{layoutVis}-'].update(visible=True)
if event == 'YOLO Saved Frames':
win.Close()
gallery.displayImages()
if event is None or event =='Exit':
exit()
if event == 'Extract Sound':
print("Sed Life")
# Add the spleeter thing here
# Yeah done
clip = mp.VideoFileClip(values["inputSound"])
outputs = os.getcwd() + '/inference/' +'sounds/'
print(outputs)
clip.audio.write_audiofile(r"sound.mp3")
separator = Separator('spleeter:5stems')
sounds_file = os.getcwd() + '/sound.mp3'
print(sounds_file)
separator.separate_to_file(sounds_file, 'output')
media_player.MediaPlayerGUI()
if event == 'OK':
write_to_disk = values['_DISK_']
args = values
win.Close()
gui_confidence = args["confidence"]/10
gui_threshold = args["threshold"]/10
labelsPath = os.path.sep.join([yoloModelPath, "model.names"])
LABELS = open(labelsPath).read().strip().split("\n")
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),
dtype="uint8")
weightsPath = os.path.sep.join([yoloModelPath, "yolov3.weights"])
configPath = os.path.sep.join([yoloModelPath, "yolov3.cfg"])
print("[INFO] loading YOLO from disk...")
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
vs = cv2.VideoCapture(args["input"])
writer = None
(W, H) = (None, None)
try:
prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() \
else cv2.CAP_PROP_FRAME_COUNT
total = int(vs.get(prop))
print("[INFO] {} total frames in video".format(total))
except:
print("[INFO] could not determine # of frames in video")
print("[INFO] no approx. completion time can be provided")
total = -1
win_started = False
Frame_number = 0
while True:
grabbed, frame = vs.read()
format_frame = [copy.deepcopy(frame),copy.deepcopy(frame)]
if not grabbed:
break
if W is None or H is None:
(H, W) = format_frame[0].shape[:2]
blob = cv2.dnn.blobFromImage(format_frame[0], 1 / 255.0, (416, 416),
swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)
end = time.time()
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > gui_confidence:
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, gui_confidence, gui_threshold)
if len(idxs) > 0:
count = 0
for i in idxs.flatten():
format_frame[1] = copy.deepcopy(frame)
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
color = [int(c) for c in COLORS[classIDs[i]]]
for g in range(len(format_frame)):
cv2.rectangle(format_frame[g], (x, y), (x + w, y + h), color, 2)
text = "{}: {:.4f}".format(LABELS[classIDs[i]],
confidences[i])
cv2.putText(format_frame[g], text, (x, y - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
outputer = os.getcwd() + '/inference/Objects/' + LABELS[classIDs[i]] \
+ str(Frame_number) +'_' + str(count) + '.jpg'
cv2.imwrite(outputer,format_frame[1])
count += 1
if write_to_disk:
if writer is None:
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
writer = cv2.VideoWriter(args["output"], fourcc, 30,
(format_frame[0].shape[1], format_frame[0].shape[0]), True)
if total > 0:
elap = (end - start)
print("[INFO] single frame took {:.4f} seconds".format(elap))
print("[INFO] estimated total time to finish: {:.4f}".format(
elap * total))
writer.write(format_frame[0])
imgbytes = cv2.imencode('.png', format_frame[0])[1].tobytes()
if not win_started:
win_started = True
layout = [
[sg.Text('Labelled Video', size=(30,1))],
[sg.Image(data=imgbytes, key='_IMAGE_')],
[sg.Text('Confidence'),
sg.Slider(range=(0, 10), orientation='h', resolution=1, default_value=5, size=(15, 15), key='confidence'),
sg.Text('Threshold'),
sg.Slider(range=(0, 10), orientation='h', resolution=1, default_value=3, size=(15, 15), key='threshold')],
[sg.Exit(size=(50, 30))]
]
win = sg.Window('Object Detection Output',
default_element_size=(14, 1),
text_justification='right',
auto_size_text=False).Layout(layout).Finalize()
image_elem = win.FindElement('_IMAGE_')
else:
image_elem.Update(data=imgbytes)
event, values = win.Read(timeout=0)
if event is None or event == 'Exit':
break
gui_confidence = values['confidence']/10
gui_threshold = values['threshold']/10
print(Frame_number)
Frame_number += 1
win.Close()
print("[INFO] cleaning up...")
writer.release() if writer is not None else None
vs.release()