-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
485 lines (400 loc) · 18.2 KB
/
main.py
File metadata and controls
485 lines (400 loc) · 18.2 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# FindFrame
# Author: Gunbard
from posixpath import join
import cv2, json, math, os, subprocess, asyncio, qasync, sys
from datetime import datetime
from enum import Enum
from mainWindow import Ui_MainWindow
from resultsWindow import Ui_ResultsWindow
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QLabel, QTableWidgetItem
from PyQt5.QtWinExtras import QWinTaskbarButton
APP_TITLE = 'FindFrame'
VERSION = '1.1.0'
WINDOW_TITLE = "{} {}".format(APP_TITLE, VERSION)
MATCH_FILTER_THRESHOLD = 0.9 # Discard 10% of possible outlier matches
MULTI_FILE_DELMITER = ';'
# Default number of features to detect in a image or video frame. Default is usually 500, but 1000 provides more
# usable keypoints on larger images. Increasing this will add processing time.
ORB_NFEATURES = 1000
# Increase to allow processing more files at once, but progress bars and thumbnails will bug out until the UI is
# updated in a way that makes sense
MAX_BATCH_SIZE = 1
# Columns in the Results table
class ResultsColumns(Enum):
FILENAME = 0
TIMESTAMP = 1
CONFIDENCE = 2
THUMBNAIL = 3
match_threshold = 30 # Default percent of matching descriptors
file_list = [] # List of video files to process
last_fps_check = datetime.now()
fps_count = 0
processed_frame = None
def millisToTime(ms):
'''
Helper to convert milliseconds to a timestamp string
Parameters:
ms (int): Milliseconds to convert
Returns:
string: Ex. "05:30:23"
'''
x = ms / 1000
seconds = round(x % 60)
x /= 60
minutes = math.floor(x % 60)
x /= 60
hours = math.floor(x % 24)
return "{}:{}:{}".format(str(hours).zfill(2), str(minutes).zfill(2), str(seconds).zfill(2))
def open_image_path():
'''
Opens a file chooser dialog to select the source image
'''
working_dir = ui.fieldInputImage.text()
if len(working_dir) == 0 or not os.path.exists(working_dir):
working_dir = os.getcwd()
path = QtWidgets.QFileDialog.getOpenFileName(None, "Select image", working_dir, \
"Images (*.png *.jpg);;idgaf (*.*)")
if not path[0]:
print("No image selected!")
return
ui.fieldInputImage.setText(os.path.normpath(path[0]))
analyze_image(path[0])
def open_video_path():
'''
Opens a multi-file chooser dialog to select video(s) to scan
'''
working_dir = ui.fieldVideo.text()
if len(working_dir) == 0:
working_dir = os.getcwd()
else:
# If multiple files, just try getting the default dir of the first one
working_dirs = working_dir.split(MULTI_FILE_DELMITER)
working_dir = working_dirs[0]
if not os.path.exists(working_dir):
working_dir = os.getcwd()
paths = QtWidgets.QFileDialog.getOpenFileNames(None, "Select one or more video files", working_dir, \
"Videos (*.mp4 *.mkv *.webm);;idgaf (*.*)")
if not paths[0]:
print("No video(s) selected!")
return
normalized_paths = map(lambda item: os.path.normpath(item), paths[0])
ui.fieldVideo.setText(MULTI_FILE_DELMITER.join(normalized_paths))
def open_viewer(event):
if not processed_frame:
return
image = QPixmap(processed_frame)
newLabel = QtWidgets.QLabel()
newLabel.setPixmap(image)
newLabel.setMinimumSize(1, 1)
def resized(newSize):
newLabel.setPixmap(image.scaled(newSize.size().width(), newSize.size().height(), \
QtCore.Qt.KeepAspectRatio, \
QtCore.Qt.SmoothTransformation))
newDialog = QtWidgets.QDialog()
newDialog.setWindowTitle("Image Viewer")
newDialog.resizeEvent = resized
newDialog.setMaximumSize(app.primaryScreen().availableSize().width(), \
app.primaryScreen().availableSize().height())
dialogLayout = QtWidgets.QGridLayout()
dialogLayout.setContentsMargins(0, 0, 0, 0)
dialogLayout.addWidget(newLabel, 0, 0, QtCore.Qt.AlignCenter)
newDialog.setLayout(dialogLayout)
newDialog.exec()
def analyze_image(image):
'''
Generates a thumbnail with detected keypoints drawn on the source image
Parameters:
image (string): Path to an image file
'''
global processed_frame
parsed_image = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
orb = cv2.ORB_create(nfeatures=ORB_NFEATURES)
keypoints, descriptors = orb.detectAndCompute(parsed_image, None)
img = cv2.drawKeypoints(parsed_image, keypoints, None, color=(255,0,0), flags=0)
height, width, channel = img.shape
bytesPerLine = 3 * width
qImg = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888)
processed_frame = qImg
aspectFitPixmap = QPixmap(qImg).scaled(ui.imageInput.width(), \
ui.imageInput.height(), \
QtCore.Qt.KeepAspectRatio, \
QtCore.Qt.SmoothTransformation)
ui.imageInput.setPixmap(aspectFitPixmap)
async def scan_video(index, semaphore):
'''
Opens a video to process frames. Adds match frames to the results table in the Results window.
Parameters:
index (int): Index of the video to scan in file_list
semaphore (asyncio.Semaphore): Used for async processing
'''
async with semaphore:
global file_list
path = file_list[index]
log('Starting processing {}...'.format(os.path.basename(path)))
set_processing_mode(True)
video = cv2.VideoCapture(path)
if not video.isOpened():
log("Failed to open {}".format(os.path.basename(path)))
return
ui.labelFileProgress.setText('File: {}'.format(os.path.basename(path)))
ui.progressBarFiles.setValue(index + 1)
# Get frame count
total_frames = video.get(cv2.CAP_PROP_FRAME_COUNT)
log("Total frames: {}".format(int(total_frames)))
ui.progressBar.setTextVisible(True)
ui.progressBar.setValue(0)
ui.progressBar.setRange(0, int(total_frames))
taskbarProgress.setRange(ui.progressBar.minimum(), ui.progressBar.maximum())
taskbarProgress.setValue(ui.progressBar.value())
frameWidth = ui.imageVideoFrame.width()
frameHeight = ui.imageVideoFrame.height()
# Only need to get source image and generate descriptors once
source_frame = cv2.imread(ui.fieldInputImage.text(), cv2.IMREAD_GRAYSCALE)
log('Generating source image descriptors...')
orb = cv2.ORB_create(nfeatures=ORB_NFEATURES)
keypoints, descriptors = await loop.run_in_executor(None, orb.detectAndCompute, source_frame, None)
# Configure flanfly.png matcher
index_params = dict(algorithm = 6, # FLANN_INDEX_LSH
table_number = 6, # 12
key_size = 12, # 20
multi_probe_level = 1) # 2
search_params = dict()
matcher = cv2.FlannBasedMatcher(index_params, search_params)
#log('Descriptor count for source frame: {}'.format(len(descriptors)))
log('Beginning match search...')
ui.progressMatch.setRange(0, len(descriptors))
ui.progressMatchPeak.setRange(0, len(descriptors))
candidate_frames = set() # Using a Set to remove duplicate frames (granularity of one second)
boost_contrast = ui.checkBoostContrast.isChecked()
while video.isOpened():
result, timestamp, bad_frame, matches = await loop.run_in_executor(None, process_frame, \
frameWidth, frameHeight, \
matcher, descriptors, video, boost_contrast)
if bad_frame:
progress = ui.progressBar.value()
ui.progressBar.setValue(progress + 1)
taskbarProgress.setValue(ui.progressBar.value())
elif result:
# Update FPS label
global fps_count, last_fps_check
if (datetime.now() - last_fps_check).seconds >= 1:
ui.labelFPS.setText('FPS: {}'.format(fps_count))
fps_count = 0
last_fps_check = datetime.now()
else:
fps_count += 1
ui.progressMatch.setValue(matches)
if ui.progressMatchPeak.value() < matches:
ui.progressMatchPeak.setValue(matches)
ui.imageVideoFrame.setPixmap(result)
progress = ui.progressBar.value()
ui.progressBar.setValue(progress + 1)
taskbarProgress.setValue(ui.progressBar.value())
if timestamp > -1:
converted_timestamp = millisToTime(timestamp)
candidate_frames_prev_size = len(candidate_frames)
candidate_frames.add(converted_timestamp)
# New timestamp, so include in results table
if len(candidate_frames) > candidate_frames_prev_size:
results_ui.resultsTable.setRowCount(results_ui.resultsTable.rowCount() + 1)
filename_item = QTableWidgetItem(os.path.basename(path))
filename_item.setTextAlignment(QtCore.Qt.AlignCenter)
thumbnail_label = QLabel()
thumbnail_label.setAlignment(QtCore.Qt.AlignCenter)
thumbnail_label.setPixmap(result)
timestamp_item = QTableWidgetItem(converted_timestamp)
timestamp_item.setTextAlignment(QtCore.Qt.AlignCenter)
confidence_item = QTableWidgetItem('{:.1f}% ({}/{})'.format(((matches/len(descriptors)) * 100), \
matches, len(descriptors)))
confidence_item.setTextAlignment(QtCore.Qt.AlignCenter)
results_ui.resultsTable.setItem(results_ui.resultsTable.rowCount() - 1, \
ResultsColumns.FILENAME.value, filename_item)
results_ui.resultsTable.setItem(results_ui.resultsTable.rowCount() - 1, \
ResultsColumns.TIMESTAMP.value, timestamp_item)
results_ui.resultsTable.setItem(results_ui.resultsTable.rowCount() - 1, \
ResultsColumns.CONFIDENCE.value, confidence_item)
results_ui.resultsTable.setCellWidget(results_ui.resultsTable.rowCount() - 1, \
ResultsColumns.THUMBNAIL.value, thumbnail_label)
ResultsWindow.setWindowTitle('{} - Results ({})' \
.format(APP_TITLE, results_ui.resultsTable.rowCount()))
log('Possible match at {}'.format(converted_timestamp))
else:
set_processing_mode(False)
log('Processing complete.')
if len(candidate_frames) > 0:
candidate_frames = sorted(candidate_frames)
log('Found potential matches at: {}'.format(list(candidate_frames)))
if not ResultsWindow.isVisible():
ResultsWindow.show()
else:
log('Did not find any matches!')
ui.progressBar.setValue(ui.progressBar.maximum())
taskbarProgress.setValue(ui.progressBar.value())
break
MainWindow.setWindowTitle('{} ({})'.format(ui.progressBar.text(), ui.progressBarFiles.text()))
def processing_complete(status):
print(status)
def process_frame(scaledWidth, scaledHeight, matcher, source_descriptors, video, boost_contrast):
'''
Grabs a frame, compares it to the source image, and if it's within the match threshold, return it as a good match.
Parameters:
scaledWidth (int): Thumbnail width for aspect ratio aware scaling
scaledHeight (int): Thumbnail height for aspect ratio aware scaling
matcher (cv2 matcher): Magical OpenCV matcher to use for matching
source_descriptors (array?): OpenCV descriptors generated via ORB thingy
video (OpenCV VideoCapture): OpenCV handle to the video file
boost_contrast (bool): Whether or not to increase the video frame's contrast to help with matching
Returns:
tuple: (
result: scaled thumbnail of the processed frame,
timestamp: timestamp of the processed frame in milliseconds or -1,
bad_frame: whether or not the frame was bad or corrupt,
match count in the frame
)
'''
#video.set(cv2.CAP_PROP_POS_FRAMES, 10) # Seek to frame 10
timestamp = -1
bad_frame = (None, timestamp, True, 0)
success, frame = video.read()
if not success:
# Done with video, probably
return None, timestamp, False, 0
if not frame.any():
print('Bad frame')
return bad_frame
frame_number = video.get(cv2.CAP_PROP_POS_FRAMES)
#rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Increase contrast
if boost_contrast:
contrast = 4.0
brightness = 0
brightness += int(round(255 * (1 - contrast) / 2))
frame = cv2.addWeighted(frame, contrast, frame, 0, brightness)
image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
height, width = image.shape
if height <= 0 or width <= 0:
# Discard bad frame
print('Bad frame')
return bad_frame
# Generate frame desciptors
orb = cv2.ORB_create(nfeatures=ORB_NFEATURES)
keypoints, descriptors = orb.detectAndCompute(image, None)
if descriptors is None: # This is absolutely idiotic syntax
# Discard frame with no descriptors (this can be blank or corrupt frames)
return bad_frame
try:
matches = matcher.knnMatch(source_descriptors, descriptors, k=2)
except:
return bad_frame
good_matches = []
for match in matches:
# A match object has two points, but can have just one for some reason
if len(match) < 2:
continue
m, n = match
if m.distance < MATCH_FILTER_THRESHOLD * n.distance:
good_matches.append(m)
print('Matches for frame {}: {}'.format(int(frame_number), len(good_matches)))
if len(good_matches) > (len(source_descriptors) * (match_threshold / 100)):
# Possible match, so get timestamp
timestamp = video.get(cv2.CAP_PROP_POS_MSEC)
qImg = QImage(image.data, width, height, width, QImage.Format_Grayscale8)
return QPixmap(qImg).scaled(scaledWidth, \
scaledHeight, \
QtCore.Qt.KeepAspectRatio, \
QtCore.Qt.FastTransformation), \
timestamp, \
False, \
len(good_matches)
def start_processing():
'''
Creates the asyncio tasks for processing the files in the file list or cancels the existing tasks
'''
if not ui.fieldInputImage.text() or not ui.fieldVideo.text():
log('<span style="color:red;">Error: Must have both a source image and target video!</span>')
return
if ui.btnStartScan.text() == "Cancel":
pending_tasks = asyncio.all_tasks(loop)
for task in pending_tasks:
task.cancel()
log('Cancelled!')
set_processing_mode(False)
else:
# Clear out results table
results_ui.resultsTable.setRowCount(0)
ResultsWindow.setWindowTitle('{} - Results'.format(APP_TITLE))
global file_list
file_list = ui.fieldVideo.text().split(';')
ui.progressBarFiles.setValue(0)
ui.progressBarFiles.setRange(0, len(file_list))
for index, item in enumerate(file_list):
task = asyncio.ensure_future(scan_video(index, asyncio_semaphore))
task.add_done_callback(processing_complete)
def set_processing_mode(processing):
'''
Updates the UI based on whether or not the app is processing
Parameters:
processing (bool): Whether or not the app is processing
'''
if processing:
ui.btnStartScan.setText("Cancel")
taskbarProgress.resume()
else:
ui.btnStartScan.setText("Scan")
ui.labelFileProgress.setText('')
ui.labelFPS.setText('')
taskbarProgress.stop()
MainWindow.setWindowTitle(WINDOW_TITLE)
ui.progressBar.setTextVisible(processing)
ui.progressBarFiles.setTextVisible(processing)
ui.btnOpenInputImage.setEnabled(not processing)
ui.btnOpenVideo.setEnabled(not processing)
ui.sliderMatchThresh.setEnabled(not processing)
ui.checkBoostContrast.setEnabled(not processing)
def match_thresh_changed():
'''
Handler for when the match threshold slider changes. Updates its label.
'''
global match_threshold
match_threshold = ui.sliderMatchThresh.value()
ui.labelMatchThresh.setText("{}%".format(match_threshold))
def log(message):
'''
Helper for adding a timestamp to the front-facing logger
'''
ui.textLog.append("[{}] {}".format(datetime.now().strftime("%H:%M:%S.%f"), message))
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_DisableWindowContextHelpButton)
app = QtWidgets.QApplication(sys.argv)
loop = qasync.QEventLoop(app)
asyncio.set_event_loop(loop)
asyncio.events._set_running_loop(loop)
asyncio_semaphore = asyncio.Semaphore(MAX_BATCH_SIZE)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
ResultsWindow = QtWidgets.QDialog(MainWindow)
results_ui = Ui_ResultsWindow()
results_ui.setupUi(ResultsWindow)
results_ui.resultsTable.setHorizontalHeaderLabels(['File', 'Timestamp', 'Confidence', 'Thumbnail'])
# Defaults
ui.sliderMatchThresh.setSliderPosition(match_threshold)
ui.labelMatchThresh.setText("{}%".format(match_threshold))
# EVENTS
ui.btnOpenInputImage.clicked.connect(open_image_path)
ui.btnOpenVideo.clicked.connect(open_video_path)
ui.btnStartScan.clicked.connect(start_processing)
ui.btnResults.clicked.connect(lambda: ResultsWindow.show())
ui.sliderMatchThresh.valueChanged.connect(match_thresh_changed)
ui.imageInput.mousePressEvent = open_viewer
MainWindow.setWindowTitle(WINDOW_TITLE)
MainWindow.show()
taskbarButton = QWinTaskbarButton()
taskbarProgress = taskbarButton.progress()
taskbarProgress.setRange(0, 100)
taskbarProgress.show()
taskbarButton.setWindow(MainWindow.windowHandle())
with loop:
loop.run_forever()