-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathScaleSolver.py
1344 lines (1221 loc) · 51.5 KB
/
ScaleSolver.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
import os, sys
from config import ROS_PATH
if ROS_PATH in sys.path:
sys.path.remove(ROS_PATH)
from tkinter import *
from tkinter import filedialog, ttk, scrolledtext, Menu, messagebox
from PIL import ImageTk, ImageOps
from PIL import Image as PILImage
import pickle
import numpy as np
import cv2
import math
from src.ui.drag_rect import DragRect
from src.ui.color import marker_colors,get_hex_by_index,get_finegrained_color
from src.ui.dialog import View_AskDistance, View_AskIDLabel
from src.ui.video_clipper import VideoClipperWindow
from src.ui.lane_frame import LaneSettingFrame
from src.ui.canvas import ZoomFrame # , ZoomFrame2
from src.ui.image_utils import *
from src.ui.drag_line import DragLine
#from src.calibration.calib_main import camera_clibration_canvas
#from src.detect.detect_factory import Detecter
#from src.track.track_factory import TRACKER
#from src.track.trajectory import postprocessing_trajectory,select_trajectory, auto_refine
#from src.road.road_main import initialize_bounary_points, generate_road
from src.error_check import multi_frame_error_check
from config import DET_CONFIG,TRACK_CONFIG, SOURCE
from src.scale.PoseData import LoadPoseData
from src.scale.CameraCal import CamCal
from src.scale.utils import *
from src.file_parser import *
import subprocess
import argparse
'''
Note:
All coordinates loaded in variables are canvas coordiantes(for drawing)
Saved coordinates are pixel-based, so need conversion when save/load
'''
def parse_args():
parser = argparse.ArgumentParser(
description='Set the video name')
parser.add_argument('--videoname', type=str,
help='path to the video', required=True)
return parser.parse_args()
CANVAS_WIDTH = 640
CANVAS_HEIGHT = 480
ORIENTATION_LIST = ['North', 'West', 'South', 'East']
RADIUS = 7#to draw oval in road
LINEWIDTH = 2 #draw line
SMALL_CANVAS_SIZE=200
SHOW_ZOOMED_FOCUS = True
class MainGUI:
def __init__(self, master,videoname):
self.parent = master
self.parent.title("ScaleSolver v0.1")
self.canvasFrame = Frame(self.parent)
self.tabFrame = Frame(self.parent)
self.statusFrame = Frame(self.parent)
self.titleFrame = Frame(self.parent)
self.titleFrame.grid(row=0, column=0, columnspan=2, sticky="ew")
self.canvasFrame.grid(row=1, column=0, sticky="n")
self.tabFrame.grid(row=1, column=1, sticky="nsew")
self.statusFrame.grid(row=3, column=0, columnspan=2, sticky="ew")
self.cameraView = StringVar()
self.detectionModel = StringVar()
self.enableTrajDisp = IntVar()
# ----------------- GUI stuff ---------------------
self.titleLabel = Label(self.titleFrame, text="Video:", anchor=W)
self.openBtn = Button(self.titleFrame, text="Open Video",command=self.open_video)
self.SLAMBtn = Button(self.titleFrame, text="Mono SLAM", command=self.start_SLAM)
self.titleLabel.pack(fill=X, side=LEFT, expand=1)
#self.cameraViewBox.pack(fill=X, side=RIGHT, padx=5, expand=0)
self.SLAMBtn.pack(fill=X, side=RIGHT, padx=5, expand=0)
self.openBtn.pack(fill=X, side=RIGHT, padx=5, expand=0)
self.scale_solver = None
self.canvasDisplayFrame = ttk.Frame(self.canvasFrame)
self.canvasDisplayFrame.grid(row=0, column=0, columnspan=2)
self.videoNavigationFrame = ttk.Frame(self.canvasFrame, width=CANVAS_WIDTH-SMALL_CANVAS_SIZE-50)
self.videoNavigationFrame.grid(row=1, column=0,padx=5, sticky="w")
self.resultDisplayFrame = ttk.Frame(self.canvasFrame)
self.resultDisplayFrame.grid(row=1, column=1, padx=10, pady=10, sticky="e")
self.canvasPaintFrame = ZoomFrame(self.canvasDisplayFrame, width=CANVAS_WIDTH, height=CANVAS_HEIGHT,
background="#90AFC5") # cursor='tcross'
self.frameLabel = Label(self.videoNavigationFrame, text="Frame ")
self.gotoFirstBtn = Button(self.videoNavigationFrame, text="|<", command=self.goto_first)
self.gotoPrev10Btn = Button(self.videoNavigationFrame, text="<<", command=self.goto_prev10)
self.gotoPrevBtn = Button(self.videoNavigationFrame, text="<", command=self.goto_prev)
self.playBtn = Button(self.videoNavigationFrame,text="|>", command=self.play)
self.gotoNextBtn = Button(self.videoNavigationFrame, text=">", command=self.goto_next)
self.gotoNext10Btn = Button(self.videoNavigationFrame, text=">>", command=self.goto_next10)
self.gotoLastBtn = Button(self.videoNavigationFrame, text=">|", command=self.goto_last)
self.clipperIcon = PhotoImage(file="./icon/clipper.jpg")
self.videoEditBtn = Button(self.videoNavigationFrame, image=self.clipperIcon, command=self.video_edit)
self.frameLabel.pack(fill=BOTH, side=TOP)
self.frameLabel.config(font=("Helvetica", 16))#Helvetica, Rouge
self.gotoFirstBtn.pack(fill=X, side=LEFT, padx=1, expand=0)
self.gotoPrev10Btn.pack(fill=X, side=LEFT, padx=1, expand=0)
self.gotoPrevBtn.pack(fill=X, side=LEFT, padx=1, expand=0)
self.playBtn.pack(fill=X, side=LEFT, padx=1, expand=0)
self.gotoNextBtn.pack(fill=X, side=LEFT, padx=1, expand=0)
self.gotoNext10Btn.pack(fill=X, side=LEFT, padx=1, expand=0)
self.gotoLastBtn.pack(fill=X, side=LEFT, padx=1, expand=0)
self.videoEditBtn.pack(fill=X, side=LEFT, padx=5, expand=0)
self.animationDisplayCanvas = Canvas(self.resultDisplayFrame, background='white',
width=SMALL_CANVAS_SIZE,height=SMALL_CANVAS_SIZE)
self.animationDisplayCanvas.pack(fill=X, side=RIGHT)
# s =ttk.Style()
# s.configure('', tabposition='en')
# self.tabFrame.pack_propagate(False)
self.tabControl = ttk.Notebook(self.tabFrame)
self.tabCalib = ttk.Frame(self.tabControl)
self.tabControl.add(self.tabCalib, text='ScaleSolve')
self.tabControl.pack(expand=1, fill="both") # Pack to make visible
# tab-calibration
# self.calHlsBtn = Button(self.tabCalib, text='HLS_Image', command=self.show_hls)
# self.calHlsBtn.pack(fill=X, side=TOP, pady=5)
self.calPointEditFrame = Frame(self.tabCalib)
self.calPointEditFrame.pack(fill=X,pady=5, side=TOP)
#Button(self.tabCalib, text="MonoSLAM", command=self.clean_cal_selections).pack(fill=X, side=TOP) # , pady=1)
self.calEditPLRPBtn = Button(self.calPointEditFrame, text="Adjust",command=self.select_edit_add,
disabledforeground = 'red')
self.calEditPLRPBtn.grid(row=0, column=1, rowspan=2, sticky='ew')
self.calAddRPBtn = Button(self.calPointEditFrame, text="Insert Ref Points",command=self.select_ref_points,
disabledforeground="red")
self.calAddRPBtn.grid(row=1, column=0, sticky='we')
self.calPointEditFrame.columnconfigure(0, weight=1)
self.calPointEditFrame.columnconfigure(1, weight=1)
Button(self.tabCalib, text="Re-Selection",command=self.clean_cal_selections).pack(fill=X, side=TOP) # , pady=1)
Button(self.tabCalib, text="AutoCalibrate", command=self.cam_calib).pack(fill=X, side=TOP) # , pady=1)
Button(self.tabCalib, text="Save Results",command=self.calib_save_results).pack(fill=X, side=TOP) # , pady=1)
#Button(self.tabCalib, text="Display Results",command=self.calib_disp_results).pack(fill=X, side=TOP) # , pady=1)
ttk.Separator(self.tabCalib, orient=HORIZONTAL).pack(fill=X, pady=10, side=TOP) # grid(..., sticky="ew")
self.calFrameLbl = Label(self.tabCalib, text="")
self.calFrameLbl.pack(fill=X, side=TOP)
Label(self.tabCalib, text="Selected Reference Lines").pack(fill=X, side=TOP)
self.calRPListBox = Listbox(self.tabCalib, width=40, height=5)
self.calRPListBox.pack(fill=X, side=TOP)
Label(self.tabCalib, text="Scaled Map",font=("Helvetica", 12),background='green', fg='white').pack(fill=X, side=TOP)
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
self.Mapfig = Figure(figsize=(5, 4), dpi=100)
#fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t), 'o')
# canvas.draw()
self.ax_map = self.Mapfig.add_subplot(111)
self.ax_map.set_xlim([-50,50])
self.ax_map.set_ylim([-50,500])
self.MapCanvas = FigureCanvasTkAgg(self.Mapfig, master=self.tabCalib) # A tk.DrawingArea.
self.MapCanvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
#self.calRstListBox = Listbox(self.tabCalib, height=20)
#self.calRstListBox.pack(fill=X, side=TOP)
# status frame
self.statusLabel = Label(self.statusFrame, text="Status:", bd=1, relief=SUNKEN, anchor=W)
self.statusLabel.pack(fill=X, side=LEFT, expand=True)
self.statusFrameLabel = Label(self.statusLabel, text="Frame", bd=1, relief=SUNKEN, anchor=E)
self.statusFrameLabel.pack(fill=BOTH, side=RIGHT, expand=False)
self.parent.grid_rowconfigure(1, weight=1)
self.parent.grid_columnconfigure(1, weight=1)
self.bind_events()
# ----------------- Variables---------------------
self.videoFilename = videoname
self.videoCap = None
self.totalFrameNum = 0
self.currFrameId = None
self.calFrameID = None # frame id to do clibration
self.roadFrameID = None # frame id to do road reconstruction
self.imgRaw = None # cv mode(bgr)
self.imgDisp = None
self.imgScale = None
self.imgPadding = None
self.imgHLS = None
self.imgHLSDisp = None
self.imgRoad = None
self.imgRoadDisp = None
self.tkimg = None
self.detector = None
self.tracker = None
self.ObjectList = None
self.ObjectList_pixel = None
self.trajectory = None
self.nextObjectID = 1
self.calParaLines = [] # canvas_coords,group of 4 points
self.calRefPoints = [] # canvas_coords,group of 2 points
self.calParaLinesObjIds = []
self.calRefPointsObjIds = []
self.cameraParam = None
self.roadBoundPts = dict() # save selected roudn poits
self.roadLaneNum ={'ne':1, 'nw':1, 'wn':1, 'ws':1, 'sw':1, 'se':1,'es':1,'en':1}#default
self.dragRectObjs = None
self.labelList =['Car', 'Bus','Pedestrian','Rider',
'Truck', 'Motorcycle','Bicycle','Tricycle']#TODO: read/write to file
# mode-related variables
self.currHitTab = "" # text
self.hlsImageMode = False
self.roadImageMode = False #road or hls image
self.playMode = True #play or pause
self.calAddPLMode = True #para/ref to be added
self.calAddEditMode = True #edit or insert ref/para
self.InsertObjectMode = False #add or edit objects in canvas
# temporay variables used to recording intermediate status
self.tempParaLineObjIds = []
self.tempDragData = None # drag
self.tempInsertObjectData = dict()
self.tempParallelData = dict()
self.tempRefPointData = dict() #temp when drawing line(parallel mode)
self.tempPLLine = [] #save temporary line(first) when drawing 2 parallel lines
###new added
self.tempRoadData = None # road
self.SamplesFlag_N = False
self.SamplesFlag_W = False
self.SamplesFlag_S = False
self.SamplesFlag_E = False
self.RoadDataList_N=[]
self.RoadDataList_W=[]
self.RoadDataList_S=[]
self.RoadDataList_E=[]
#icon image
self.icon_image_size=(20,20)
warning_icon = Image.open('icon/warning.png')
warning_thumb=ImageOps.fit(warning_icon, self.icon_image_size, Image.LANCZOS)
self.warning_tkimg = ImageTk.PhotoImage(warning_thumb)
pass_icon = Image.open('icon/pass.png')
pass_thumb=ImageOps.fit(pass_icon, self.icon_image_size, Image.LANCZOS)
self.pass_tkimg = ImageTk.PhotoImage(pass_thumb)
empty_arr = np.zeros([self.icon_image_size[1], self.icon_image_size[0], 3],dtype=np.uint8)
empty_arr.fill(255)
self.empty_tkimg = ImageTk.PhotoImage(Image.fromarray(empty_arr))
self.open_video(self.videoFilename)
def bind_events(self): # bind all messages
self.parent.bind("<Key-Left>", self.goto_prev)
self.parent.bind("<Key-Right>", self.goto_next)
self.parent.bind("Escape", self.cancel_bbox)
self.tabControl.bind("<<NotebookTabChanged>>", self.tab_click)
self.canvasPaintFrame.canvas.bind("<Button-1>", self.mouse_click)
self.canvasPaintFrame.canvas.bind("<Motion>", self.mouse_move)#, "+")
self.canvasPaintFrame.canvas.bind("<B1-Motion>", self.mouse_drag)
self.canvasPaintFrame.canvas.bind("<ButtonRelease-1>", self.mouse_release)
#self.calPLListBox.bind('<Key>', self.edit_parallel_line_keypress)
self.calRPListBox.bind('<Double-Button>', self.edit_reference_points)
self.calRPListBox.bind('<Key>', self.edit_reference_points_keypress)
'''
self.detObjectListBox.bind('<Double-Button>', self.edit_object_label_id)
self.detObjectListBox.bind('<<ListboxSelect>>', self.listbox_select)
self.detObjectListBox.bind('<Key>', self.object_listbox_keypress)
self.frameTreeView.bind('<Double-Button>', self.treeview_click)
'''
def open_video(self, filename=None):
# self.parent.focus()
if filename is None:
self.videoFilename = filedialog.askopenfilename(title="Select Video", filetypes=(("video files", "*.mp4"), ("video files", "*.avi"),
("all files", "*.*")))
if not self.videoFilename:
return
else:
self.videoFilename = filename
if not isinstance(self.videoFilename, str):
return
if not os.path.exists(self.videoFilename):
messagebox.showerror("Error", "Cannot find file")
self.videoFilename = None
return
try:
self.videoCap.release()
except: #do nothing
pass
self.videoCap = cv2.VideoCapture(self.videoFilename)
if not self.videoCap.isOpened():
messagebox.showerror("Error", "Cannot open video file")
self.videoFilename = None
self.videoCap = None
return
#self.clean_draw_objects()
#kf_saver = os.path.join(self.videoFilename + "_KeyFrameTrajectory.txt")
#times_saver = os.path.join(self.videoFilename + "_times.txt")
#gt_saver = os.path.join(self.videoFilename + "_gt.txt")
#if not os.path.exists(kf_saver):
#return
kf_saver = os.path.join(os.path.dirname(self.videoFilename),
'KeyFrameTrajectory.txt')
times_saver = os.path.join(os.path.dirname(self.videoFilename),
'times.txt')
gt_saver = os.path.join(self.videoFilename + "_gt.txt")
if os.path.exists(kf_saver) and os.path.exists(times_saver):
self.kf_idx_list, self.kf_pose_data = LoadPoseData(kf_saver,times_saver)
self.canvasPaintFrame.canvas.delete('all')
self.reset_modes(True)
self.totalFrameNum = int(self.videoCap.get(7))
self.currFrameId = 0
self.titleLabel.config(text="Video Path: "+self.videoFilename)
self.load_image_data(self.kf_idx_list[self.currFrameId])
if os.path.exists(gt_saver):
self.load_pose_gt(gt_saver)
self.ax_map.plot(self.GT_Position[:, 0], self.GT_Position[:, 1], 'o')
self.ax_map.legend(['GT_map'])
self.MapCanvas.draw()
self.draw_image(self.imgDisp)
self.draw_annotations()
#self.generate_thumbnail_view()
self.update_message_boxes()
self.statusFrameLabel.config(text ='Frame {}/{}'.format(self.kf_idx_list[self.currFrameId], self.totalFrameNum))
self.frameLabel.config(text ='Frame {}'.format(self.kf_idx_list[self.currFrameId]))
else:
messagebox.showinfo(
title='Warning', message='Please do SLAM first')
return
def load_pose_gt(self,gt_file):
self.GT_Pose = read_kitti_poses_file(gt_file)
self.GT_Position = np.asarray([[pose[0,3],pose[2,3]] for pose in self.GT_Pose])
def start_SLAM(self):
cmd = "cd ./src/SLAM/ && ./mono_kitti ORBvoc.txt KITTI00-02.yaml test2"
#"./Examples/Monocular/mono_kitti Vocabulary/ORBvoc.txt Examples/Monocular/KITTIX.yaml PATH_TO_DATASET_FOLDER/dataset/sequences/SEQUENCE_NUMBER"
subprocess.Popen(cmd, shell=True, executable="/bin/bash")
print("gen enhance file cmd", cmd)
print("generate test cases for scenario %s" % casename + "_00")
def load_image_data(self, frame_id):
print(frame_id)
self.videoCap.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
_, self.imgRaw = self.videoCap.read()
self.imgDisp, _, self.imgScale, self.imgPadding = fit_image_to_canvas(self.imgRaw,
desired_width=CANVAS_WIDTH, desired_height=CANVAS_HEIGHT)
def draw_image(self, image_data):
if image_data is None:
return
rgb = cv2.cvtColor(image_data, cv2.COLOR_BGR2RGB)
self.tkImg = ImageTk.PhotoImage(PILImage.fromarray(rgb))
self.canvasPaintFrame.load_image(PILImage.fromarray(rgb))
def draw_annotations(self):
# redraw all annotaitons(detection, calibration, road)
if self.imgRaw is None:
return
if self.currHitTab=='Detect&Track':
if self.ObjectList is None:
self.ObjectList = [[] for _ in range(self.totalFrameNum)]
self.ObjectList_pixel = [[] for _ in range(self.totalFrameNum)]
return
curr_anno = self.ObjectList[self.currFrameId]
self.dragRectObjs = []
for anno in curr_anno:
id =anno[0]
if 1:
color = get_hex_by_index(id)
else: #debug mode, color differs on source
src = anno[-1]
if src ==0: #auto-det
color = "#FF0000"#red
elif src==1: #track
color = '#FFFF00'#yellow
elif src==2: #manual
color = '#03ecfc'#blue
else: #others
color = '#008000'#green
label_str = self.labelList[anno[1]]
textstr = "{} {}".format(label_str, id)
points =[[anno[2], anno[3]],[anno[4], anno[5]]]
ls = DragRect(self, self.canvasPaintFrame.canvas,points, color,
title = textstr, objectID=id)
self.dragRectObjs.append(ls)
self.show_trajectory(False)
self.show_trajectory(True)
elif self.currHitTab=='ScaleSolve':
if not self.calFrameID is None:
self.calFrameLbl.configure(text='Frame {}'.format(self.calFrameID+1))
if self.currFrameId == self.calFrameID:
self.calParaLinesObjIds = []
for item in self.calParaLines:
temp_objs = []
oid = item[0][0]
temp_objs = []
for i in range(2):
title = '{}({})'.format(oid, i+1)
points = item[i][1:]
para_line = DragLine(self, self.canvasPaintFrame.canvas, points,
oid, color='red',tag='parallel_line',
title=title)
temp_objs.append(para_line)
self.calParaLinesObjIds.append(temp_objs)
self.calRefPointsObjIds=[]
for item in self.calRefPoints:
temp_objs = []
oid = item[0]
points = item[1:5]
title = '{}'.format(oid)
ref_line = DragLine(self, self.canvasPaintFrame.canvas,
points, oid, color='yellow',
tag ='reference_line', title=title)
self.calRefPointsObjIds.append(ref_line)
elif self.currHitTab=='Road Reconstruction':
if self.roadFrameID == self.currFrameId:
if bool(self.roadBoundPts):
self.bound_oval_id=dict()
for key in self.roadBoundPts:
x,y =self.roadBoundPts[key][0], self.roadBoundPts[key][1]
self.bound_oval_id[key] = self.canvasPaintFrame.canvas.create_oval(x - RADIUS, y - RADIUS,
x + RADIUS, y + RADIUS,
fill=marker_colors[key], tags='boundary_points')
self.canvasPaintFrame.canvas.tag_bind("boundary_points", "<ButtonPress-1>", self.drag_oval_start)
self.canvasPaintFrame.canvas.tag_bind("boundary_points", "<ButtonRelease-1>", lambda event, tag='boundary_points':self.drag_oval_stop(event, tag))
self.canvasPaintFrame.canvas.tag_bind("boundary_points", "<B1-Motion>", lambda event, tag='boundary_points':self.drag_oval(event, tag))
if bool(self.roadLaneNum):
self.laneSettingFrame.set_lane_num(self.roadLaneNum)
def tab_click(self, event):
selection = event.widget.select()
tab = event.widget.tab(selection, "text")
if 'Detect' in tab:
self.canvasPaintFrame.set_zoomable(True)
else:
self.canvasPaintFrame.set_zoomable(False)
self.currHitTab = tab
self.clean_draw_objects()
self.reset_modes(False)
self.draw_image(self.imgDisp)
self.draw_annotations()
self.update_message_boxes()
def update_dragrect(self, target_object_id):
#update annotation once dragrect is changed
for i, obj in enumerate(self.dragRectObjs):
if obj.objectID ==target_object_id: #find object
drag_rect_points = obj.points
canvas_points = self.canvasPaintFrame.get_pixel_values(drag_rect_points)
pixel_points = convert_canvas_to_pixel(canvas_points, self.imgScale, self.imgPadding)
self.ObjectList[self.currFrameId][i][2:6]= np.asarray(canvas_points).ravel().astype(int)
self.ObjectList_pixel[self.currFrameId][i][2:6]=np.asarray(pixel_points).ravel().astype(int)
self.ObjectList[self.currFrameId][i][-1] = SOURCE['Manual']
self.ObjectList_pixel[self.currFrameId][i][-1] = SOURCE['Manual']
break
self.update_message_boxes()#update framelist box info.
def update_line(self, target_object_id, type):
if type=='parallel_line':
for i, lines in enumerate(self.calParaLines):
if lines[0][0]==target_object_id:
for j in range(2):
points = self.calParaLinesObjIds[i][j].points
self.calParaLines[i][j][1:] = np.asarray(points).ravel()
break
elif type=='reference_line':
for i, line in enumerate(self.calRefPoints):
if line[0]==target_object_id:
points = self.calRefPointsObjIds[i].points
self.calRefPoints[i][1:5] = np.asarray(points).ravel()
break
self.update_message_boxes()
def update_message_boxes(self):
if self.currHitTab =='Detect&Track':
if self.ObjectList is None:
return
#update object listbox
self.detObjectListBox.delete(0,END)
curr_objs = self.ObjectList[self.currFrameId]
for obj in curr_objs:
temp_str = '{} : {}'.format(obj[0], self.labelList[obj[1]])
self.detObjectListBox.insert(END, temp_str)
valid_arr = multi_frame_error_check(self.ObjectList)
tree = self.frameTreeView.get_children()
if len(tree)>0:
for i in range(self.totalFrameNum):
src_list = [item[-1] for item in self.ObjectList[i]]
src_list = list(set(src_list))
info_str = ''
if SOURCE['Auto'] in src_list:
info_str += ' A '
else:
info_str += ' '
if SOURCE['Track'] in src_list:
info_str += ' T '
else:
info_str += ' '
if SOURCE['Manual'] in src_list:
info_str += ' M '
else:
info_str += ' '
v = valid_arr[i]
if v ==0:
self.frameTreeView.item(tree[i],values=('Frame {}'.format(i+1),info_str),
image=self.warning_tkimg)
elif v==1:
self.frameTreeView.item(tree[i],values=('Frame {}'.format(i+1),info_str),
image=self.pass_tkimg)
else:
self.frameTreeView.item(tree[i],values=('Frame {}'.format(i+1),info_str),
image=self.empty_tkimg)
elif self.currHitTab=='ScaleSolve':
# fill in calParaLines->calPLListBox
#self.calPLListBox.delete(0, END)
# each paraline has 4 points
for lines in self.calParaLines:
temp_str = '{}:'.format(lines[0][0])
for j in range(1):
temp_str += '({},{})-({},{})-({})'.format(lines[j][1],lines[j][2],
lines[j][3],lines[j][4],lines[j][5])
if j==0:
temp_str +='\t'
self.calPLListBox.insert(END, temp_str)
# fill in calRefPoints ->calRPListBox
self.calRPListBox.delete(0, END)
for i, line in enumerate(self.calRefPoints):
temp_str = '{}: ({},{})-({},{})\t Distance={} \t KeyFrameNum={}'.format(
line[0], line[1], line[2], line[3], line[4], line[5],line[6])
self.calRPListBox.insert(END, temp_str)
# todo: fill result pose
elif self.currHitTab=='Road Reconstruction':
pass #no message box in this tab
def clean_draw_objects(self):
# TODO: clean all annotations, keep image
self.canvasPaintFrame.canvas.delete('dragRect')
self.canvasPaintFrame.canvas.delete('parallel_line')
self.canvasPaintFrame.canvas.delete('reference_line')
# self.canvasPaintFrame.canvas.delete('line')
self.canvasPaintFrame.canvas.delete('boundary_points')
self.show_trajectory(False)
def reset_modes(self, clean_results=False):
#self.calHlsBtn["text"] = "HLS Image"
self.hlsImageMode = False
self.calAddPLMode = True #EDIT_MODE
self.calAddEditMode = True #edit reference point
self.calEditPLRPBtn['state'] = 'disable'
self.calAddRPBtn['state'] = 'normal'
self.pauseMode = False
self.GT_Pose = None
self.GT_Position =None
#default button color
self.calEditPLRPBtn.configure(bg='#6495ed')
#self.modifyObjectBtn.configure(bg='#6495ed')
c = self.parent.cget('bg')
self.calAddRPBtn.configure(bg=c)
#self.calAddPLBtn.configure(bg=c)
#self.insertObjectBtn.configure(bg=c)
if clean_results:
#self.ObjectList = None
#self.ObjectList_pixel = None
self.cameraParam = None
#self.roadBoundPts = dict()
#self.roadLaneNum = {'ne':1, 'nw':1, 'wn':1, 'ws':1, 'sw':1, 'se':1,'es':1,'en':1}#default
self.calParaLines = [] # canvas_coords,group of 4 points
self.calRefPoints = [] # canvas_coords,group of 2 points
self.calParaLinesObjIds = []
self.calRefPointsObjIds = []
#self.trajectory = None
# --------tab1:-Calibration-----------
def show_hls(self):
if self.imgRaw is None:
return
self.clean_draw_objects()
self.hlsImageMode = not self.hlsImageMode
if self.hlsImageMode:
# self.calHlsBtn["text"] = "Original Image"
self.imgHLS = convert_hls_image(self.imgRaw)
# note: dim(self.imgHLS)== dim(self.imgRaw)
self.imgHLSDisp = resize_padding_image(
self.imgHLS, self.imgScale, self.imgPadding)
self.draw_image(self.imgHLSDisp)
else:
# self.calHlsBtn["text"] = "HLS Image"
self.draw_image(self.imgDisp)
self.draw_annotations()
def select_edit_add(self):
c = self.parent.cget('bg')
self.calAddEditMode = not self.calAddEditMode
if not self.calAddEditMode:
self.calEditPLRPBtn['state']='normal'
#self.calAddPLBtn['state'] = 'disable'
self.calAddRPBtn['state'] = 'disable'
else:
self.calEditPLRPBtn['state']='disable'
self.calAddRPBtn['state']='normal'
#self.calAddPLBtn['state']='normal'
self.calEditPLRPBtn.configure(bg='#6495ed')
#self.calAddPLBtn.configure(bg=c)
self.calAddRPBtn.configure(bg=c)
self.statusLabel.config(text = "Mode: Edit Lines")
for objs in self.calParaLinesObjIds:
for obj in objs:
obj.freeze(False)
for obj in self.calRefPointsObjIds:
obj.freeze(False)
if len(self.tempPLLine)>0:
self.tempPLLine[0].erase()
self.tempPLLine = []
def select_parallel_line(self):
if self.imgRaw is None:
return
c = self.parent.cget('bg')
self.calAddEditMode = False # not edit mode
self.calAddPLMode = True #insert parallel
self.calAddPLBtn['state'] = 'disable'
self.calEditPLRPBtn['state'] = 'normal'
self.calAddRPBtn['state']='normal'
self.calAddPLBtn.configure(bg='#6495ed')
self.calEditPLRPBtn.configure(bg=c)
self.calAddRPBtn.configure(bg=c)
self.calFrameID = self.currFrameId
self.statusLabel.config(text = "Mode: insert paralle lines")
for objs in self.calParaLinesObjIds:
for obj in objs:
obj.freeze(True)
for obj in self.calRefPointsObjIds:
obj.freeze(True)
def select_ref_points(self):
if self.imgRaw is None:
return
c = self.parent.cget('bg')
self.calAddEditMode = False # not edit mode
self.calAddPLMode = False #insert parallel
#self.calAddPLBtn['state'] = 'normal'
self.calEditPLRPBtn['state'] = 'normal'
self.calAddRPBtn['state']='disable'
self.calAddRPBtn.configure(bg='#6495ed')
self.calEditPLRPBtn.configure(bg=c)
#self.calAddPLBtn.configure(bg=c)
self.calFrameID = self.currFrameId
self.statusLabel.config(text = "Mode: insert reference points")
for objs in self.calParaLinesObjIds:
for obj in objs:
obj.freeze(True)
for obj in self.calRefPointsObjIds:
obj.freeze(True)
if len(self.tempPLLine)>0: #click when 1 line is draw, another is not drw
self.tempPLLine[0].erase()
self.tempPLLine = []
def clean_cal_selections(self): # re-select
self.clean_draw_objects()
self.calParaLines = []
self.calRefPoints = []
self.tempPLLine = []
self.tempDragData = None # drag boundary points
self.update_message_boxes()
return
def cam_calib(self):
if len(self.calRefPoints) < 1:
messagebox.showinfo(
"Info", "Please select at least 1 reference line")
return
SegNodes = []
NodeDist = []
Pose_list = []
self.scale_solver = CamCal()
width_scale = 1
height_scale = 1
for ref_line in self.calRefPoints:
print(ref_line)
for i in range(2):
canvas_x, canvas_y = ref_line[1 + 2 * i], ref_line[1 + 2 * i + 1]
pixel_x, pixel_y = get_pixel_coordinate(canvas_x, canvas_y, self.imgScale, self.imgPadding)
SegNodes.append(np.array([pixel_x * width_scale, pixel_y * height_scale]))
NodeDist.append([float(ref_line[5])])
print(ref_line[-1])
Pose_list.append(self.kf_pose_data[ref_line[-1]])
#print(SegNodes)
#print(NodeDist)
#print(Pose_list)
self.scale_solver.process(SegNodes, NodeDist,Pose_list)
print(self.scale_solver.CamParam.Scale_X, self.scale_solver.CamParam.Scale_Z, self.scale_solver.CamParam.Ty)
'''
self.cameraParam = camera_clibration_canvas(self.calParaLines, self.calRefPoints,
(self.imgRaw.shape[1],
self.imgRaw.shape[0]),
self.imgScale, self.imgPadding)
'''
self.update_message_boxes()
#####show the result map#####
position_list=np.zeros((2,len(self.kf_pose_data)))
for i in range(len(self.kf_pose_data)):
position_list[0, i]=self.kf_pose_data[i][0,3]*self.scale_solver.CamParam.Scale_X
position_list[1, i] = self.kf_pose_data[i][2, 3] * self.scale_solver.CamParam.Scale_Z
#t = np.arange(0, 3, .01)
#self.Mapfig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t), 'o')
self.ax_map.plot(position_list[0,:],position_list[1,:],'o')
#self.ax_map.scatter(t, 2 * np.sin(2 * np.pi * t), color='g')
#self.ax_map.scatter(t, 2 * np.cos(2 * np.pi * t), color='blue')
self.ax_map.legend(['GT_map','scaled_map'])
self.MapCanvas.draw()
def calib_save_results(self):
result_saver = os.path.join(os.path.dirname(self.videoFilename), 'ScaleSolver_result.txt')
with open(result_saver, 'w+') as f:
f.write('ScaleX: ' + str(self.scale_solver.CamParam.Scale_X[0]) + '\n')
f.write('ScaleZ: ' + str(self.scale_solver.CamParam.Scale_Z[0]) + '\n')
f.write('ScaleY: ' + str(self.scale_solver.CamParam.Ty[0]) + '\n')
def load_calib_results(self):
path = self.videoFilename+'_camcalib.pkl'
if not os.path.exists(path):
return
raw = pickle.load(open(path, 'rb'))
self.calFrameID = raw[0]
paraline_pixels = raw[1]
refpts_pixels = raw[2]
self.cameraParam = raw[3]
self.calParaLines = paraline_pixels
for i, item in enumerate(paraline_pixels):
for j, line in enumerate(item):
pixel_coords=[[line[1], line[2],line[3], line[4]]]
canvas_coords = convert_pixel_to_canvas(pixel_coords, self.imgScale, self.imgPadding)[0]
self.calParaLines[i][j][1] = canvas_coords[0]
self.calParaLines[i][j][2] = canvas_coords[1]
self.calParaLines[i][j][3] = canvas_coords[2]
self.calParaLines[i][j][4] = canvas_coords[3]
self.calRefPoints = refpts_pixels
for i, item in enumerate(refpts_pixels):
pixel_coords=[[item[1], item[2], item[3], item[4]]]
canvas_coords = convert_pixel_to_canvas(pixel_coords, self.imgScale, self.imgPadding)[0]
self.calRefPoints[i][1] = canvas_coords[0]
self.calRefPoints[i][2] = canvas_coords[1]
self.calRefPoints[i][3] = canvas_coords[2]
self.calRefPoints[i][4] = canvas_coords[3]
def calib_disp_results(self, canvas_coord=None):
if self.imgRaw is None:
return
self.animationDisplayCanvas.delete('all')
if SHOW_ZOOMED_FOCUS: #show cropped enlarged image, center on mouse
try:
pixel_coord = convert_canvas_to_pixel([canvas_coord], self.imgScale, self.imgPadding)[0]
raw_img_arr = copy.deepcopy(self.imgRaw)
raw_img_pil = Image.fromarray(cv2.cvtColor(raw_img_arr, cv2.COLOR_BGR2RGB))
x,y = pixel_coord[0], pixel_coord[1]
margin = 30
zoom_img_crop = raw_img_pil.crop(((x - margin), (y - margin), (x + margin), (y + margin)))
zoom_img_crop = zoom_img_crop.resize((SMALL_CANVAS_SIZE, SMALL_CANVAS_SIZE))
self.imgZoomTk = ImageTk.PhotoImage(zoom_img_crop)
self.animationDisplayCanvas.create_image(0, 0, image=self.imgZoomTk, anchor=NW)
#hline
self.animationDisplayCanvas.create_line(0, SMALL_CANVAS_SIZE/2,
SMALL_CANVAS_SIZE, SMALL_CANVAS_SIZE/2,
width=2)
#vline
self.animationDisplayCanvas.create_line(SMALL_CANVAS_SIZE/2, 0,
SMALL_CANVAS_SIZE/2, SMALL_CANVAS_SIZE,
width=2)
except:
pass
else:
if self.cameraParam is None:
return
fpitch = self.calPitchScale.get()
froll = self.calRollScale.get()
fyaw = self.calYawScale.get()
#note the perspective image is a square image, different resolution with raw image
self.imgPerspectiveTk, self.cameraParm = convert_perspective_image(self.imgRaw,
froll, fpitch, fyaw, self.cameraParam,
canvas_size=SMALL_CANVAS_SIZE)
self.animationDisplayCanvas.create_image(0,0,image=self.imgPerspectiveTk,
anchor='nw')
def show_trajectory(self, show=True):
if not show:
self.canvasPaintFrame.canvas.delete('trajectory')
return
bshow_traj = self.enableTrajDisp.get()
if (self.ObjectList is None) or (self.trajectory is None):
return
if bshow_traj==1:
curr_objs = self.ObjectList[self.currFrameId]
if len(curr_objs)<=0:
return
for i, obj in enumerate(curr_objs):
pts = select_trajectory(self.trajectory, obj[0], self.currFrameId)
alpha_color = self.dragRectObjs[i].fill_color
x_prev, y_prev = 0, 0
for j, pt in enumerate(pts):
x,y = pt[0],pt[1]
self.canvasPaintFrame.canvas.create_oval(x - 3, y - 3, x + 3, y + 3,
fill=alpha_color, tag='trajectory')
if j >=1:
self.canvasPaintFrame.canvas.create_line(x_prev, y_prev, x, y,
fill=alpha_color, width=1, tag='trajectory')
x_prev, y_prev = x,y
else:
self.canvasPaintFrame.canvas.delete('trajectory')
# --------Mouse event capture -----------
def edit_reference_points(self, event):
#todo , calRefPoints
w = View_AskDistance(self.parent) # canvasPaintFrame)
self.parent.wait_window(w.top)
if w.value:
widget = event.widget
selection = widget.curselection()
#value = widget.get(selection[0])
self.calRefPoints[selection[0]][-1] = float(w.value)
self.update_message_boxes()
def edit_reference_points_keypress(self, event):
#print( "You pressed"), print(repr(event.char))
c = repr(event.char)
# print(c)
if c == "'\\x7f'": # delete press
result = messagebox.askquestion(
"Delete", "Are You Sure?", icon='warning')
if result == 'yes':
widget = event.widget
selection = widget.curselection()
del self.calRefPoints[selection[0]]
self.calRefPointsObjIds[selection[0]].erase()
del self.calRefPointsObjIds[selection[0]]
self.update_message_boxes()
#print("ref points"), print(self.calRefPoints)
def edit_parallel_line_keypress(self, event):
c = repr(event.char)
if c == "'\\x7f'": # delete press
result = messagebox.askquestion(
"Delete", "Are You Sure?", icon='warning')
if result == 'yes':
widget = event.widget
selection = widget.curselection()
del self.calParaLines[selection[0]]
for line in self.calParaLinesObjIds[selection[0]]:
line.erase()
del self.calParaLinesObjIds[selection[0]]
self.update_message_boxes()
def mouse_move(self, event):
if not self.dragRectObjs is None:
for i in range(len(self.dragRectObjs)):
self.dragRectObjs[i].highlight(False)
if SHOW_ZOOMED_FOCUS:
self.calib_disp_results(canvas_coord=[event.x, event.y])
def mouse_click(self, event):
if self.imgRaw is None:
return
if self.currHitTab =='Detect&Track':# only work in detection tab
if self.InsertObjectMode: #only work in insert mode
x = self.canvasPaintFrame.canvas.canvasx(event.x)
y = self.canvasPaintFrame.canvas.canvasy(event.y)
oval_id1 = self.canvasPaintFrame.canvas.create_oval(x - RADIUS, y - RADIUS,
x + RADIUS, y + RADIUS,
fill='red', tags='temp_oval')
oval_id2 = self.canvasPaintFrame.canvas.create_oval(x - RADIUS, y - RADIUS,
x + RADIUS, y + RADIUS,
fill='red', tags='temp_oval')
rect_id = self.canvasPaintFrame.canvas.create_rectangle(x,y,x,y, width=2,
fill='', outline='red',
tags='temp_rect')
self.tempInsertObjectData = dict()
self.tempInsertObjectData["startpoint"] = oval_id1
self.tempInsertObjectData["startx"] = x
self.tempInsertObjectData["starty"] = y
self.tempInsertObjectData["x"] = x
self.tempInsertObjectData["y"] = y
self.tempInsertObjectData["endpoint"] = oval_id2
self.tempInsertObjectData["rect"] = rect_id
else:
self.tempInsertObjectData = dict() #clean
elif self.currHitTab =='ScaleSolve':
if self.calAddPLMode and not self.calAddEditMode:
x = self.canvasPaintFrame.canvas.canvasx(event.x)
y = self.canvasPaintFrame.canvas.canvasy(event.y)
oval_id1 = self.canvasPaintFrame.canvas.create_oval(x - RADIUS, y - RADIUS,
x + RADIUS, y + RADIUS,
fill='red', tags='temp_oval')
oval_id2 = self.canvasPaintFrame.canvas.create_oval(x - RADIUS, y - RADIUS,
x + RADIUS, y + RADIUS,
fill='red', tags='temp_oval')
line_id = self.canvasPaintFrame.canvas.create_line(x,y,x,y, width=2,
fill='red',
tags='temp_line')
self.tempParallelData = dict()
self.tempParallelData["startpoint"] = oval_id1
self.tempParallelData["startx"] = x
self.tempParallelData["starty"] = y
self.tempParallelData["x"] = x
self.tempParallelData["y"] = y
self.tempParallelData["endpoint"] = oval_id2
self.tempParallelData["line"] = line_id
elif not self.calAddEditMode and not self.calAddEditMode:
x = self.canvasPaintFrame.canvas.canvasx(event.x)
y = self.canvasPaintFrame.canvas.canvasy(event.y)
oval_id1 = self.canvasPaintFrame.canvas.create_oval(x - RADIUS, y - RADIUS,
x + RADIUS, y + RADIUS,
fill='yellow', tags='temp_oval')
oval_id2 = self.canvasPaintFrame.canvas.create_oval(x - RADIUS, y - RADIUS,
x + RADIUS, y + RADIUS,
fill='yellow', tags='temp_oval')
line_id = self.canvasPaintFrame.canvas.create_line(x,y,x,y, width=2,
fill='yellow',
tags='temp_line')
self.tempRefPointData = dict()
self.tempRefPointData["startpoint"] = oval_id1
self.tempRefPointData["startx"] = x
self.tempRefPointData["starty"] = y
self.tempRefPointData["x"] = x
self.tempRefPointData["y"] = y
self.tempRefPointData["endpoint"] = oval_id2
self.tempRefPointData["line"] = line_id
elif self.currHitTab =='Road Reconstruction':# only work in detection tab
x = self.canvasPaintFrame.canvas.canvasx(event.x)
y = self.canvasPaintFrame.canvas.canvasy(event.y)
if self.SamplesFlag_N:
oval_id1 = self.canvasPaintFrame.canvas.create_oval(x - 3, y - 3,
x + 3, y + 3,
fill='blue', tags='temp_oval')
self.tempRoadData=[x,y]
elif self.SamplesFlag_W:
oval_id1 = self.canvasPaintFrame.canvas.create_oval(x - 3, y - 3,
x + 3, y + 3,
fill='yellow', tags='temp_oval')
self.tempRoadData=[x,y]
elif self.SamplesFlag_S:
oval_id1 = self.canvasPaintFrame.canvas.create_oval(x - 3, y - 3,
x + 3, y + 3,
fill='green', tags='temp_oval')
self.tempRoadData=[x,y]
elif self.SamplesFlag_E:
oval_id1 = self.canvasPaintFrame.canvas.create_oval(x - 3, y - 3,
x + 3, y + 3,
fill='red', tags='temp_oval')
self.tempRoadData=[x,y]
def mouse_drag(self, event):
if self.currHitTab =='Detect&Track':# only work in detection tab
if self.calAddPLMode and bool(self.tempInsertObjectData): #only work in insert mode
x = self.canvasPaintFrame.canvas.canvasx(event.x)
y = self.canvasPaintFrame.canvas.canvasy(event.y)
delta_x = event.x - self.tempInsertObjectData["x"]