-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdetect_Main_Jeff.py
1467 lines (1211 loc) · 65.2 KB
/
detect_Main_Jeff.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
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Run inference with a YOLOv5 model on images, videos, directories, streams
Usage:
$ python path/to/detect.py --source path/to/img.jpg --weights yolov5s.pt --img 640
"""
import argparse
import sys
import time
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
FILE = Path(__file__).absolute()
sys.path.append(FILE.parents[0].as_posix()) # add yolov5/ to path
from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, check_imshow, colorstr, non_max_suppression, \
apply_classifier, scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path, save_one_box, get_bbox_area
from utils.plots import colors, plot_one_box
from utils.torch_utils import select_device, load_classifier, time_synchronized
import PIL
import tkinter
import numpy as np
# import os
# os.system(" python SignAlgo_DL_final.py")
import TrafficLightAlgo_v01 as LightAlgo
import SignAlgo_DL_final as SignAlgo_DL
import SignAlgo_ML_final as SignAlgo_ML
import gc
B_StopRun = False
class Parameters_Project:
link_Youtube = 'https://www.youtube.com/watch?v=nHUbCoPS-Xc&ab_channel=PinoSu' # Youtube
link_DroidCam = 'http://192.168.0.101:4747/video' # DroidCam
def __init__(self):
pass
class Parameters_YOLO():
def __init__(self, WaitTime = 10, save_img = False, save_crop = False, weights_path = 'yolov5x.pt',
source_path = 'D:/Machine Learning/工研院產業新尖兵/專題/yolo/yolov5/plate/images/valid',
Imgsz = 512, Conf_thres = 0.45, Iou_thres = 0.45, Max_det = 1000, save_txt = False, line_thickness = 3, hide_labels=False, hide_conf=False):
self.WaitTime = WaitTime
self.save_img = save_img
self.save_crop = save_crop
self.weights_path = weights_path
self.source_path = source_path
self.Imgsz = Imgsz
self.Conf_thres = Conf_thres
self.Iou_thres = Iou_thres
self.Max_det = Max_det
self.save_txt = save_txt
self.line_thickness = line_thickness
self.hide_labels = hide_labels
self.hide_conf = hide_conf
def Update_Parameters(self, e = None):
self.save_crop = True if str(root.getvar('chk_btnsavebboxImg')) == '1' else False
self.save_txt = True if str(root.getvar('chk_btnsavetxt')) == '1' else False
self.WaitTime = int(root.getvar('ent_WaitTime'))
self.Imgsz = int(root.getvar('ent_Imgsz'))
self.Conf_thres = float(root.getvar('ent_Conf_thres'))
self.Iou_thres = float(root.getvar('ent_Iou_thres'))
self.Max_det = int(root.getvar('ent_Max_det'))
self.line_thickness = int(root.getvar('ent_line_thickness'))
self.hide_labels = True if str(root.getvar('chk_hidelabels')) == '1' else False
self.hide_conf = True if str(root.getvar('chk_hideconf')) == '1' else False
class Parameters_Light(LightAlgo.AI_Algo):
def __init__(self, enable = True, ModelPath = 'D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\Light\Model/svm_model_3.sav'):
self.enable = enable
self.ModelPath = ModelPath
self.ai_Algo = LightAlgo.AI_Algo()
self.loadModel()
def Update_Parameters(self):
self.enable = True if root.getvar('chk_btnLightEnable') == '1' else False
def loadModel(self):
if self.enable:
try:
self.ai_Algo.loadModel(self.ModelPath)
except Exception as ex:
print(ex)
def predict(self, listImg: list, probability=True, dimension=(20, 20), BGR=False):
return self.ai_Algo.predict2(listImg, probability, dimension, BGR)
class Parameters_SignDL(SignAlgo_DL.AI_Algo):
def __init__(self, enable = True, ModelPath = 'D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\Sign\DL\h5\VGG19_combinedata_Kfold.h5'):
self.enable = enable
self.ModelPath = ModelPath
self.ai_Algo = SignAlgo_DL.AI_Algo()
self.loadModel()
def Update_Parameters(self):
self.enable = True if root.getvar('chk_btnSignDLEnable') == '1' else False
def loadModel(self):
if self.enable:
try:
self.ai_Algo.loadModel(self.ModelPath)
except Exception as ex:
print(ex)
def predict(self, listImg: list, probability=True, dimension=(32, 32), BGR=False):
return self.ai_Algo.predict2(listImg, dimension, BGR)
class Parameters_SignML(SignAlgo_ML.AI_Algo):
def __init__(self, enable = False, ModelPath = 'D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\Sign\ML\Model\RGB_Logist_Model.sav'):
self.enable = enable
self.ModelPath = ModelPath
self.ai_Algo = SignAlgo_ML.AI_Algo()
self.loadModel()
def Update_Parameters(self):
self.enable = True if root.getvar('chk_btnSignMLEnable') == '1' else False
self.loadModel()
def loadModel(self):
if self.enable:
try:
self.ai_Algo.loadModel(self.ModelPath)
except Exception as ex:
print(ex)
def predict(self, listImg: list, probability=True, dimension=(32, 32), BGR=False):
return self.ai_Algo.predict2(listImg, dimension, BGR)
ParametersProject = Parameters_Project()
# YOLO v5
ParametersYOLO = Parameters_YOLO()
# 呼叫分類演算法
ParametersLight = Parameters_Light()
ParametersSignDL = Parameters_SignDL()
ParametersSignML = Parameters_SignML()
@torch.no_grad()
def run(weights='yolov5s.pt', # model.pt path(s)
source='data/images', # file/dir/URL/glob, 0 for webcam
imgsz=640, # inference size (pixels)
conf_thres=0.25, # confidence threshold
iou_thres=0.45, # NMS IOU threshold
max_det=1000, # maximum detections per image
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
view_img=False, # show results
save_txt=False, # save results to *.txt
save_conf=False, # save confidences in --save-txt labels
save_crop=False, # save cropped prediction boxes
nosave=False, # do not save images/videos
classes=None, # filter by class: --class 0, or --class 0 2 3
agnostic_nms=False, # class-agnostic NMS
augment=False, # augmented inference
visualize=False, # visualize features
update=False, # update all models
project='runs/detect', # save results to project/name
name='exp', # save results to project/name
exist_ok=False, # existing project/name ok, do not increment
line_thickness=3, # bounding box thickness (pixels)
hide_labels=False, # hide labels
hide_conf=False, # hide confidences
half=False, # use FP16 half-precision inference
):
global ParametersYOLO, ParametersLight, B_StopRun # Jeff Revised!
# ParametersYOLO.save_img = not nosave and not source.endswith('.txt') # save inference images
webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
# Directories
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
# Initialize
set_logging()
device = select_device(device)
half &= device.type != 'cpu' # half precision only supported on CUDA
# Load model
model = attempt_load(weights, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check image size # Jeff Revised! 不加會有錯誤: RuntimeError: Sizes of tensors must match except in dimension 2. Got 43 and 44 (The offending index is 0)
names = model.module.names if hasattr(model, 'module') else model.names # get class names
if half:
model.half() # to FP16
# Second-stage classifier
classify = False
if classify:
modelc = load_classifier(name='resnet50', n=2) # initialize
modelc.load_state_dict(torch.load('resnet50.pt', map_location=device)['model']).to(device).eval()
# Dataloader
if webcam:
view_img = check_imshow()
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=imgsz, stride=stride)
bs = len(dataset) # batch_size
else:
dataset = LoadImages(source, img_size=imgsz, stride=stride)
bs = 1 # batch_size
vid_path, vid_writer = [None] * bs, [None] * bs
if view_img: # Jeff Revised!
# cv2.namedWindow('frame')
pass
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
t0 = time.time()
for path, img, im0s, vid_cap in dataset: # for each image
if B_StopRun: # Jeff Revised!
B_StopRun = False
dataset.StopRun()
break
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
t1 = time_synchronized()
pred = model(img,
augment=augment,
visualize=increment_path(save_dir / 'features', mkdir=True) if visualize else False)[0]
# Apply NMS
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
t2 = time_synchronized()
# Apply Classifier
if classify:
pred = apply_classifier(pred, modelc, img, im0s)
# Process detections
for i, det in enumerate(pred): # detections per image
if webcam: # batch_size >= 1
p, s, im0, frame = path[i], f'{i}: ', im0s[i].copy(), dataset.count
else:
p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)
p = Path(p) # to Path
save_path = str(save_dir / p.name) # img.jpg
txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # img.txt
s += '%gx%g ' % img.shape[2:] # print string
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
# imc = im0.copy() if save_crop else im0 # for save_crop
imc = im0.copy() # Jeff Revised!
# Jeff Revised!
resetDisplay()
ListInfo_Light = []
ListInfo_Sign = []
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
for *xyxy, conf, cls in reversed(det): # for each detected object
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
if ParametersYOLO.save_img or save_crop or view_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
plot_one_box(xyxy, im0, label=label, color=colors(c, True), line_thickness=line_thickness)
if save_crop:
save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
# Jeff Revised!
if ParametersLight.enable:
if str(names[c]) == 'light':
ListInfo_Light.append([save_one_box(xyxy, imc, BGR=True, save=False), get_bbox_area(xyxy), conf])
if ParametersSignDL.enable or ParametersSignML.enable:
if str(names[c]) == 'sign':
ListInfo_Sign.append([save_one_box(xyxy, imc, BGR=True, save=False), get_bbox_area(xyxy), conf])
# Print time (inference + NMS)
infoText = f'{s}Done. ({t2 - t1:.3f}s)'
print(infoText)
scrolledtext_info.insert(tk.INSERT, infoText + '\n\n')
#region GUI update + 分類演算法(紅綠燈+交通號誌) (Jeff Revised!)
# 分類演算法(紅綠燈)
if ParametersLight.enable:
t3 = time_synchronized()
if len(ListInfo_Light) > 0:
ListInfo_Light = sorted(ListInfo_Light, key = lambda s: s[1], reverse = True)
ListImage_orig_Light = []
for info in ListInfo_Light:
ListImage_orig_Light.append(info[0])
info_light = ParametersLight.predict(ListImage_orig_Light, probability=True, BGR=True)
ListImage_stan_Light = [ParametersLight.DictImage_stan_Light[info[0]] for info in info_light]
LightBlocks.updateDisplay(ListImage_orig_Light, ListImage_stan_Light, info_light)
t4 = time_synchronized()
infoText = f'Traffic Light Algo Done. ({t4 - t3:.3f}s)'
print(infoText)
scrolledtext_info.insert(tk.INSERT, infoText + '\n\n')
# 分類演算法(交通號誌)
if ParametersSignDL.enable or ParametersSignML.enable:
if ParametersSignDL.enable:
parameters_sign = ParametersSignDL
else:
parameters_sign = ParametersSignML
t3 = time_synchronized()
if len(ListInfo_Sign) > 0:
ListInfo_Sign = sorted(ListInfo_Sign, key = lambda s: s[1], reverse = True)
ListImage_orig_Sign = []
for info in ListInfo_Sign:
ListImage_orig_Sign.append(info[0])
info_sign = parameters_sign.predict(ListImage_orig_Sign, probability=True, BGR=True)
ListImage_stan_Sign = [ParametersSignDL.DictImage_stan_Sign[info[0]] for info in info_sign]
# Group
ListImage_orig_Sign_M = []
ListImage_orig_Sign_P= []
ListImage_orig_Sign_W = []
ListImage_stan_Sign_M = []
ListImage_stan_Sign_P = []
ListImage_stan_Sign_W = []
info_sign_M = []
info_sign_P = []
info_sign_W = []
for i in range(len(ListImage_orig_Sign)):
orig_img = ListImage_orig_Sign[i]
stan_img = ListImage_stan_Sign[i]
info_ = info_sign[i]
group = ParametersSignDL.Dict_name_2_Group[info_sign[i][0]]
if group == 'M':
ListImage_orig_Sign_M.append(orig_img)
ListImage_stan_Sign_M.append(stan_img)
info_sign_M.append(info_)
elif group == 'P':
ListImage_orig_Sign_P.append(orig_img)
ListImage_stan_Sign_P.append(stan_img)
info_sign_P.append(info_)
elif group == 'W':
ListImage_orig_Sign_W.append(orig_img)
ListImage_stan_Sign_W.append(stan_img)
info_sign_W.append(info_)
# SignBlocks1.updateDisplay(ListImage_orig_Sign, ListImage_stan_Sign, info_sign)
SignBlocks1.updateDisplay(ListImage_orig_Sign_M, ListImage_stan_Sign_M, info_sign_M)
SignBlocks2.updateDisplay(ListImage_orig_Sign_P, ListImage_stan_Sign_P, info_sign_P)
SignBlocks3.updateDisplay(ListImage_orig_Sign_W, ListImage_stan_Sign_W, info_sign_W)
t4 = time_synchronized()
infoText = f'Traffic Sign Algo Done. ({t4 - t3:.3f}s)'
print(infoText)
scrolledtext_info.insert(tk.INSERT, infoText + '\n\n')
#endregion
# Stream results
if view_img:
# cv2.imshow(str(p), im0)
# cv2.imshow('frame', im0)
# Use PIL (Pillow) to convert the NumPy ndarray to a PhotoImage
im0_resize = cv2.resize(im0, (1000, 800))
# Show FPS
t5 = time_synchronized()
fps = f'{(1.0 / (t5 - t1)):.1f} fps'
cv2.putText(im0_resize, fps, (850, 60), 0, 1, [0, 0, 255], thickness=2, lineType=cv2.LINE_AA)
global photo
photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(im0_resize[:, :, ::-1]))
# Add a PhotoImage to the Canvas
ca.create_image(0, 0, image=photo, anchor=tkinter.NW)
root.update()
cv2.waitKey(ParametersYOLO.WaitTime) # WaitTime millisecond # Jeff Revised!
#region Save results (image with detections)
if ParametersYOLO.save_img:
if dataset.mode == 'image':
cv2.imwrite(save_path, im0)
else: # 'video' or 'stream'
if vid_path[i] != save_path: # new video
vid_path[i] = save_path
if isinstance(vid_writer[i], cv2.VideoWriter):
vid_writer[i].release() # release previous video writer
if vid_cap: # video
fps = vid_cap.get(cv2.CAP_PROP_FPS)
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
else: # stream
fps, w, h = 30, im0.shape[1], im0.shape[0]
save_path += '.mp4'
vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
vid_writer[i].write(im0)
#endregion
if save_txt or ParametersYOLO.save_img:
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
infoText = f"Results saved to {save_dir}{s}"
print(infoText)
scrolledtext_info.insert(tk.INSERT, infoText + '\n\n')
if update:
strip_optimizer(weights) # update model (to fix SourceChangeWarning)
if view_img: # Jeff Revised!
# cv2.destroyAllWindows()
pass
infoText = f'Done. ({time.time() - t0:.3f}s)'
print(infoText)
scrolledtext_info.insert(tk.INSERT, infoText + '\n\n' + '-' * 50 + '\n\n')
def parse_opt():
# Jeff Revised!
global ParametersYOLO
# ParametersYOLO.weights_path = 'yolov5x.pt'
# ParametersYOLO.weights_path = r'D:\Machine Learning\工研院產業新尖兵\專題\yolo\yolov5\best.pt'
# ParametersYOLO.source_path = './data/testing_images'
# ParametersYOLO.source_path = 'D:\Machine Learning\工研院產業新尖兵\專題\Self_Driving_Cars\data\TrafficLight'
# ParametersYOLO.source_path = 'D:\Machine Learning\工研院產業新尖兵\專題\Self_Driving_Cars\data\TrafficLight Video'
# ParametersYOLO.source_path = 'D:\Machine Learning\工研院產業新尖兵\專題\Video'
# ParametersYOLO.source_path = r'D:\Machine Learning\工研院產業新尖兵\專題\yolo\yolov5\plate\images\valid'
# ParametersYOLO.source_path = 'D:/Machine Learning/工研院產業新尖兵/專題/yolo/yolov5/plate/images/valid'
# ParametersYOLO.source_path = 'http://192.168.0.101:4747/video' # DroidCam
# ParametersYOLO.source_path = 'https://www.youtube.com/watch?v=nHUbCoPS-Xc&ab_channel=PinoSu' # Youtube
# ParametersYOLO.save_img = True if root.getvar('chk_btnSaved') == 1 else False
ParametersYOLO.save_img = True if str(root.getvar('chk_btnSaved')) == '1' else False
ParametersYOLO.save_crop = True if str(root.getvar('chk_btnsavebboxImg')) == '1' else False
# print(root.getvar('chk_btnSaved'))
view_img = True
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str, default=ParametersYOLO.weights_path, help='model.pt path(s)') # Jeff Revised!
parser.add_argument('--source', type=str, default=ParametersYOLO.source_path, help='file/dir/URL/glob, 0 for webcam') # Jeff Revised!
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=ParametersYOLO.Imgsz, help='inference size (pixels)') # Jeff Revised!
parser.add_argument('--conf-thres', type=float, default=ParametersYOLO.Conf_thres, help='confidence threshold') # Jeff Revised!
parser.add_argument('--iou-thres', type=float, default=ParametersYOLO.Iou_thres, help='NMS IoU threshold')
parser.add_argument('--max-det', type=int, default=ParametersYOLO.Max_det, help='maximum detections per image')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
# parser.add_argument('--view-img', action='store_true', help='show results')
parser.add_argument('--view-img', type=bool, default=view_img, help='show results') # Jeff Revised!
parser.add_argument('--save-txt', type=bool, default=ParametersYOLO.save_txt, help='save results to *.txt')
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
# parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes') # Jeff Revised!
parser.add_argument('--save-crop', type=bool, default=ParametersYOLO.save_crop, help='save cropped prediction boxes') # Jeff Revised!
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--visualize', action='store_true', help='visualize features')
parser.add_argument('--update', action='store_true', help='update all models')
parser.add_argument('--project', default='runs/detect', help='save results to project/name')
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument('--line-thickness', type=int, default=ParametersYOLO.line_thickness, help='bounding box thickness (pixels)')
parser.add_argument('--hide-labels', type=bool, default=ParametersYOLO.hide_labels, help='hide labels')
parser.add_argument('--hide-conf', type=bool, default=ParametersYOLO.hide_conf, help='hide confidences')
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
opt = parser.parse_args()
return opt
def main(opt):
infoText = colorstr('detect: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())
print(infoText)
scrolledtext_info.insert(tk.INSERT, infoText + '\n\n\n')
check_requirements(exclude=('tensorboard', 'thop'))
run(**vars(opt))
#region GUI
# import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import easygui as gui
import glob
import os
root = Tk()
root.geometry('%dx%d+%d+%d' % (1650, 1000, 30, 5))
root.config(bg="#da0")
##root.config(bg="khaki")
# root.title("AI專題神人鯊鯊團")
root.title("自駕車影像辨識系統 (Image Identification for Self-Driving Cars)")
root.iconbitmap('GUI Image/鯊鯊4-2.ico')
#region Style Setting
s = ttk.Style()
s.configure('my.TButton', font=('Helvetica', 14,'bold'), foreground='green')
s.configure('my2.TButton', font=('Helvetica', 12), background='#7AC5CD', foreground='magenta')
s.configure('my3.TButton', font=('Helvetica', 14,'bold'), foreground='red')
s.configure('my.TLabel', font=('Arial', 18,'bold','italic'), background='blue', foreground='white', anchor = 'center')
s.configure('my2.TLabel', font=('Arial', 14,'bold','italic'), background='purple', foreground='yellow', anchor = 'center')
s.configure('my3.TLabel', font=('Arial', 14,'bold','italic'), background='gray', foreground='white', anchor = 'center')
s.configure('my4.TLabel', font=('Arial', 12,'bold','italic'), background='red', foreground='white', anchor = 'center')
s.configure('param.TLabel', font=('Arial', 14,'bold'), background='orange', foreground='white')
s.configure('param.TCheckbutton', font=('Arial', 14,'bold'), background='orange', foreground='white')
s.configure('param.TEntry', font=('Arial', 14,'bold'), background='white', foreground='#da0')
s.configure('TNotebook.Tab', font=('URW Gothic L','14','bold'))
s.configure('my.TRadiobutton', font=('Arial','10','bold'), foreground='green')
#endregion
source_dir_img = ''
source_dir_img2 = ''
source_dir_img3 = ''
def btn_Browse(e):
global ParametersYOLO
if e.widget is btn_brow_Model: # Model
# s = gui.fileopenbox("MSG","TITLE", default = "*.pt")
s = gui.fileopenbox("MSG","TITLE", default = "D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\YOLO\Model\*.pt")
if s is not None:
root.setvar('ent_Model', s)
ParametersYOLO.weights_path = s
elif e.widget is btn_brow_Test: # Test
s = filedialog.askdirectory(initialdir="D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\Test Image", title='Please select a directory')
if s != '':
root.setvar('ent_Test', s)
ParametersYOLO.source_path = s
elif e.widget is btn_brow_Image: # Image
global source_dir_img
s = filedialog.askdirectory(initialdir="D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\Test Image", title='Please select a directory')
if s != '':
root.setvar('ent_Image', s)
source_dir_img = s
list_imgName = []
# for filename in glob.glob(s + '/*.jpg'):
for filename in glob.glob(os.path.join(s, '*.jpg')):
list_imgName.append(filename.split('\\')[-1]) # Windows系統
# list_imgName.append(filename.split('/')[-1]) # Mac系統
cb_Image.config(values=list_imgName)
elif e.widget is btn_brow_Image2: # Image 2
global source_dir_img2
s = filedialog.askdirectory(initialdir="D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\Test Image", title='Please select a directory')
if s != '':
root.setvar('ent_Image2', s)
source_dir_img2 = s
list_imgName = []
# for filename in glob.glob(s + '/*.jpg'):
for filename in glob.glob(os.path.join(s, '*.jpg')):
list_imgName.append(filename.split('\\')[-1]) # Windows系統
# list_imgName.append(filename.split('/')[-1]) # Mac系統
cb_Image2.config(values=list_imgName)
elif e.widget is btn_brow_Image3: # Image 3
global source_dir_img3
s = filedialog.askdirectory(initialdir="D:\Machine Learning\工研院產業新尖兵\專題\結案報告\Demo\Test Image", title='Please select a directory')
if s != '':
root.setvar('ent_Image3', s)
source_dir_img3 = s
list_imgName = []
# for filename in glob.glob(s + '/*.jpg'):
for filename in glob.glob(os.path.join(s, '*.jpg')):
list_imgName.append(filename.split('\\')[-1]) # Windows系統
# list_imgName.append(filename.split('/')[-1]) # Mac系統
cb_Image3.config(values=list_imgName)
def execute_mode_changed():
if v_exe.get() == 1: # batch
root.setvar('ent_Test', '')
elif v_exe.get() == 2: # single
root.setvar('ent_Test', '')
elif v_exe.get() == 3: # Youtube
root.setvar('ent_Test', ParametersProject.link_Youtube)
elif v_exe.get() == 4: # DroidCam
root.setvar('ent_Test', ParametersProject.link_DroidCam)
elif v_exe.get() == 5: # WebCam
root.setvar('ent_Test', '')
import tkinter as tk
v_exe = tk.IntVar()
v_exe.set(1)
#rbt_batch = ttk.Radiobutton(root, text = 'Batch', variable=v_exe, value=1, command=execute_mode_changed, style = 'my.TRadiobutton')
#rbt_batch.place(x=160, y=135, width=60, height=30)
photo_icon_batch = PhotoImage(file = "GUI Image/folder32.png")
rbt_batch = ttk.Radiobutton(root, image = photo_icon_batch, variable=v_exe, value=1, command=execute_mode_changed, style = 'my.TRadiobutton')
rbt_batch.place(x=160, y=130, width=60, height=55)
#rbt_single = ttk.Radiobutton(root, text = 'Single', variable=v_exe, value=2, command=execute_mode_changed, style = 'my.TRadiobutton')
#rbt_single.place(x=230, y=135, width=65, height=30)
photo_icon_single = PhotoImage(file = "GUI Image/image.png")
rbt_single = ttk.Radiobutton(root, image = photo_icon_single, variable=v_exe, value=2, command=execute_mode_changed, style = 'my.TRadiobutton')
rbt_single.place(x=230, y=130, width=60, height=55)
#rbt_Youtube = ttk.Radiobutton(root, text = 'Youtube', variable=v_exe, value=3, command=execute_mode_changed, style = 'my.TRadiobutton')
#rbt_Youtube.place(x=305, y=135, width=75, height=30)
photo_icon_Youtube = PhotoImage(file = "GUI Image/youtube.png")
rbt_Youtube = ttk.Radiobutton(root, image = photo_icon_Youtube, variable=v_exe, value=3, command=execute_mode_changed, style = 'my.TRadiobutton')
rbt_Youtube.place(x=300, y=130, width=60, height=55)
#rbt_camera = ttk.Radiobutton(root, text = 'DroidCam', variable=v_exe, value=4, command=execute_mode_changed, style = 'my.TRadiobutton')
#rbt_camera.place(x=370, y=130, width=90, height=35)
photo_icon_camera = PhotoImage(file = "GUI Image/DroidCam2_50.png")
rbt_camera = ttk.Radiobutton(root, image = photo_icon_camera, variable=v_exe, value=4, command=execute_mode_changed, style = 'my.TRadiobutton')
rbt_camera.place(x=370, y=130, width=80, height=55)
#rbt_WebCam = ttk.Radiobutton(root, text = 'WebCam', variable=v_exe, value=5, command=execute_mode_changed, style = 'my.TRadiobutton')
#rbt_WebCam.place(x=500, y=135, width=80, height=30)
#rbt_WebCam = ttk.Radiobutton(root, text = 'WebCam', variable=v_exe, value=5, command=execute_mode_changed, style = 'my.TRadiobutton')
#rbt_WebCam.place(x=460, y=130, width=80, height=55)
photo_icon_WebCam = PhotoImage(file = "GUI Image/WebCam.png")
rbt_WebCam = ttk.Radiobutton(root, image = photo_icon_WebCam, variable=v_exe, value=5, command=execute_mode_changed, style = 'my.TRadiobutton')
rbt_WebCam.place(x=460, y=130, width=60, height=55)
# print(v_exe.get())
def btn_Execute_press(e):
btn_Execute.config(style='my3.TButton')
global ParametersYOLO, source_dir_img
if v_exe.get() == 1: # batch
dir = root.getvar('ent_Test')
if os.path.isdir(dir):
ParametersYOLO.source_path = dir
elif v_exe.get() == 2: # single
try:
path = source_dir_img + '/' + root.getvar('cb_Image')
if os.path.exists(path):
ParametersYOLO.source_path = path
except:
btn_Execute.config(style='my.TButton')
return
elif v_exe.get() == 3: # Youtube
ParametersProject.link_Youtube = root.getvar('ent_Test')
ParametersYOLO.source_path = ParametersProject.link_Youtube
elif v_exe.get() == 4: # DroidCam
ParametersProject.link_DroidCam = root.getvar('ent_Test')
ParametersYOLO.source_path = ParametersProject.link_DroidCam
elif v_exe.get() == 5: # WebCam
ParametersYOLO.source_path = '0'
opt = parse_opt()
main(opt)
gc.collect() # or gc.collect(2)
btn_Execute.config(style='my.TButton')
# lb_Model = Label(root, text="Model", fg="white", bg="blue")
lb_Model = ttk.Label(root, text="Model", style='my.TLabel')
lb_Model.place(x=10, y=10, width=100, height=30)
# lb_Model.config(font="Arial 22 bold italic")
lb_Test = ttk.Label(root, text="Test", style='my.TLabel')
lb_Test.place(x=10, y=50, width=100, height=30)
ent_Model = ttk.Entry(root, width=60, textvariable= 'ent_Model')
ent_Model.place(x=130, y=10, width = 550, height=30)
root.setvar('ent_Model', 'Model path')
ent_Test = ttk.Entry(root, width=60, textvariable= 'ent_Test')
ent_Test.place(x=130, y=50, width = 550, height=30)
root.setvar('ent_Test', 'Test image directory')
btn_brow_Model = ttk.Button(root, text="Browse", style='my2.TButton')
btn_brow_Model.place(x=700, y=10, width = 80, height=30)
btn_brow_Model.bind('<ButtonRelease-1>', btn_Browse)
btn_brow_Test = ttk.Button(root, text="Browse", style='my2.TButton')
btn_brow_Test.place(x=700, y=50, width = 80, height=30)
btn_brow_Test.bind('<ButtonRelease-1>', btn_Browse)
photo_icon_Execute = PhotoImage(file = "GUI Image/execution.png")
btn_Execute = ttk.Button(root, text=" Execute", style='my.TButton', image = photo_icon_Execute, compound = LEFT)
# btn_Execute = Button(root, text=" Execute", fg='green', image = photo_icon_Execute, compound = LEFT)
btn_Execute.place(x=20, y=130, width=130, height=40)
btn_Execute.bind('<ButtonRelease-1>', btn_Execute_press)
def btn_player_press(e):
if e.widget is btn_Stop: # Stop
global B_StopRun
B_StopRun = True
photo_icon_Stop = PhotoImage(file = "GUI Image/stop.png")
btn_Stop = ttk.Button(root, image = photo_icon_Stop)
btn_Stop.place(x=540, y=135, width=40, height=40)
btn_Stop.bind('<ButtonRelease-1>', btn_player_press)
ca = Canvas(root, width=1000, height=800, bg='white')
ca.place(x=10, y=190, width = 1000, height = 800)
from PIL import Image, ImageTk
import PIL
import numpy as np
# import tkinter
# photo_1 = ImageTk.PhotoImage(Image.open("plates.jpg"))
# ca.create_image(0, 0, image = photo_1)
import cv2
# Use PIL (Pillow) to convert the NumPy ndarray to a PhotoImage
# img = cv2.imread('plates.jpg')
# print(type(img))
# photo = ImageTk.PhotoImage(image = Image.fromarray(img))
# Add a PhotoImage to the Canvas
# ca.create_image(0, 0, image=photo, anchor=tkinter.NW)
# ca.create_image(0, 0, image=photo, anchor=NW)
# ca.create_image(0, 0, image=photo, anchor='nw')
#region 鯊鯊icon
# w_icon = 200
# h_icon = 150
# size_icon = (w_icon, h_icon)
# ca_icon1 = Canvas(root, width=w_icon, height=h_icon, bg='white')
# ca_icon1.place(x=1290, y=10, width = w_icon, height = h_icon)
# # img = cv2.imdecode(np.fromfile('yolov5/GUI Image/鯊鯊1.png', dtype=np.uint8), 1)
# img = cv2.imdecode(np.fromfile('GUI Image/鯊鯊1.png', dtype=np.uint8), 1)
# img = cv2.resize(img, size_icon)
# photo_icon1 = ImageTk.PhotoImage(image = Image.fromarray(img[:, :, ::-1]))
# ca_icon1.create_image(0, 0, image=photo_icon1, anchor=tkinter.NW)
# ca_icon2 = Canvas(root, width=w_icon, height=h_icon, bg='white')
# ca_icon2.place(x=1290, y=170, width = w_icon, height = h_icon)
# # img = cv2.imdecode(np.fromfile('yolov5/GUI Image/鯊鯊2.jpg', dtype=np.uint8), 1)
# img = cv2.imdecode(np.fromfile('GUI Image/鯊鯊2.jpg', dtype=np.uint8), 1)
# img = cv2.resize(img, size_icon)
# photo_icon2 = ImageTk.PhotoImage(image = Image.fromarray(img[:, :, ::-1]))
# ca_icon2.create_image(0, 0, image=photo_icon2, anchor=tkinter.NW)
# ca_icon3 = Canvas(root, width=w_icon, height=h_icon, bg='white')
# ca_icon3.place(x=1290, y=330, width = w_icon, height = h_icon)
# # img = cv2.imdecode(np.fromfile('yolov5/GUI Image/鯊鯊3.jpg', dtype=np.uint8), 1)
# img = cv2.imdecode(np.fromfile('GUI Image/鯊鯊3.jpg', dtype=np.uint8), 1)
# img = cv2.resize(img, size_icon)
# photo_icon3 = ImageTk.PhotoImage(image = Image.fromarray(img[:, :, ::-1]))
# ca_icon3.create_image(0, 0, image=photo_icon3, anchor=tkinter.NW)
# ca_icon4 = Canvas(root, width=w_icon, height=h_icon, bg='white')
# ca_icon4.place(x=1290, y=490, width = w_icon, height = h_icon)
# # img = cv2.imdecode(np.fromfile('yolov5/GUI Image/鯊鯊4.png', dtype=np.uint8), 1)
# img = cv2.imdecode(np.fromfile('GUI Image/鯊鯊4.png', dtype=np.uint8), 1)
# img = cv2.resize(img, size_icon)
# photo_icon4 = ImageTk.PhotoImage(image = Image.fromarray(img[:, :, ::-1]))
# ca_icon4.create_image(0, 0, image=photo_icon4, anchor=tkinter.NW)
#endregion
chk_btn_saveImg =ttk.Checkbutton(root, text="Save Image", takefocus = 0, textvariable = 'chk_btn_saveImg', variable= 'chk_btnSaved')
chk_btn_saveImg.place(x=800, y=50, width = 95, height = 30)
root.setvar('chk_btn_saveImg', 'Save Image')
root.setvar('chk_btnSaved', False)
print(type(root.getvar('chk_btnSaved')))
#root.getvar('chk_btnSaved')
B_synImage = False
def chk_btn_synImage_changed():
print(root.getvar('chk_btnsynImage'))
print(type(root.getvar('chk_btnsynImage')))
global B_synImage
B_synImage = True if root.getvar('chk_btnsynImage') == '1' else False
print(B_synImage)
chk_btn_synImage =ttk.Checkbutton(root, text="Syn Image", takefocus = 0, textvariable = 'chk_btn_synImage', variable= 'chk_btnsynImage', command=chk_btn_synImage_changed)
chk_btn_synImage.place(x=800, y=130, width = 95, height = 30)
root.setvar('chk_btn_synImage', 'Syn Image')
root.setvar('chk_btnsynImage', False)
btn_brow_Image = ttk.Button(root, text="Browse", style='my2.TButton')
btn_brow_Image.place(x=700, y=90, width = 80, height=30)
btn_brow_Image.bind('<ButtonRelease-1>', btn_Browse)
# lb_Image = Label(root, text="Image", fg="white", bg="blue")
lb_Image = ttk.Label(root, text="Image", style='my.TLabel')
lb_Image.place(x=10, y=90, width=100, height=30)
# lb_Image.config(font="Arial 22 bold italic")
ent_Image = ttk.Entry(root, width=60, textvariable= 'ent_Image')
ent_Image.place(x=130, y=90, width = 550, height=30)
root.setvar('ent_Image', 'Inspect image directory')
#region Load & Inspect image
def cb_Image_Update(e = None):
global photo_cb_Image
#print(e)
print(root.getvar('cb_Image'))
path = source_dir_img + '/' + root.getvar('cb_Image')
#print(path)
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), 1)
# Resize
img = cv2.resize(img, (1000, 800))
photo_cb_Image = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(img[:, :, ::-1]))
# ca.create_image(0, 0, image=photo_cb_Image, anchor=tkinter.NW)
ca.create_image(0, 0, image=photo_cb_Image, anchor='nw')
# root.update()
#cb_Image = ttk.Combobox(root,font="Arial 12", height=10, width=10, textvariable= 'cb_Image', command=cb_Image_Update)
cb_Image = ttk.Combobox(root,font="Arial 12", height=10, width=10, textvariable= 'cb_Image')
cb_Image.place(x=800, y=90, height=30, width = 100)
cb_Image.config(values=[])
cb_Image.bind("<<ComboboxSelected>>", cb_Image_Update)
#photo_icon_next = PhotoImage(file = "GUI Image/next.png")
img = Image.open("GUI Image/next.png")
img = img.resize((32, 32), Image.ANTIALIAS)
photo_icon_next = ImageTk.PhotoImage(img)
btn_Next = ttk.Button(root, text="Next", image = photo_icon_next)
# btn_Next.pack(side = TOP)
btn_Next.place(x=960, y=85, height=40)
img = Image.open("GUI Image/prev.png")
img = img.resize((32, 32), Image.ANTIALIAS)
photo_icon_prev = ImageTk.PhotoImage(img)
btn_Prev = ttk.Button(root, text="Prev", image = photo_icon_prev)
# btn_Prev.pack(side = TOP)
btn_Prev.place(x=910, y=85, height=40)
def btn_NextPrevImg(e):
# print('-' * 10)
# print(root.getvar('cb_Image'))
# print(cb_Image.get())
index_now = cb_Image.current()
# print(index_now)
bNext = False
if e.widget is btn_Next:
bNext = True
index_max = len(cb_Image['values']) - 1
if index_now >= index_max:
return
index_now += 1
elif e.widget is btn_Prev:
if index_now <= 0:
return
index_now -= 1
cb_Image.current(index_now)
cb_Image_Update() # 更新影像顯示
global B_synImage
if B_synImage:
btn_NextPrevImg2(None, bNext)
btn_NextPrevImg3(None, bNext)
btn_Next.bind('<ButtonRelease-1>', btn_NextPrevImg)
btn_Prev.bind('<ButtonRelease-1>', btn_NextPrevImg)
#endregion
tabPage_frame = Frame(root, bg="white", relief=SUNKEN)
tabPage_frame.place(x=1020, y=10, width = 620, height=980)
a_notebook = ttk.Notebook(tabPage_frame, width=200, height=200)
# a_notebook.config(font="Arial 25 bold italic")
# tab1 = Frame(a_notebook, bg='red')
tab1 = ttk.Frame(a_notebook)
a_notebook.add(tab1, text = 'Real-time Detection')
tab2 = ttk.Frame(a_notebook)
a_notebook.add(tab2, text = 'More Information')
tab3 = ttk.Frame(a_notebook)
a_notebook.add(tab3, text = 'Advanced Setting')
tab4 = ttk.Frame(a_notebook)
a_notebook.add(tab4, text = 'Others')
# a_notebook.pack(expand=True, fill=tk.BOTH)
a_notebook.pack(expand=True, fill='both')
# a_notebook.pack(expand=True)
#region 【Real-time Detection】
class singleBlock():
def __init__(self, parentFrame_: ttk.Frame, x: int, y: int, w: int, h: int):
"""
Constructor: Class initialization
"""
# print('__init__')
self.parentFrame = parentFrame_
self.block = Frame(parentFrame_, bg="orange")
self.block.place(x=x, y=y, width=w, height=h)
self.canvas1 = Canvas(self.block, bg='white')
self.canvas1.place(x=5, y=5, width=138, height=130)
self.canvas2 = Canvas(self.block, bg='white')
self.canvas2.place(x=148, y=5, width=138, height=130)
self.labelConf = None
self.labelInfo = Label(self.block, text='', bg="white")
self.labelInfo.place(x=5, y=140, width=281, height=28)
self.labelInfo.config(font="Arial 20 bold italic")
# self.disable()
# self.enable()
def destroy(self):
"""
Destroy block
"""
self.block.destroy()
def enable(self):
for child in self.block.winfo_children():
# print(child['state'])
child.configure(state='normal')
def disable(self):
for child in self.block.winfo_children():
# print(child['state'])
child.configure(state='disable')
def updateDisplay(self, image_orig, image_stan, label: str, confidence: float, info: str, color: str):
# Resize
img = cv2.resize(image_orig, (138, 130))
self.photo_orig = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(img[:, :, ::-1]))
self.canvas1.create_image(0, 0, image=self.photo_orig, anchor='nw')
if image_stan is not None:
img = cv2.resize(image_stan, (138, 130))
self.photo_stan = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(img[:, :, ::-1]))
self.canvas2.create_image(0, 0, image=self.photo_stan, anchor='nw')
self.labelConf = ttk.Label(self.block, text=f'{label}:{confidence:.2f}', style='my4.TLabel')
self.labelConf.place(x=70, y=3, width=160, height=25)
# self.labelInfo = Label(self.block, text=info, fg=color, bg="white")
self.labelInfo['text'] = info
self.labelInfo['fg'] = color
def reset(self):
if self.labelConf is not None:
self.labelConf.destroy()
self.labelConf = None
self.labelInfo['text'] = ''
self.canvas1.delete(ALL)
self.canvas2.delete(ALL)
class updateBlocks():
def __init__(self, parentFrame_: ttk.Frame, scrollableFrame_: ttk.Frame, count: int):
"""
Constructor: Class initialization
"""
# print('__init__')
self.parentFrame = parentFrame_
self.scrollableFrame = scrollableFrame_
self.listBlocks = []
self.update(count)
def update(self, count: int):
"""
Update all blocks
"""
self.clear()
y = 30
w = 290
h = 170
w_parentFrame = 595
# Adjust width of parentFrame
# x_max = 5 + (w + 5) * (count - 1) + w
# if x_max > w_parentFrame - 5:
# self.parentFrame.config(width=x_max + 5)
# self.scrollableFrame.config(width=x_max + 5)
# else:
# self.parentFrame.config(width=w_parentFrame)
# self.scrollableFrame.config(width=w_parentFrame)
for i in range(count):
self.listBlocks.append(singleBlock(self.parentFrame, x = 5 + (w + 5) * i, y = y, w = w, h = h))
def clear(self):
"""
Clear all blocks
"""
for block in self.listBlocks:
block.destroy()
self.listBlocks = []
def updateDisplay(self, listImage_orig_: list, listImage_stan_: list, info_: list):
"""
Update display
'''
Parameters
----------
info_ : list (n, information), where information: (label, confidence, description or warning, color)
e.g. [('Red', 0.94, 'Stop!', 'red'), ('Green', 0.64, 'Go!', 'green'), ('Red', 0.74, 'Stop!', 'red')]
Note: 順序依照面積由大到小
"""