-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobserver.py
More file actions
774 lines (690 loc) · 32.2 KB
/
observer.py
File metadata and controls
774 lines (690 loc) · 32.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
import cv2
import sys
import numpy as np
import os
import tensorflow.compat.v1 as tf
import math
import time
import json
sys.path.append("C:\\Users\\notebook\\Documents\\GitHub\\models\\research\\")
sys.path.append("C:\\Users\\notebook\\Documents\\GitHub\\models\\research\\object_detection\\utils")
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QListWidget, QMessageBox
from PyQt5.QtCore import QThread, Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QImage, QPixmap
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
global started
global fileCreated
started = False
global ex
class AnkiCar:
def __init__(self, img, bounding_box, name=""):
self.tracker = cv2.TrackerCSRT_create()
self.tracker.init(img, tuple(bounding_box))
self.name = name
self.lastxPos = float(-1)
self.lastyPos = float(-1)
self.xPos = float(-1)
self.yPos = float(-1)
self.updateTime = [float(0),float(0)]
self.unit = float(-1)
self.laps = float(-1) # assuming car starts at the finish line
self.lapCount = -1
self.ratio = float(0)
self.lapStart = [] # Time when car starts a lap
self.lapTime = [] # if not empty, show LapTime[-1]
self.speed = [] # if not empty, show speed[-1]
# split time cannot be defined in self class - need info from another AnkiCar
def update_rect(self, img):
self.lastxPos = self.xPos
self.lastyPos = self.yPos
self.unit
is_success, bounding_box = self.tracker.update(img)
if is_success:
x, y, w, h = (int (n) for n in bounding_box)
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255,0), 3)
self.xPos = x + (w/2)
self.yPos = y + (h/2) # midpoint of a car
self.updateTime.pop()
self.updateTime.insert(0,time.time())
if self.name:
cv2.putText(img, self.name, (x,y), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0))
class FinishLine:
def __init__(self, img, bounding_box):
self.tracker = cv2.TrackerCSRT_create()
self.tracker.init(img, tuple(bounding_box))
self.vertical = True
self.xPos = float(-1)
self.yPos = float(-1)
self.trackWidth = float(-1)
self.unit = float(-1)
def update_rect(self, img):
is_success, bounding_box = self.tracker.update(img)
if is_success:
x, y, w, h = (int (n) for n in bounding_box)
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255,0), 3)
if (w > h):
self.vertical = False
self.yPos = y + (h/2)
self.xPos = x
self.trackWidth = w
self.unit = 210/w ## in millimeters(mm)
else:
self.vertical = True
self.xPos = x + (w/2)
self.yPos = y
self.trackWidth = h
self.unit = 210/h ## in millimeters(mm)
class DataHandler:
def __init__(self, anki_cars, finishLine, list_widgets):
self.carList = anki_cars
self.finishLine = finishLine
self.ranking = []
self.currentSpeed = []
self.avgSpeed = []
self.fastestSpeed = []
self.laps = []
self.currentLapTime = []
self.avgLapTime = []
self.fastestLapTime = []
self.splitTime = []
self.dataBoxes = list_widgets
def calculateRank(self, anki_cars):
if (len(anki_cars[0].speed) > 0):
cars = anki_cars
sortSpeed = sorted(cars, key = lambda x : x.speed[-1], reverse = True)
self.ranking = sorted(sortSpeed, key = lambda x : x.laps, reverse = True)
def calculateSpeed(self, anki_cars):
for car in anki_cars:
if ((car.lastxPos != float(-1)) and (car.lastyPos != float(-1)) & (car.updateTime[1] != float(0))):
xDiff = abs(car.xPos - car.lastxPos)
yDiff = abs(car.yPos - car.lastyPos)
speedPerSec = (math.sqrt(math.pow(xDiff,2) + math.pow(yDiff,2)))*car.unit/(car.updateTime[0] - car.updateTime[1])
car.speed.append(speedPerSec)
def calculateLaps(self, anki_cars, finishLine):
for car in anki_cars:
car.laps = car.laps - car.ratio
if (car.lastxPos != float(-1) and car.lastyPos != float(-1)):
if (finishLine.vertical):
if (car.xPos >= finishLine.xPos and car.lastxPos < finishLine.xPos and car.yPos >= finishLine.yPos and car.yPos <= (finishLine.yPos + finishLine.trackWidth)):
car.lapCount = car.lapCount + 1
car.lapStart.append(time.time())
car.laps = car.lapCount
if (len(car.lapTime) > 0):
currentTime = time.time()
car.ratio = (currentTime - car.lapStart[-1])/car.lapTime[-1]
car.laps = car.lapCount + car.ratio
if (car.laps > (car.lapCount + 1)):
car.laps = float(car.lapCount + 1 - 0.000000000000001)
else:
if (car.yPos >= finishLine.yPos and car.lastyPos < finishLine.yPos and car.xPos >= finishLine.xPos and car.xPos <= (finishLine.xPos + finishLine.trackWidth)):
car.lapCount = car.lapCount + 1
car.lapStart.append(time.time())
car.laps = car.lapCount
if (len(car.lapTime) > 0):
currentTime = time.time()
car.ratio = (currentTime - car.lapStart[-1])/car.lapTime[-1]
car.laps = car.lapCount + car.ratio
def calculateLapTime(self, anki_cars):
for car in anki_cars:
if ((len(car.lapStart)) >= 2):
startTime = car.lapStart[-2]
endTime = car.lapStart[-1]
oneLapTime = endTime - startTime
car.lapTime.append(oneLapTime)
def calculateSplitTime(self, anki_cars):
if (len(anki_cars) >= 2):
self.splitTime = []
self.splitTime.append(0.0)
startIndex = 1
lastIndex = len(anki_cars) - 1
while (startIndex <= lastIndex):
currentCar = anki_cars[startIndex]
carAhead = anki_cars[startIndex-1]
xDiff = abs(currentCar.xPos - carAhead.lastxPos)
yDiff = abs(currentCar.yPos - carAhead.lastyPos)
distance = float(0)
if (currentCar.lapCount > -1 and len(carAhead.speed) > 0):
if (currentCar.laps <= 1):
if ((carAhead.laps - currentCar.laps) > 0 and len(carAhead.lapTime) > 0):
distance = (carAhead.lapCount - currentCar.lapCount)*(carAhead.lapTime[-1]*carAhead.speed[-1]) + (math.sqrt(math.pow(xDiff,2) + math.pow(yDiff,2)))
else:
distance = (math.sqrt(math.pow(xDiff,2) + math.pow(yDiff,2)))
if (currentCar.speed[-1] > 0):
split = distance/currentCar.speed[-1]
self.splitTime.append(split)
else:
split = (carAhead.laps - currentCar.laps)*(currentCar.lapTime[-1])
self.splitTime.append(split)
startIndex = startIndex + 1
def calculateAvgSpeed(self, rank):
if (len(rank) > 0):
for car in rank:
totalSpeed = float(0)
oneAvgSpeed = float(0)
for s in car.speed:
totalSpeed = totalSpeed + s
if (len(car.speed) > 0):
oneAvgSpeed = totalSpeed/len(car.speed)
self.avgSpeed.append(oneAvgSpeed)
def calculateAvgLapTime(self, rank):
if (len(rank) > 0):
for car in rank:
if (len(car.lapTime) > 0):
totalLapTime = float(0)
oneAvgLapTime = float(0)
for l in car.lapTime:
totalLapTime = totalLapTime + l
if (len(car.lapTime) > 0):
oneAvgLapTime = totalLapTime/len(car.lapTime)
self.avgLapTime.append(oneAvgLapTime)
def calculateFastestSpeed(self, rank):
if (len(rank) > 0):
for car in rank:
fastSpeed = float(0)
for s in car.speed:
if (s > fastSpeed):
fastSpeed = s
self.fastestSpeed.append(fastSpeed)
def calculateFastestLapTime(self, rank):
if (len(rank) > 0):
for car in rank:
if (len(car.lapTime) > 0):
fastLapTime = car.lapTime[0]
for l in car.lapTime:
if (l < fastLapTime):
fastLapTime = l
self.fastestLapTime.append(fastLapTime)
def getCurrentSpeed(self, rank):
current = []
if (len(rank) > 0):
for car in rank:
speed = car.speed[-1]
current.append(speed)
self.currentSpeed = current
def getLaps(self, rank):
current = []
if (len(rank) > 0):
for car in rank:
laps = car.lapCount
current.append(laps)
self.laps = current
def getCurrentLapTime(self, rank):
current = []
if (len(rank) > 0):
for car in rank:
if (len(car.lapTime) > 0):
lapTime = car.lapTime[-1]
current.append(lapTime)
self.currentLapTime = current
def handle(self):
self.calculateSpeed(self.carList)
self.calculateLaps(self.carList, self.finishLine)
self.calculateLapTime(self.carList)
self.calculateRank(self.carList)
self.calculateSplitTime(self.ranking)
self.getCurrentSpeed(self.ranking)
self.getLaps(self.ranking)
self.getCurrentLapTime(self.ranking)
def display(self):
nrOfCars = len(self.ranking)
if (nrOfCars > 5):
nrOfCars = 5
counter = 0
while (counter < nrOfCars):
currentBox = self.dataBoxes[counter]
currentBox.takeItem(0)
currentBox.insertItem(0, "Name: " + self.ranking[counter].name)
currentBox.item(1).setText("Ranking: " + str(counter + 1))
currentBox.item(2).setText("Laps: " + str(self.laps[counter]))
if (len(self.currentLapTime) > counter):
currentBox.item(3).setText("Lap Time: " + str(self.currentLapTime[counter]))
currentBox.item(4).setText("Speed: " + str(self.currentSpeed[counter]))
if (len(self.splitTime) > counter):
currentBox.item(5).setText("Split Time: " + str(self.splitTime[counter]))
counter = counter + 1
def fileCreate(self):
msgBox = QMessageBox()
msgBox.setWindowTitle("File Create Confirmation")
msgBox.setText("The race data has been recorded and will be dumped into JSON format.")
msgBox.setInformativeText("Do you want to save the data?")
msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Cancel)
msgBox.setDefaultButton(QMessageBox.Save)
ret = msgBox.exec()
if (ret == QMessageBox.Save):
self.calculateAvgLapTime(self.ranking)
self.calculateAvgSpeed(self.ranking)
self.calculateFastestLapTime(self.ranking)
self.calculateFastestSpeed(self.ranking)
data = {}
data['anki_car'] = []
nrOfCars = len(self.ranking)
if (nrOfCars > 5):
nrOfCars = 5
counter = 0
while (counter < nrOfCars):
name = self.ranking[counter].name
ranking = counter + 1
laps = self.laps[counter]
if counter < len(self.avgLapTime):
avgLapTime = self.avgLapTime[counter]
else:
avgLapTime = "N/A"
if counter < len(self.fastestLapTime):
fastestLapTime = self.fastestLapTime[counter]
else:
fastestLapTime = "N/A"
avgSpeed = self.avgSpeed[counter]
fastestSpeed = self.fastestSpeed[counter]
if counter < len(self.splitTime):
split = self.splitTime[counter]
else:
split = "N/A"
data['anki_car'].append({
'name': name,
'ranking': ranking,
'laps': laps,
'avgLapTime': avgLapTime,
'fastestLapTime': fastestLapTime,
'avgSpeed': avgSpeed,
'fastestSpeed': fastestSpeed,
'splitTime': split
})
counter = counter + 1
fileName = time.strftime("%Y%m%d_%H_%M_%S.txt", time.localtime())
with open(fileName, 'w') as outfile:
json.dump(data, outfile)
PATH_TO_LABELS = r'C:\Users\notebook\Documents\GitHub\anki-race-observer\training\label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
MODEL_NAME = 'inference_graph'
PATH_TO_FROZEN_GRAPH = r'C:\Users\notebook\Documents\GitHub\anki-race-observer\training\frozen_inference_graph.pb'
TEST_IMAGE_DIR_PATH = r"C:\Users\notebook\Documents\GitHub\anki-race-observer\images"
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
def run_inference_for_single_image(image, graph, tensor_dict):
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(
tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [
real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [
real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
# Run inference
sess = tf.Session()
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
return output_dict
class Thread(QThread):
global started
global fileCreated
global ex
changePixmap = pyqtSignal(QImage)
def __init__(self, list_widgets, parent=None):
QThread.__init__(self, parent=parent)
self.list_widgets = list_widgets
self.filepath = None
def run(self):
global started
global fileCreated
try:
video = cv2.VideoCapture(self.filepath)
if not video.isOpened():
raise NameError("File Error")
ret, frame = video.read()
finishLine = None
anki_cars = []
with detection_graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
frame
frame_height, frame_width, channels = frame.shape
# Actual detection.
output_dict = run_inference_for_single_image(
frame, detection_graph, tensor_dict)
boxes = output_dict['detection_boxes']
# get all boxes from an array
max_boxes_to_draw = boxes.shape[0]
# get scores to get a threshold
scores = output_dict['detection_scores']
# this is set as a default for now
min_score_thresh=.5
# iterate through all boxes detected (there are alot of detection boxes most are not the ones we want)
for i in range(min(max_boxes_to_draw, boxes.shape[0])):
# box is not none and score is higher than 50% (these are the detections we want)
if scores is None or scores[i] > min_score_thresh:
# boxes[i] is the box which will be drawn
class_name = category_index[output_dict['detection_classes'][i]]['name']
# boxes[i] holds [ymin, xmin, ymax, ymin] they are floats ranging from 0 to 1
y1,x1,y2,x2 = boxes[i]
coordinates = (x1*frame_width, y1*frame_height, x2*frame_width, y2*frame_height)
coordinates = tuple((int (n) for n in coordinates))
nameRepeated = 1
if class_name == "Finish Line":
finish_line_coordinates = coordinates
fl_x1, fl_y1, fl_x2, fl_y2 = finish_line_coordinates
fl_w = fl_x2-fl_x1
fl_h = fl_y2-fl_y1
finishLine = FinishLine(frame, (fl_x1,fl_y1,fl_w,fl_h))
finishLine.update_rect(frame)
else:
x1,y1,x2,y2 = coordinates
w = x2-x1
h = y2-y1
car_name = class_name
for car in anki_cars:
if car_name == car.name:
nameRepeated = nameRepeated + 1
car_name = class_name + str(nameRepeated)
anki_cars.append(AnkiCar(frame, (x1,y1,w,h), car_name))
if (len(anki_cars) == 0):
raise NameError("No car detected.")
elif (finishLine == None):
raise NameError("No finish line detected.")
dataHandler = DataHandler(anki_cars, finishLine, self.list_widgets)
for anki_car in anki_cars:
anki_car.unit = finishLine.unit
fileCreated = False
while True:
ret, frame = video.read()
if ret:
if not started:
dataHandler.fileCreate()
fileCreated = True
self.quit()
break
for anki_car in anki_cars:
anki_car.update_rect(frame)
dataHandler.handle()
dataHandler.display()
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgbImage.shape
bytesPerLine = ch * w
convertToQtFormat = QImage(rgbImage.data, w, h, bytesPerLine, QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
else:
break
if not fileCreated:
dataHandler.fileCreate()
fileCreated = True
ex.fileCreate()
self.quit()
except cv2.error as e:
ex.cvError()
except Exception as e:
if str(e) == "No car detected.":
ex.noCar()
elif str(e) == "No finish line detected.":
ex.noFinishLine()
elif str(e) == "File Error":
ex.fileError()
else:
ex.unknownError()
class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
@pyqtSlot(QImage)
def setImage(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
self.label.setScaledContents(True)
def initUI(self):
self.setWindowTitle("ANKI Race Observer")
self.setObjectName("MainWindow")
sizeObject = QtWidgets.QDesktopWidget().screenGeometry(0)
self.resize(sizeObject.width()-20, sizeObject.height()-100)
self.setFocusPolicy(QtCore.Qt.NoFocus)
self.centralwidget = QtWidgets.QWidget(self)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.listWidget_1 = QListWidget(self.centralwidget)
self.listWidget_1.setObjectName("listWidget_1")
name1 = "Name: "
ranking1 = "Ranking: "
laps1 = "Laps: "
lapTime1 = "Lap Time: "
speed1 = "Speed: "
split1 = "Split Time: "
self.listWidget_1.addItem(name1)
self.listWidget_1.addItem(ranking1)
self.listWidget_1.addItem(laps1)
self.listWidget_1.addItem(lapTime1)
self.listWidget_1.addItem(speed1)
self.listWidget_1.addItem(split1)
self.horizontalLayout_2.addWidget(self.listWidget_1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem1)
self.listWidget_2 = QListWidget(self.centralwidget)
self.listWidget_2.setObjectName("listWidget_2")
name2 = "Name: "
ranking2 = "Ranking: "
laps2 = "Laps: "
lapTime2 = "Lap Time: "
speed2 = "Speed: "
split2 = "Split Time: "
self.listWidget_2.addItem(name2)
self.listWidget_2.addItem(ranking2)
self.listWidget_2.addItem(laps2)
self.listWidget_2.addItem(lapTime2)
self.listWidget_2.addItem(speed2)
self.listWidget_2.addItem(split2)
self.horizontalLayout_2.addWidget(self.listWidget_2)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem2)
self.listWidget_3 = QListWidget(self.centralwidget)
self.listWidget_3.setObjectName("listWidget_3")
name3 = "Name: "
ranking3 = "Ranking: "
laps3 = "Laps: "
lapTime3 = "Lap Time: "
speed3 = "Speed: "
split3 = "Split Time: "
self.listWidget_3.addItem(name3)
self.listWidget_3.addItem(ranking3)
self.listWidget_3.addItem(laps3)
self.listWidget_3.addItem(lapTime3)
self.listWidget_3.addItem(speed3)
self.listWidget_3.addItem(split3)
self.horizontalLayout_2.addWidget(self.listWidget_3)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem3)
self.listWidget_4 = QListWidget(self.centralwidget)
self.listWidget_4.setObjectName("listWidget_4")
name4 = "Name: "
ranking4 = "Ranking: "
laps4 = "Laps: "
lapTime4 = "Lap Time: "
speed4 = "Speed: "
split4 = "Split Time: "
self.listWidget_4.addItem(name4)
self.listWidget_4.addItem(ranking4)
self.listWidget_4.addItem(laps4)
self.listWidget_4.addItem(lapTime4)
self.listWidget_4.addItem(speed4)
self.listWidget_4.addItem(split4)
self.horizontalLayout_2.addWidget(self.listWidget_4)
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem4)
self.listWidget_5 = QListWidget(self.centralwidget)
self.listWidget_5.setObjectName("listWidget_5")
name5 = "Name: "
ranking5 = "Ranking: "
laps5 = "Laps: "
lapTime5 = "Lap Time: "
speed5 = "Speed: "
split5 = "Split Time: "
self.listWidget_5.addItem(name5)
self.listWidget_5.addItem(ranking5)
self.listWidget_5.addItem(laps5)
self.listWidget_5.addItem(lapTime5)
self.listWidget_5.addItem(speed5)
self.listWidget_5.addItem(split5)
self.horizontalLayout_2.addWidget(self.listWidget_5)
spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem5)
self.list_widgets = [self.listWidget_1, self.listWidget_2, self.listWidget_3, self.listWidget_4, self.listWidget_5]
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.horizontalLayout_3.addItem(spacerItem6)
self.label = QtWidgets.QLabel('Waiting for video input...')
self.label.setObjectName("label")
self.horizontalLayout_3.addWidget(self.label)
spacerItem7 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.horizontalLayout_3.addItem(spacerItem7)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalFrame = QtWidgets.QFrame(self.centralwidget)
self.horizontalFrame.setObjectName("horizontalFrame")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalFrame)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton("Start")
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setMaximumSize(QtCore.QSize(16777215, 50))
icon = QtGui.QIcon("play-circle.svg")
self.pushButton.setIcon(icon)
self.pushButton.setAutoDefault(False)
self.pushButton.setFlat(False)
self.pushButton.clicked.connect(self.displayFrame)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.horizontalFrame)
self.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(self)
self.t = Thread(self.list_widgets, parent=None)
self.t.changePixmap.connect(self.setImage)
self.show()
def displayFrame(self):
global started
if (started):
self.fileCreate()
else:
text, ok = QtWidgets.QInputDialog.getText(self, 'File Path', 'Enter the path to the video file:')
if ok and text:
self.t.filepath = str(text)
self.t.start()
self.pushButton.setFlat(False)
self.pushButton.setText("Stop")
icon = QtGui.QIcon("stop-circle.svg")
self.pushButton.setIcon(icon)
started = True
def fileCreate(self):
global started
self.pushButton.setFlat(False)
self.pushButton.setText("Start")
icon = QtGui.QIcon("play-circle.svg")
self.pushButton.setIcon(icon)
started = False
def fileError(self):
global started
msgBox = QMessageBox()
msgBox.setWindowTitle("Error")
msgBox.setText("This file cannot be opened by Open CV. Please check your file path.")
ret = msgBox.exec()
self.pushButton.setFlat(False)
self.pushButton.setText("Start")
icon = QtGui.QIcon("play-circle.svg")
self.pushButton.setIcon(icon)
started = False
def cvError(self):
global started
msgBox = QMessageBox()
msgBox.setWindowTitle("Open CV Error")
msgBox.setText("This file cannot be opened by Open CV. Please check your file type.")
ret = msgBox.exec()
self.pushButton.setFlat(False)
self.pushButton.setText("Start")
icon = QtGui.QIcon("play-circle.svg")
self.pushButton.setIcon(icon)
started = False
def noCar(self):
global started
msgBox = QMessageBox()
msgBox.setWindowTitle("No Car Detected")
msgBox.setText("No car is detected. Please make sure your video contains ANKI cars " +
"that can be detected.\n See the user manual for detectable ANKI car models.")
ret = msgBox.exec()
self.pushButton.setFlat(False)
self.pushButton.setText("Start")
icon = QtGui.QIcon("play-circle.svg")
self.pushButton.setIcon(icon)
started = False
def noFinishLine(self):
global started
msgBox = QMessageBox()
msgBox.setWindowTitle("No Finish Line Detected")
msgBox.setText("No finish line is detected. Please make sure your video contains an ANKI Overdrive " +
"Start/Finish road piece. ")
ret = msgBox.exec()
self.pushButton.setFlat(False)
self.pushButton.setText("Start")
icon = QtGui.QIcon("play-circle.svg")
self.pushButton.setIcon(icon)
started = False
def unknownError(self):
global started
msgBox = QMessageBox()
msgBox.setWindowTitle("Unknown Error")
msgBox.setText("An unknown error has occured. Please make sure you follow the user manual. ")
ret = msgBox.exec()
self.pushButton.setFlat(False)
self.pushButton.setText("Start")
icon = QtGui.QIcon("play-circle.svg")
self.pushButton.setIcon(icon)
started = False
if __name__ == '__main__':
global ex
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())