-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual_camera_labeling.py
1216 lines (909 loc) · 41.9 KB
/
manual_camera_labeling.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 cv2
import time
import os
import numpy as np
import csv
import string
import re
import copy
import _pickle as pickle
import torch
import PyNvCodec as nvc
import PytorchNvCodec as pnvc
import torchvision.transforms.functional as F
from scipy.spatial import ConvexHull
import itertools
import sys
from transform_compute import compute_correspondences,get_all_dash_points
def line_to_point(line,point):
"""
Given a line defined by two points, finds the distance from that line to the third point
line - (x0,y0,x1,y1) as floats
point - (x,y) as floats
Returns
-------
distance - float >= 0
"""
numerator = np.abs((line[2]-line[0])*(line[1]-point[1]) - (line[3]-line[1])*(line[0]-point[0]))
denominator = np.sqrt((line[2]-line[0])**2 +(line[3]-line[1])**2)
return numerator / (denominator + 1e-08)
def find_vanishing_point(lines):
"""
Finds best (L2 norm) vanishing point given a list of lines
Parameters
----------
lines : [(x0,y0,x1,y1), ...]
Returns
-------
vp - (x,y)
"""
# mx+b form
#y0 = ax + c
#y1 = bx + d
line0 = lines[0]
line1 = lines[1]
a = (line0[3] - line0[1])/line0[2] - line0[0]
b = (line1[3] - line1[1])/line1[2] - line1[0]
c = line0[1] - a*line0[0]
d = line1[1] - c*line1[0]
# intersection
px = (d-c)/(a-b)
py = a*(d-c)/(a-b) + c
best_dist = np.inf
# using intersection as starting point, grid out a grid of 11 x 11 points with spacing g
g = 1e+16
n_pts = 31
while g > 1:
#print("Gridding at g = {}".format(g))
# create grid centered around px,py with spacing g
x_pts = np.arange(px-g*(n_pts//2),px+g*(n_pts//2),g)
y_pts = np.arange(py-g*(n_pts//2),py+g*(n_pts//2),g)
for x in x_pts:
for y in y_pts:
# for each point in grid, compute average distance to vanishing point
dist = 0
for line in lines:
dist += line_to_point(line,(x,y))**2
# keep best point in grid
if dist < best_dist:
px = x
py = y
best_dist = dist
#print("Best vp so far: ({},{}), with average distance {}".format(px,py,np.sqrt(dist/len(lines))))
# regrid
g = g / 10.0
return [px,py]
def average_frame(sequence):
frames = []
resize = None # (1920,1080)
gpuID = 0
nvDec = nvc.PyNvDecoder(sequence, gpuID)
target_h, target_w = nvDec.Height(), nvDec.Width()
to_rgb = nvc.PySurfaceConverter(nvDec.Width(), nvDec.Height(
), nvc.PixelFormat.NV12, nvc.PixelFormat.RGB, gpuID)
to_planar = nvc.PySurfaceConverter(nvDec.Width(), nvDec.Height(
), nvc.PixelFormat.RGB, nvc.PixelFormat.RGB_PLANAR, gpuID)
cspace, crange = nvDec.ColorSpace(), nvDec.ColorRange()
if nvc.ColorSpace.UNSPEC == cspace:
cspace = nvc.ColorSpace.BT_601
if nvc.ColorRange.UDEF == crange:
crange = nvc.ColorRange.MPEG
cc_ctx = nvc.ColorspaceConversionContext(cspace, crange)
count = 0
avg_frame = None
# get frames from one file
while True:
if count % 1000 == 0:
print("On frame {} for sequence {}".format(count, sequence))
pkt = nvc.PacketData()
# Obtain NV12 decoded surface from decoder;
raw_surface = nvDec.DecodeSingleSurface(pkt)
if raw_surface.Empty():
break
# Convert to RGB interleaved;
rgb_byte = to_rgb.Execute(raw_surface, cc_ctx)
# Convert to RGB planar because that's what to_tensor + normalize are doing;
rgb_planar = to_planar.Execute(rgb_byte, cc_ctx)
# likewise, end of video file
if rgb_planar.Empty():
break
# Create torch tensor from it and reshape because
# pnvc.makefromDevicePtrUint8 creates just a chunk of CUDA memory
# and then copies data from plane pointer to allocated chunk;
surfPlane = rgb_planar.PlanePtr()
surface_tensor = pnvc.makefromDevicePtrUint8(surfPlane.GpuMem(), surfPlane.Width(
), surfPlane.Height(), surfPlane.Pitch(), surfPlane.ElemSize())
surface_tensor.resize_(3, target_h, target_w)
if resize is not None:
try:
surface_tensor = torch.nn.functional.interpolate(
surface_tensor.unsqueeze(0), resize).squeeze(0)
except:
raise Exception(
"Surface tensor shape:{} --- resize shape: {}".format(surface_tensor.shape, resize))
# This is optional and depends on what you NN expects to take as input
# Normalize to range desired by NN. Originally it's
surface_tensor = surface_tensor.type(
dtype=torch.cuda.FloatTensor)
# apply normalization
#surface_tensor = F.normalize(surface_tensor,mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
if avg_frame is None:
avg_frame = surface_tensor
else:
avg_frame += surface_tensor
count += 1
# if count > 100:
# break
if count % 100 == 0:
frames.append(surface_tensor.permute(1, 2, 0).data.cpu().numpy()[:,:,::-1])
avg_frame /= count
avg_frame = avg_frame.permute(1, 2, 0).data.cpu().numpy()
return avg_frame, frames
def poly_area(polygon):
"""
Returns the area of the polygon
polygon - [n_vertices,2] tensor of clockwise points
"""
x1 = polygon[:,0]
y1 = polygon[:,1]
x2 = x1.roll(1)
y2 = y1.roll(1)
# per this formula: http://www.mathwords.com/a/area_convex_polygon.htm
area = -1/2.0 * (torch.sum(x1*y2) - torch.sum(x2*y1))
return area
def get_hull(points, indices=False):
hull = ConvexHull(points.clone().detach()).vertices.astype(int)
if indices:
return hull
points = points[hull, :]
return points
def clockify(polygon, clockwise=True, hull=False, center = None):
"""
polygon - [n_vertices,2] tensor of x,y,coordinates for each convex polygon
clockwise - if True, clockwise, otherwise counterclockwise
returns - [n_vertices,2] tensor of sorted coordinates
"""
relist = False
if type(polygon) == list:
relist = True
polygon = torch.stack([torch.tensor(pt) for pt in polygon]).float()
# get center
if center is None:
center = torch.mean(polygon, dim=0)
# get angle to each point from center
diff = polygon - center.unsqueeze(0).expand([polygon.shape[0], 2])
tan = torch.atan(diff[:, 1]/diff[:, 0])
direction = (torch.sign(diff[:, 0]) - 1)/2.0 * -np.pi
angle = tan + direction
sorted_idxs = torch.argsort(angle)
if not clockwise:
sorted_idxs.reverse()
polygon = polygon[sorted_idxs.detach(), :]
if hull:
polygon = get_hull(polygon)
if relist:
polygon = polygon.int()
polygon = [(row[0].item(), row[1].item()) for row in polygon]
return polygon
# define annotator
class CameraAnnotator:
def __init__(self, frame, frame_list = None, cam_name="UNKNOWN", save_directory=None, load = False):
self.frame_list = frame_list
self.frame_list_idx = 0
self.im = (frame).astype(np.uint8)
self.cam_name = cam_name
if save_directory is not None:
self.save_file = os.path.join(save_directory, self.cam_name) + ".cpkl"
else:
self.save_file = None
self.pos = (0, 0)
self.new = False
self.moved = False
self.SHOW_LABELS = 1
self.SHOW_VP = True
self.DIRECTION = "EB"
self.ACTIVE = "VP"
self.clicked_point = None
self.save_dir = save_dir
self.temp_pts = None
self.text_color = (0,0,0)
self.active_text = ""
self.active_feature = "lane1"
self.active_curve_feature = "yellow"
self.active_index = 0
self.active_curve_index = 0
self.active_letter = 0
self.letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","aa","bb","cc","dd","ee","ff","gg","hh"]
self.guess_text = "{}_{}_{}_{}".format(self.DIRECTION.lower(),self.active_feature,self.active_index,self.letters[self.active_letter])
self.active_curve = "{}_{}_{}".format(self.DIRECTION.lower(),self.active_curve_feature,self.active_curve_index)
# each list element will be (row,column,name)
self.data = {
"WB": {
"curves": [],
"points": [],
"FOV": [],
"mask": [],
"vp":None,
"z_vp":None
},
"EB": {
"curves": [],
"points": [],
"FOV": [],
"mask": [],
"vp":None,
"z_vp":None
}
}
self.undo_cache = [copy.deepcopy(self.data.copy)]
if load:
self.load()
self.plot()
self.vp_cache = []
self.z_cache = []
self.corr = {}
def save(self):
try:
with open(self.save_file, "wb") as f:
pickle.dump(self.data, f)
print("Saved annotations at {}".format(self.save_file))
except:
print("Invalid save file, unable to save")
def load(self):
try:
with open(self.save_file, "rb") as f:
self.data = pickle.load(f)
self.undo_cache.append(copy.deepcopy(self.data))
except:
print("Invalid save file, unable to save")
def get_correspondence(self):
self.save()
corr = compute_correspondences([self.cam_name],direction = self.DIRECTION,ADD_PROJ = False)
self.corr[self.DIRECTION] = corr["{}_{}".format(self.cam_name,self.DIRECTION)]
def on_mouse(self, event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
self.clicked_point = (x, y)
self.new = True
if event == cv2.EVENT_LBUTTONUP and self.ACTIVE == "Z AXIS":
#height = float(self.keyboard_input(update_plot = True))
height = 14
ln = [self.clicked_point[0], self.clicked_point[1],x,y,height]
self.z_cache.append(ln)
self.undo_cache.append(copy.deepcopy(self.data))
self.clicked_point = None
self.new = True
cv2.waitKey(1)
if event == cv2.EVENT_RBUTTONDOWN:
self.new = True
elif event == cv2.EVENT_MOUSEMOVE:
self.pos = (x, y)
self.moved = True
def plot_space_pts(self,im):
"""
Projects 3D space points into image/correspondence using P:
new_pts = P x points T ---> [dm,3] T = [3,4] x [4,dm]
performed by flattening batch dimension d and object point dimension m together
points - [d,m,3] array of points in 3-space
"""
try:
corr = self.corr[self.DIRECTION]["P"]
except:
return []
points = torch.stack([torch.tensor(self.corr[self.DIRECTION]["state_plane_pts"][0]),
torch.tensor(self.corr[self.DIRECTION]["state_plane_pts"][1]),
torch.zeros(len(self.corr[self.DIRECTION]["state_plane_pts"][1]))]).transpose(1,0)
d = points.shape[0]
# convert points into size [dm,4]
points = points.view(-1,3)
points = torch.cat((points.double(),torch.ones([points.shape[0],1],device = points.device).double()),1) # add 4th row
points = torch.transpose(points,0,1).double()
P = torch.from_numpy(corr).double().to(points.device)
new_pts = torch.matmul(P,points).transpose(0,1)
# divide each point 0th and 1st column by the 2nd column
new_pts[:,0] = new_pts[:,0] / new_pts[:,2]
new_pts[:,1] = new_pts[:,1] / new_pts[:,2]
# drop scale factor column
new_pts = new_pts[:,:2]
# reshape to [d,m,2]
new_pts = new_pts.view(d,-1,2).squeeze()
for pt in new_pts:
pt = int(pt[0].item()), int(pt[1].item())
im = cv2.circle(im,pt,3,(0,255,0),-1)
return im
def plot(self):
self.cur_image = self.im.copy()
if self.ACTIVE == "Z AXIS" and self.frame_list is not None:
self.cur_image = (self.frame_list[self.frame_list_idx].copy()).astype(np.uint8)
text_block = [
"Camera: {}".format(self.cam_name),
"Active Direction: {}".format(self.DIRECTION),
"Active Command: {}".format(self.ACTIVE),
"",
"COMMANDS:",
"q: save and quit",
"1: toggle mode",
"=: toggle direction",
"t: toggle text labels",
"u: undo",
"!: load saved file",
"f: FOV command",
"p: POINT command",
"m: MASK command",
"c: CURVE command",
"s: save",
"a: AUTO POINTS command"
]
try:
self.corr[self.DIRECTION]
self.cur_image = self.plot_space_pts(self.cur_image)
if self.SHOW_VP:
# project self.pos up by 10 feet and backproject, then plot line
pt = torch.tensor([self.pos[0],self.pos[1],1]).unsqueeze(0).transpose(1,0)
P = torch.from_numpy(self.corr[self.DIRECTION]["P"])
H = torch.from_numpy(self.corr[self.DIRECTION]["H"])
new_pts = torch.matmul(H,pt.double()).transpose(0,1)
new_pts[:,0] = new_pts[:,0] / new_pts[:,2]
new_pts[:,1] = new_pts[:,1] / new_pts[:,2]
new_pts = new_pts[:,:2]
new_pts = torch.cat((new_pts.transpose(0,1),14+torch.zeros([1,1]), torch.ones([1,1])))
new_pts = torch.matmul(P,new_pts).transpose(0,1)
new_pts[:,0] = new_pts[:,0] / new_pts[:,2]
new_pts[:,1] = new_pts[:,1] / new_pts[:,2]
top_pt = int(new_pts[0,0].item()), int(new_pts[0,1].item())
cv2.line(self.cur_image,top_pt,self.pos,(0,0,255),2)
except:
# show vp
vp = self.data[self.DIRECTION]["vp"]
if vp is not None and self.SHOW_VP:
cv2.line(self.cur_image,(int(vp[0]),int(vp[1])),self.pos,(0,0,0),1)
# show z vp
z_vp = self.data[self.DIRECTION]["z_vp"]
if z_vp is not None and self.SHOW_VP:
cv2.line(self.cur_image,(int(z_vp[0]),int(z_vp[1])),self.pos,(0,0,255),1)
# if FOV is active, display FOV
if self.ACTIVE == "FOV" or self.ACTIVE == "MASK":
FOV_poly = self.data[self.DIRECTION]["FOV"]
if len(FOV_poly) > 2:
FOV_poly = np.stack([np.array(pt) for pt in FOV_poly]).reshape(
1, -1, 2).astype(np.int32)
transparency = (np.ones(self.im.shape)*0.5)
transparency = cv2.fillPoly(
transparency, FOV_poly, (1, 0.8, 0.8), lineType=cv2.LINE_AA)
self.cur_image = (transparency.astype(
float) * self.cur_image.astype(float)).astype(np.uint8)
# # if mask is active, display mask
# elif self.ACTIVE == "MASK":
mask_poly = self.data[self.DIRECTION]["mask"]
if len(mask_poly) > 2:
mask_poly = np.stack([np.array(pt) for pt in mask_poly]).reshape(
1, -1, 2).astype(np.int32)
transparency = (np.ones(self.im.shape))
transparency[:, :, 0:2] = 0.8
transparency = cv2.fillPoly(
transparency, mask_poly, (1, 1, 1), lineType=cv2.LINE_AA)
self.cur_image = (transparency.astype(
float) * self.cur_image.astype(float)).astype(np.uint8)
if self.ACTIVE == "Z AXIS":
for line in self.z_cache:
self.cur_image = cv2.line(self.cur_image,(int(line[0]),int(line[1])),(int(line[2]),int(line[3])),(0,0,255),1)
# display preliminary info in corner unless mouse_position is in that region
font = cv2.FONT_HERSHEY_SIMPLEX
scale = 1
if self.pos[0] > 400 or self.pos[1] > (3+len(text_block))*30:
for ridx, row in enumerate(text_block):
self.cur_image = cv2.putText(
self.cur_image, row, (10, (ridx+2)*30), font, scale, (255, 255, 255), 1)
# if show_labels, show labels
# regardless, show current label if a label is currently being typed
if self.clicked_point is not None and self.ACTIVE == "POINTS":
if len(self.active_text) > 0:
self.cur_image = cv2.putText(
self.cur_image, " " + self.active_text, self.clicked_point, font, self.SHOW_LABELS, self.text_color, 1)
else:
self.cur_image = cv2.putText(
self.cur_image, " " + self.guess_text, self.clicked_point, font, self.SHOW_LABELS, (100,100,100), 1)
self.cur_image = cv2.circle(
self.cur_image, self.clicked_point, 1, (100, 0, 100), -1)
self.cur_image = cv2.circle(
self.cur_image, self.clicked_point, 4, (100, 0, 100), 1)
elif self.clicked_point is not None and self.ACTIVE == "DELETE":
if len(self.active_text) > 0:
self.cur_image = cv2.putText(
self.cur_image, " " + self.active_text, self.clicked_point, font, self.SHOW_LABELS, self.text_color, 1)
else:
self.cur_image = cv2.putText(
self.cur_image, " " + self.guess_text, self.clicked_point, font, self.SHOW_LABELS, (100,100,100), 1)
elif self.temp_pts is not None and self.ACTIVE == "AUTO POINTS":
if len(self.active_text) > 0:
self.cur_image = cv2.putText(
self.cur_image, " " + self.active_text, self.clicked_point, font, 1, self.text_color, 1)
else:
self.cur_image = cv2.putText(
self.cur_image, " " + self.guess_text, self.clicked_point, font, 1, (100,100,100), 1)
for pidx, point in enumerate(self.temp_pts):
self.cur_image = cv2.circle(
self.cur_image, point, 1, (100, 0, 100), -1)
self.cur_image = cv2.circle(
self.cur_image, point, 4, (100, 0, 100), 1)
self.cur_image = cv2.putText(self.cur_image,self.letters[pidx],point,font,0.5,(100,0,100),1)
# regardless, show current label if a label is currently being typed
elif self.clicked_point is not None and self.ACTIVE == "CURVE":
if len(self.active_text) > 0:
self.cur_image = cv2.putText(
self.cur_image, " " + self.active_text, self.clicked_point, font, 1, self.text_color, 1)
elif self.active_curve is not None:
self.cur_image = cv2.putText(
self.cur_image, " " + self.active_curve, self.clicked_point, font, 1, (100,100,100), 1)
else:
self.cur_image = cv2.putText(
self.cur_image, " " + self.guess_text, self.clicked_point, font, 1, (100,100,100), 1)
self.cur_image = cv2.circle(
self.cur_image, self.clicked_point, 1, (100, 0, 100), -1)
self.cur_image = cv2.circle(
self.cur_image, self.clicked_point, 4, (100, 0, 100), 1)
elif self.clicked_point is not None and self.ACTIVE == "Z AXIS":
if len(self.active_text) > 0:
self.cur_image = cv2.putText(
self.cur_image, " " + self.active_text, self.clicked_point, font, 1, self.text_color, 1)
# plot points
for point in self.data[self.DIRECTION]["points"]:
pt = (int(point[0]), int(point[1]))
if self.SHOW_LABELS:
self.cur_image = cv2.putText(
self.cur_image, " " + point[2], pt, font, self.SHOW_LABELS, self.text_color, 1)
self.cur_image = cv2.circle(self.cur_image, pt, 1, (255, 0, 0), -1)
self.cur_image = cv2.circle(self.cur_image, pt, 4, (255, 0, 0), 1)
# plot curve points
for point in self.data[self.DIRECTION]["curves"]:
pt = (int(point[0]), int(point[1]))
if self.SHOW_LABELS:
self.cur_image = cv2.putText(
self.cur_image, " " + point[2], pt, font, self.SHOW_LABELS, self.text_color, 1)
self.cur_image = cv2.circle(self.cur_image, pt, 1, (0, 255, 255), -1)
self.cur_image = cv2.circle(self.cur_image, pt, 4, (0, 255, 255), 1)
# END
def impute_points(self):
# ensure that homgraphy exists
try:
corr = self.corr[self.DIRECTION]["P"]
except: return
#get all aerial points
ae_x,ae_y, ae_id = get_all_dash_points(self.DIRECTION)
# get FOV pixel extents
x_min = 3840
x_max = 0
y_min = 2160
y_max = 0
for point in self.data[self.DIRECTION]["FOV"]:
if point[0] < x_min:
x_min = point[0]
if point[0] > x_max:
x_max = point[0]
if point[1] < y_min:
y_min = point[1]
if point[1] > y_max:
y_max = point[1]
# transform to image
points = torch.stack([torch.tensor(ae_x),
torch.tensor(ae_y),
torch.zeros(len(ae_x))]).transpose(1,0)
d = points.shape[0]
# convert points into size [dm,4]
points = points.view(-1,3)
points = torch.cat((points.double(),torch.ones([points.shape[0],1],device = points.device).double()),1) # add 4th row
points = torch.transpose(points,0,1).double()
P = torch.from_numpy(corr).double().to(points.device)
new_pts = torch.matmul(P,points).transpose(0,1)
# divide each point 0th and 1st column by the 2nd column
new_pts[:,0] = new_pts[:,0] / new_pts[:,2]
new_pts[:,1] = new_pts[:,1] / new_pts[:,2]
# drop scale factor column
new_pts = new_pts[:,:2]
# reshape to [d,m,2]
new_pts = new_pts.view(d,-1,2).squeeze()
# remove all dashes with points outside of frame
dash_dict = {}
for i in range(len(ae_id)):
if new_pts[i,0] > x_min and new_pts[i,1] > y_min and new_pts[i,0] < x_max and new_pts[i,1] < y_max:
trunc_id = ae_id[i][:-2]
try: dash_dict[trunc_id].append(new_pts[i])
except: dash_dict[trunc_id] = [new_pts[i]]
# for remaining points, get all sets of 4 dash points,
for key in dash_dict:
if len(dash_dict[key]) == 4:
# get average point for each set
avg = sum(dash_dict[key]) / len(dash_dict[key])
DUP = False
for existing_point in self.data[self.DIRECTION]["points"]:
if key in existing_point[2]:
DUP = True
break
if DUP: continue
try:
# for each average point, run auto_points
self.clicked_point = avg.data.numpy().astype(int)
points = self.auto_points()
if points is None:
p_idx = 0
while points is None and p_idx < 4:
self.clicked_point = dash_dict[key][p_idx].data.numpy().astype(int)
points = self.auto_points()
p_idx += 1
except:
continue
if points is not None:
self.active_letter = 0
# if successful, seed that dashed line with corresponding name
for pt in points:
new_name = "{}_{}_{}".format(self.DIRECTION.lower(),key,self.letters[self.active_letter])
self.data[self.DIRECTION]["points"].append([pt[0], pt[1], new_name])
self.active_letter += 1
# append to undo buffer
self.undo_cache.append(copy.deepcopy(self.data))
if len(self.undo_cache) > 100:
del self.undo_cache[0]
# show
self.plot()
cv2.imshow("window", self.cur_image)
cv2.setWindowTitle("window", self.cam_name)
cv2.waitKey(1)
# increment default point
self.active_letter = 0
self.clicked_point = None
def keyboard_input(self, update_plot=False):
keys = self.guess_text if self.ACTIVE != "CURVE" else self.active_curve
if self.ACTIVE == "Z AXIS": keys = ""
letters = string.ascii_lowercase + string.digits + string.punctuation
while not self.new:
if update_plot:
self.plot()
cv2.imshow("window", self.cur_image)
key = cv2.waitKey(1)
for letter in letters:
if key == ord(letter):
keys = keys + letter
if key == ord("\b"):
keys = keys[:-1]
if key == ord("\n") or key == ord("\r"):
break
self.active_text = keys
#self.active_text = ""
#if len(keys) == 0 and self.ACTIVE == "CURVE": keys = self.active_curve
#if len(keys) == 0: keys = self.guess_text
return keys
def click_handler(self):
if self.ACTIVE == "FOV":
self.data[self.DIRECTION]["FOV"].append(self.clicked_point)
if len(self.data[self.DIRECTION]["FOV"]) > 2:
self.data[self.DIRECTION]["FOV"] = clockify(
self.data[self.DIRECTION]["FOV"], hull=True)
elif self.ACTIVE == "MASK":
self.data[self.DIRECTION]["mask"].append(self.clicked_point)
if len(self.data[self.DIRECTION]["mask"]) > 2:
self.data[self.DIRECTION]["mask"] = clockify(
self.data[self.DIRECTION]["mask"])
elif self.ACTIVE == "POINTS":
pt = self.clicked_point
name = self.keyboard_input(update_plot=True)
self.data[self.DIRECTION]["points"].append([pt[0], pt[1], name])
# increment default point
name_parts = name.split("_")
self.active_feature = name_parts[1]
self.active_index = int(name_parts[2])
self.active_letter = self.letters.index(name_parts[3]) + 1
self.guess_text = "{}_{}_{}_{}".format(self.DIRECTION.lower(),self.active_feature,self.active_index,self.letters[self.active_letter])
elif self.ACTIVE == "AUTO POINTS":
self.temp_pts = self.auto_points()
if self.temp_pts is not None:
name = self.keyboard_input(update_plot = True)
# get name so we can assign unique letters
name_parts = name.split("_")
self.active_feature = name_parts[1]
self.active_index = int(name_parts[2])
self.active_letter = self.letters.index(name_parts[3])
for pt in self.temp_pts:
new_name = "{}_{}_{}_{}".format(self.DIRECTION.lower(),self.active_feature,self.active_index,self.letters[self.active_letter])
self.data[self.DIRECTION]["points"].append([pt[0], pt[1], new_name])
self.active_letter += 1
self.temp_pts = None
# increment default point
name_parts = name.split("_")
self.active_feature = name_parts[1]
self.active_index = int(name_parts[2]) + 1
self.active_letter = 0
self.guess_text = "{}_{}_{}_{}".format(self.DIRECTION.lower(),self.active_feature,self.active_index,self.letters[self.active_letter])
elif self.ACTIVE == "CURVE":
pt = self.clicked_point
name = self.keyboard_input(update_plot=True)
name_parts = name.split("_")
self.active_curve_feature = name_parts[1]
self.active_curve_index = int(name_parts[2])
name = "{}_{}_{}".format(self.DIRECTION.lower(),self.active_curve_feature,self.active_curve_index)
self.data[self.DIRECTION]["curves"].append([pt[0], pt[1], name])
# increment default curve
self.active_curve_index += 1
self.active_curve = "{}_{}_{}".format(self.DIRECTION.lower(),self.active_curve_feature,self.active_curve_index)
elif self.ACTIVE == "VP":
self.vp_cache.append(self.clicked_point)
if len(self.vp_cache) == 4:
self.get_vp()
self.vp_cache = []
self.ACTIVE = "POINTS"
elif self.ACTIVE == "DELETE":
name = self.keyboard_input(update_plot = True)
print("Deleting points {}".format(name))
dir_data = self.data[self.DIRECTION]
for group in dir_data:
group_data = dir_data[group]
idxs = []
for pidx,point in enumerate(group_data):
if name in point[2]:
idxs.append(pidx)
idxs.sort()
idxs.reverse()
for idx in idxs:
del group_data[idx]
if self.ACTIVE != "Z AXIS":
self.clicked_point = None
self.undo_cache.append(copy.deepcopy(self.data))
if len(self.undo_cache) > 100:
del self.undo_cache[0]
def undo(self):
if len(self.undo_cache) > 1:
self.data = self.undo_cache[-2]
del self.undo_cache[-1]
self.new = True
def get_z_vp(self):
vp = find_vanishing_point(self.z_cache)
self.data["EB"]["z_vp"] = vp
self.data["WB"]["z_vp"] = vp
self.data["EB"]["z_vp_lines"] = self.z_cache.copy()
self.data["WB"]["z_vp_lines"] = self.z_cache.copy()
self.z_cache = None
self.ACTIVE = "POINTS"
self.save()
self.SHOW_VP = True
def get_vp(self):
"""
Finds best (L2 norm) vanishing point given a list of lines
Parameters
----------
lines : [(x0,y0,x1,y1), ...]
Returns
-------
vp - (x,y)
"""
# mx+b form
#y0 = ax + c
#y1 = bx + d
line0 = [self.vp_cache[0][0],self.vp_cache[0][1],self.vp_cache[1][0],self.vp_cache[1][1]]
line1 = [self.vp_cache[2][0],self.vp_cache[2][1],self.vp_cache[3][0],self.vp_cache[3][1]]
# a = (line0[3] - line0[1])/(line0[2] - line0[0]) if line0[0] < line0[2] else (line0[1] - line0[3])/(line0[0] - line0[2])
# b = (line1[3] - line1[1])/(line1[2] - line1[0]) if line1[0] < line1[2] else (line1[1] - line1[3])/(line1[0] - line1[2])
# c = line0[1] - a*line0[0]
# d = line1[1] - b*line1[0]
# # intersection
# px = (d-b)/(a-c)
# py = a*px + c
[x1,y1,x2,y2] = line0
[x3,y3,x4,y4] = line1
D = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)
px = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-x4*y3))/D
py = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-x4*y3))/D
key = input("Got VP: {}. Is this vanishing point into town or out of town? (I/o)".format([px,py]))
if key.lower() == "o":
OUTBOUND_VP = True
else:
OUTBOUND_VP = False
self.data["EB"]["vp"] = (px,py,OUTBOUND_VP)
self.data["WB"]["vp"] = (px,py,OUTBOUND_VP)
self.save()
def auto_points(self, show = False):
gray = self.im
final = self.im.copy()
gray = np.float32(gray)
gray = cv2.cvtColor(self.im,cv2.COLOR_BGR2GRAY)
gray2 = gray.copy()
ret,gray = cv2.threshold(gray2,160,255,cv2.THRESH_BINARY)
gray = np.float32(gray)
if show:
cv2.imshow("frame",gray/255.0)
cv2.waitKey(0)
# get the cluster of all pixels that are connected to the clicked point
x = self.clicked_point[0]
y = self.clicked_point[1]
clicked_val = gray[y,x]
points_queue = [(x,y)]
visited_list = []
component = []
while len(points_queue) > 0:
pt = points_queue[0]
x = pt[0]
y = pt[1]
points_queue = points_queue[1:]
visited_list.append(pt)
if gray[y,x] == clicked_val:
component.append(pt)
gray[y,x] = 100
final[y,x] = [100,100,100]
for i in range(x-1,x+2):
for j in range(y-1,y+2):
if (i,j) not in visited_list and (i,j) not in points_queue:
points_queue.append((i,j))
if len(component) > 5000:
print("Component exceed max size, try again")
return None
if show:
cv2.imshow("frame",gray/255.0)
cv2.waitKey(0)
# get the convex hull
stack = np.stack([np.array([pt[0],pt[1]]) for pt in component]).astype(np.float64)
hull_indices = ConvexHull(stack).vertices
hull = stack[hull_indices,:]