-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheyedk_utils.py
905 lines (753 loc) · 32.1 KB
/
eyedk_utils.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
from scipy.spatial import distance
from detect import detect
import torch as torch
import imageio
import json
import cv2
import numpy as np
import matplotlib.pyplot as plt
import yaml
import imutils
import os
import re
from PIL import Image
"""This code snippet is where all the utility function resides
Inspired by source https://www.analyticsvidhya.com/blog/2020/05/social-distancing-detection-tool-deep-learning/
"""
################################ open cv functions ################################
def mid_point(img, person, idx):
"""Find mid point in bounding box info
Args:
img (np.array): image to br processed
person ([]): list of all bounding box information
idx (int): index for person to be processed
Returns:
[()]: coordinated for person mid point: (x, y)
"""
# get the coordinates
x1, y1, x2, y2 = person[idx]
_ = cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
# compute bottom center of bbox
x_mid = int((x1 + x2) / 2)
y_mid = int(y2)
mid = (x_mid, y_mid)
_ = cv2.circle(img, mid, 5, (255, 0, 0), -1)
cv2.putText(img, str(idx), mid, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
return mid
################################ Scipy functions ################################
def find_closest(dist, num, thresh):
"""Find if there are points that smaller that thres/people violates social distance rule
Args:
dist ([[]]): 2-D map for distance, from compute_distance()
num (int): number of points in total
thresh (float): threshold for distance
Returns:
p1, p2, d: points that violates the role and their coordinates
"""
p1 = []
p2 = []
d = []
for i in range(num):
for j in range(i, num):
if ((i != j) & (dist[i][j] <= thresh)):
p1.append(i)
p2.append(j)
d.append(dist[i][j])
return p1, p2, d
def change_2_red(img, person, p1, p2):
"""Change person bounding box to red
Args:
img ([[]]): image array
person ([]): person who violates the rule
p1 ([]): person points coordinates
p2 ([]): [person points coordinates
Returns:
img, points: image that processed, and associated coordinates
"""
risky = np.unique(p1 + p2)
points = []
for i in risky:
x1, y1, x2, y2 = person[i]
_ = cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
points.append((int((x1 + x2) / 2), int(y2)))
return img, points
################################ Video/image processing functions ################################
def video_to_frames(video_path, num_frames, temp_dir="frames/"):
"""Parsing video to frames
Args:
video_path (str): video path
num_frames (int): number of frames parsing to
temp_dir (str, optional): dir to save to. Defaults to "frames/".
Returns:
int: number of frames truly parsed
"""
cap = cv2.VideoCapture(video_path)
cnt = 0
if (cap.isOpened() == False):
print("Error opening video stream or file")
ret, first_frame = cap.read()
while (cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# save each frame to folder
img = temp_dir + str(cnt) + '.png'
cv2.imwrite(img, frame)
cnt = cnt + 1
if (cnt == num_frames):
break
# Break the loop
else:
break
return cnt
def load_json(file_path):
"""Loading json file
Args:
file_path (str): file path
Returns:
[obj]: data object
"""
with open(file_path) as json_file:
data = json.load(json_file)
return data
def detect_with_yolo(out='yolo_output/', source='frames/'):
"""YOLO5 premeter preset
Args:
out (str, optional): output source file. Defaults to 'yolo_output/'.
source (str, optional): input source file. Defaults to 'frames/'.
"""
weights = ['weights/yolov5l.pt']
save_txt = True
view_img = False
save_txt = True
imgsz = 640
conf_thres = 0.4
iou_thres = 0.5
classes = []
save_img = True
with torch.no_grad():
if True:
detect(out, source, weights, view_img, save_txt, imgsz)
def generate_video(image_path, num=300):
"""Convert all frames to video
Args:
image_path (str): frame path
num (int, optional): number of images selected. Defaults to 300.
"""
# image_path should be where the images are stored, images must have the format of
# i.png with i being a number, image_path must end with a / for easy of string
# operations
imgs = []
img_name_template = image_path + "{}.png"
for i in range(num):
imgs.append(imageio.imread(img_name_template.format(i)))
imageio.mimsave(image_path + "0movie.gif", imgs)
# ----------------------------------- Birds Eye View -------------------------------------------
"""Inspired by blog: https://towardsdatascience.com/a-social-distancing-detector-using-a-tensorflow-object-detection-model-python-and-opencv-4450a431238
"""
def read_dict(file_path='yolo_output/json_out.txt'):
"""read dictionary type data
Args:
file_path (str, optional): file path. Defaults to 'yolo_output/json_out.txt'.
Returns:
dict: data read
"""
with open(file_path, 'r') as inf:
dict = eval(inf.read())
return dict
def distance_metric_evaluation(dict, matrix, imgOutput, thresh=100, mode="Euclidean", save_video=True):
"""evaluate distances from frames
Args:
dict (dict): data
matrix ([np.array]): warping 2D matrix
imgOutput (str): image outputs
thresh (int, optional): threshold for distance. Defaults to 100.
mode (str, optional): distance measuring metrics. Defaults to "Euclidean".
save_video (bool, optional): If save to video. Defaults to True.
"""
process_out_path = "temp_dir_distance/"
process_in_path = "temp_dir/"
divider = "\\" # swap this for "/" for mac
for item in os.listdir(process_out_path):
os.remove(process_out_path + item)
for i in range(len(dict)):
frame_name = (list(dict.keys())[0]).split(divider)
frame_name[-1] = "{}.png".format(i)
saved_path_list = frame_name.copy()
saved_path_list[-2] = process_out_path[:-1]
frame = frame_name[0]
saved_path = frame_name[0]
for j in range(1, len(frame_name)):
frame = frame + divider + frame_name[j]
saved_path = saved_path + divider + saved_path_list[j]
saved_path = saved_path + divider + saved_path_list[j]
# string parsing
boxes = dict[frame]["location_x"]
scores = dict[frame]["confidence"]
classes = dict[frame]["category"]
img = cv2.imread(frame)
height, width, _ = img.shape
array_boxes_detected = get_human_box_detection(boxes, scores, classes, height,
width) # all human boxes in 1 frame
img = cv2.imread(frame)
array_centroids, array_groundpoints = get_centroids_and_groundpoints(
array_boxes_detected) # 1 gound point for each box
points = []
if mode == "Euclidean":
dist = compute_distance(array_groundpoints, len(array_groundpoints))
p1, p2, d = find_closest(dist, len(array_groundpoints), thresh)
for i in range(len(array_boxes_detected)):
x1, y1, x2, y2 = array_boxes_detected[i]
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 5)
points.append((int((x1 + x2) / 2), int(y2)))
change_2_red(img, array_boxes_detected, p1, p2)
cv2.imwrite(saved_path, img)
elif mode == "BirdEye":
transformed_midpoints = compute_point_perspective_transformation(matrix,
array_groundpoints) # transformed bottom centre points
dist = compute_distance(transformed_midpoints, len(
transformed_midpoints)) # computing euclidean between every pair of centre points
p1, p2, d = find_closest(dist, len(transformed_midpoints), thresh)
for i in range(len(array_boxes_detected)):
x1, y1, x2, y2 = array_boxes_detected[i]
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 5)
points.append((int((x1 + x2) / 2), int(y2)))
change_2_red(img, array_boxes_detected, p1, p2)
cv2.imwrite(saved_path, img)
# img, points = change_2_red(img, array_boxes_detected, p1, p2)
return
def compute_distance(midpoints, num):
"""Compute distance in between with brute-force
Args:
midpoints ([]): array of all midpoints for person
num (int): number of persons
Returns:
[[]]: 2D array for all distanced
"""
dist = np.zeros((num, num))
for i in range(num):
for j in range(i + 1, num):
if i != j:
dst = distance.euclidean(midpoints[i], midpoints[j])
dist[i][j] = dst
return dist
list_points = list()
def find_matrix(img_path="/Users/victorzhang/Desktop/EyeDK/frames/1.png"):
"""Find warping 2D matrix for each frame
Args:
img_path (str, optional): frame path. Defaults to "/Users/victorzhang/Desktop/EyeDK/frames/1.png".
Returns:
matrix, image: waarping matrix and output image
"""
img = cv2.imread(img_path)
width, height, _ = img.shape
windowName = 'MouseCallback'
cv2.namedWindow(windowName)
cv2.setMouseCallback(windowName, CallBackFunc)
while (True):
cv2.imshow(windowName, img)
if len(list_points) == 4:
# Return a dict to the YAML file
config_data = dict(
image_parameters=dict(
p2=list_points[3],
p1=list_points[2],
p4=list_points[0],
p3=list_points[1],
width_og=width,
height_og=height,
img_path=img_path,
size_frame=width,
))
# Write the result to the config file
with open('birdview.yml', 'w') as outfile:
yaml.dump(config_data, outfile, default_flow_style=False)
break
if cv2.waitKey(20) == 27:
break
cv2.destroyAllWindows()
with open("birdview.yml", "r") as ymlfile:
cfg = yaml.safe_load(ymlfile)
width_og, height_og = 0, 0
corner_points = []
for section in cfg:
corner_points.append(cfg["image_parameters"]["p1"])
corner_points.append(cfg["image_parameters"]["p2"])
corner_points.append(cfg["image_parameters"]["p3"])
corner_points.append(cfg["image_parameters"]["p4"])
width_og = int(cfg["image_parameters"]["width_og"])
height_og = int(cfg["image_parameters"]["height_og"])
img_path = cfg["image_parameters"]["img_path"]
size_frame = cfg["image_parameters"]["size_frame"]
matrix, imgOutput = compute_perspective_transform(corner_points, width_og, height_og, img)
return matrix, imgOutput
def compute_perspective_transform(corner_points, width, height, image):
"""
Compute the transformation matrix
@ corner_points : 4 corner points selected from the image
@ height, width : size of the image
return : transformation matrix and the transformed image
"""
corner_points_array = np.float32(corner_points)
img_params = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
matrix = cv2.getPerspectiveTransform(corner_points_array, img_params)
img_transformed = cv2.warpPerspective(image, matrix, (width, height))
return matrix, img_transformed
def CallBackFunc(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print("Left button of the mouse is clicked - position (", x, ", ", y, ")")
list_points.append([x, y])
elif event == cv2.EVENT_RBUTTONDOWN:
print("Right button of the mouse is clicked - position (", x, ", ", y, ")")
list_points.append([x, y])
def compute_point_perspective_transformation(matrix, list_downoids):
""" Apply the perspective transformation to every ground point which have been detected on the main frame.
@ matrix : the 3x3 matrix
@ list_downoids : list that contains the points to transform
return : list containing all the new points
"""
# Compute the new coordinates of our points
list_points_to_detect = np.float32(list_downoids).reshape(-1, 1, 2)
transformed_points = cv2.perspectiveTransform(list_points_to_detect, matrix)
# Loop over the points and add them to the list that will be returned
transformed_points_list = list()
for i in range(0, transformed_points.shape[0]):
transformed_points_list.append([transformed_points[i][0][0], transformed_points[i][0][1]])
return transformed_points_list
def get_human_box_detection(boxes, scores, classes, width, height):
"""
For each object detected, check if it is a human and if the confidence >> our threshold.
Return 2 coordonates necessary to build the box.
@ boxes : all our boxes coordinates
@ scores : confidence score on how good the prediction is -> between 0 & 1
@ classes : the class of the detected object ( 1 for human )
@ height : of the image -> to get the real pixel value
@ width : of the image -> to get the real pixel value
"""
array_boxes = list() # Create an empty list
for i in range(len(boxes)):
# If the class of the detected object is person and the confidence of the prediction is > 0.8
if classes[i] == 0.0 and scores[i] > 0.4:
# Multiply the X coordonnate by the height of the image and the Y coordonate by the width
# To transform the box value into pixel coordonate values.
box = [boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]] * np.array([height, width, height, width])
# Add the results converted to int
array_boxes.append([int(box[0]), int(box[1]), int(box[2]), int(box[3])])
return array_boxes
def get_centroids_and_groundpoints(array_boxes_detected):
"""
For every bounding box, compute the centroid and the point located on the bottom center of the box
@ array_boxes_detected : list containing all our bounding boxes
"""
array_centroids, array_groundpoints = [], [] # Initialize empty centroid and ground point lists
for index, box in enumerate(array_boxes_detected):
centroid, ground_point = get_points_from_box(box)
array_centroids.append(centroid)
array_groundpoints.append(ground_point)
return array_centroids, array_groundpoints
def get_points_from_box(box):
"""
Get the center of the bounding and the point "on the ground"
@ param = box : 2 points representing the bounding box
@ return = centroid (x1,y1) and ground point (x2,y2)
"""
# Center of the box x = (x1+x2)/2 et y = (y1+y2)/2
# center_x = int(((box[1]+box[3])/2))
# center_y = int(((box[0]+box[2])/2))
# center_y_ground = center_y + ((box[2] - box[0])/2)
# return (center_x, center_y),(center_x, int(center_y_ground))
center_x = int(((box[0] + box[2]) / 2)) # top left -> bottom right corner (bottom right has higher values)
center_y = int(((box[1] + box[3]) / 2))
center_y_ground = center_y + ((box[2] - box[0]) / 2) # box[2] or box[1] - whichever is bottom
return (center_x, center_y), (center_x, int(box[3])) # prev was int(center_y_ground), box[0]
def draw(img, corners):
"""Draw corners of bounding boxs
Args:
img (np.array): 2D images
corners ([]): corner points
"""
cv2.circle(img, (int((corners[0] + corners[2]) / 2), corners[3]), 15, (0, 255, 0), -1)
if __name__ == "__main__":
# detect_with_yolo()
# generate_video("yolo_output/")
json_dict = read_dict("temp_dir_out/json_out.txt")
# print(dict["/Users/victorzhang/Desktop/EyeDK/frames/102.png"]["location_x"][0])
matrix, imgOutput = find_matrix("temp_dir/0.png")
# bottom right clockwise
distance_metric_evaluation(json_dict, matrix, imgOutput)
from scipy.spatial import distance
from detect import detect
import torch as torch
import imageio
import json
import cv2
import numpy as np
import matplotlib.pyplot as plt
import yaml
import imutils
import os
import re
from PIL import Image
"""This code snippet is where all the utility function resides
Inspired by source https://www.analyticsvidhya.com/blog/2020/05/social-distancing-detection-tool-deep-learning/
"""
################################ open cv functions ################################
def mid_point(img, person, idx):
"""Find mid point in bounding box info
Args:
img (np.array): image to br processed
person ([]): list of all bounding box information
idx (int): index for person to be processed
Returns:
[()]: coordinated for person mid point: (x, y)
"""
# get the coordinates
x1, y1, x2, y2 = person[idx]
_ = cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
# compute bottom center of bbox
x_mid = int((x1 + x2) / 2)
y_mid = int(y2)
mid = (x_mid, y_mid)
_ = cv2.circle(img, mid, 5, (255, 0, 0), -1)
cv2.putText(img, str(idx), mid, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
return mid
################################ Scipy functions ################################
def find_closest(dist, num, thresh):
"""Find if there are points that smaller that thres/people violates social distance rule
Args:
dist ([[]]): 2-D map for distance, from compute_distance()
num (int): number of points in total
thresh (float): threshold for distance
Returns:
p1, p2, d: points that violates the role and their coordinates
"""
p1 = []
p2 = []
d = []
for i in range(num):
for j in range(i, num):
if ((i != j) & (dist[i][j] <= thresh)):
p1.append(i)
p2.append(j)
d.append(dist[i][j])
return p1, p2, d
def change_2_red(img, person, p1, p2):
"""Change person bounding box to red
Args:
img ([[]]): image array
person ([]): person who violates the rule
p1 ([]): person points coordinates
p2 ([]): [person points coordinates
Returns:
img, points: image that processed, and associated coordinates
"""
risky = np.unique(p1 + p2)
points = []
for i in risky:
x1, y1, x2, y2 = person[i]
_ = cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
points.append((int((x1 + x2) / 2), int(y2)))
return img, points
################################ Video/image processing functions ################################
def video_to_frames(video_path, num_frames, temp_dir="frames/"):
"""Parsing video to frames
Args:
video_path (str): video path
num_frames (int): number of frames parsing to
temp_dir (str, optional): dir to save to. Defaults to "frames/".
Returns:
int: number of frames truly parsed
"""
cap = cv2.VideoCapture(video_path)
cnt = 0
if (cap.isOpened() == False):
print("Error opening video stream or file")
ret, first_frame = cap.read()
while (cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# save each frame to folder
img = temp_dir + str(cnt) + '.png'
cv2.imwrite(img, frame)
cnt = cnt + 1
if (cnt == num_frames):
break
# Break the loop
else:
break
return cnt
def load_json(file_path):
"""Loading json file
Args:
file_path (str): file path
Returns:
[obj]: data object
"""
with open(file_path) as json_file:
data = json.load(json_file)
return data
def detect_with_yolo(out='yolo_output/', source='frames/'):
"""YOLO5 premeter preset
Args:
out (str, optional): output source file. Defaults to 'yolo_output/'.
source (str, optional): input source file. Defaults to 'frames/'.
"""
weights = ['weights/yolov5l.pt']
save_txt = True
view_img = False
save_txt = True
imgsz = 640
conf_thres = 0.4
iou_thres = 0.5
classes = []
save_img = True
with torch.no_grad():
if True:
detect(out, source, weights, view_img, save_txt, imgsz)
def generate_video(image_path, num=300):
"""Convert all frames to video
Args:
image_path (str): frame path
num (int, optional): number of images selected. Defaults to 300.
"""
# image_path should be where the images are stored, images must have the format of
# i.png with i being a number, image_path must end with a / for easy of string
# operations
imgs = []
img_name_template = image_path + "{}.png"
for i in range(num):
imgs.append(imageio.imread(img_name_template.format(i)))
imageio.mimsave(image_path + "0movie.gif", imgs)
# ----------------------------------- Birds Eye View -------------------------------------------
"""Inspired by blog: https://towardsdatascience.com/a-social-distancing-detector-using-a-tensorflow-object-detection-model-python-and-opencv-4450a431238
"""
def read_dict(file_path='yolo_output/json_out.txt'):
"""read dictionary type data
Args:
file_path (str, optional): file path. Defaults to 'yolo_output/json_out.txt'.
Returns:
dict: data read
"""
with open(file_path, 'r') as inf:
dict = eval(inf.read())
return dict
def distance_metric_evaluation(dict, matrix, imgOutput, thresh=100, mode="Euclidean", save_video=True):
"""evaluate distances from frames
Args:
dict (dict): data
matrix ([np.array]): warping 2D matrix
imgOutput (str): image outputs
thresh (int, optional): threshold for distance. Defaults to 100.
mode (str, optional): distance measuring metrics. Defaults to "Euclidean".
save_video (bool, optional): If save to video. Defaults to True.
"""
temp_dir_distance = "temp_dir_distance/"
process_in_path = "temp_dir/"
divider = "\\" # swap this for "/" for mac
for item in os.listdir(temp_dir_distance):
os.remove(temp_dir_distance + item)
for i in range(len(dict)):
frame_name = (list(dict.keys())[0]).split(divider)
frame_name[-1] = "{}.png".format(i)
saved_path_list = frame_name.copy()
saved_path_list[-2] = temp_dir_distance[:-1]
frame = frame_name[0]
saved_path = frame_name[0]
for j in range(1, len(frame_name)):
frame = frame + divider + frame_name[j]
saved_path = saved_path + divider + saved_path_list[j]
# string parsing
boxes = dict[frame]["location_x"]
scores = dict[frame]["confidence"]
classes = dict[frame]["category"]
img = cv2.imread(frame)
height, width, _ = img.shape
array_boxes_detected = get_human_box_detection(boxes, scores, classes, height,
width) # all human boxes in 1 frame
img = cv2.imread(frame)
array_centroids, array_groundpoints = get_centroids_and_groundpoints(
array_boxes_detected) # 1 gound point for each box
points = []
if mode == "Euclidean":
dist = compute_distance(array_groundpoints, len(array_groundpoints))
p1, p2, d = find_closest(dist, len(array_groundpoints), thresh)
for i in range(len(array_boxes_detected)):
x1, y1, x2, y2 = array_boxes_detected[i]
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 5)
points.append((int((x1 + x2) / 2), int(y2)))
change_2_red(img, array_boxes_detected, p1, p2)
cv2.imwrite(saved_path, img)
elif mode == "BirdEye":
transformed_midpoints = compute_point_perspective_transformation(matrix,
array_groundpoints) # transformed bottom centre points
dist = compute_distance(transformed_midpoints, len(
transformed_midpoints)) # computing euclidean between every pair of centre points
p1, p2, d = find_closest(dist, len(transformed_midpoints), thresh)
for i in range(len(array_boxes_detected)):
x1, y1, x2, y2 = array_boxes_detected[i]
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 5)
points.append((int((x1 + x2) / 2), int(y2)))
change_2_red(img, array_boxes_detected, p1, p2)
cv2.imwrite(saved_path, img)
# img, points = change_2_red(img, array_boxes_detected, p1, p2)
return
def compute_distance(midpoints, num):
"""Compute distance in between with brute-force
Args:
midpoints ([]): array of all midpoints for person
num (int): number of persons
Returns:
[[]]: 2D array for all distanced
"""
dist = np.zeros((num, num))
for i in range(num):
for j in range(i + 1, num):
if i != j:
dst = distance.euclidean(midpoints[i], midpoints[j])
dist[i][j] = dst
return dist
list_points = list()
def find_matrix(img_path="/Users/victorzhang/Desktop/EyeDK/frames/1.png"):
"""Find warping 2D matrix for each frame
Args:
img_path (str, optional): frame path. Defaults to "/Users/victorzhang/Desktop/EyeDK/frames/1.png".
Returns:
matrix, image: waarping matrix and output image
"""
img = cv2.imread(img_path)
width, height, _ = img.shape
windowName = 'MouseCallback'
cv2.namedWindow(windowName)
cv2.setMouseCallback(windowName, CallBackFunc)
while (True):
cv2.imshow(windowName, img)
if len(list_points) == 4:
# Return a dict to the YAML file
config_data = dict(
image_parameters=dict(
p2=list_points[3],
p1=list_points[2],
p4=list_points[0],
p3=list_points[1],
width_og=width,
height_og=height,
img_path=img_path,
size_frame=width,
))
# Write the result to the config file
with open('birdview.yml', 'w') as outfile:
yaml.dump(config_data, outfile, default_flow_style=False)
break
if cv2.waitKey(20) == 27:
break
cv2.destroyAllWindows()
with open("birdview.yml", "r") as ymlfile:
cfg = yaml.safe_load(ymlfile)
width_og, height_og = 0, 0
corner_points = []
for section in cfg:
corner_points.append(cfg["image_parameters"]["p1"])
corner_points.append(cfg["image_parameters"]["p2"])
corner_points.append(cfg["image_parameters"]["p3"])
corner_points.append(cfg["image_parameters"]["p4"])
width_og = int(cfg["image_parameters"]["width_og"])
height_og = int(cfg["image_parameters"]["height_og"])
img_path = cfg["image_parameters"]["img_path"]
size_frame = cfg["image_parameters"]["size_frame"]
matrix, imgOutput = compute_perspective_transform(corner_points, width_og, height_og, img)
return matrix, imgOutput
def compute_perspective_transform(corner_points, width, height, image):
"""
Compute the transformation matrix
@ corner_points : 4 corner points selected from the image
@ height, width : size of the image
return : transformation matrix and the transformed image
"""
corner_points_array = np.float32(corner_points)
img_params = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
matrix = cv2.getPerspectiveTransform(corner_points_array, img_params)
img_transformed = cv2.warpPerspective(image, matrix, (width, height))
return matrix, img_transformed
def CallBackFunc(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print("Left button of the mouse is clicked - position (", x, ", ", y, ")")
list_points.append([x, y])
elif event == cv2.EVENT_RBUTTONDOWN:
print("Right button of the mouse is clicked - position (", x, ", ", y, ")")
list_points.append([x, y])
def compute_point_perspective_transformation(matrix, list_downoids):
""" Apply the perspective transformation to every ground point which have been detected on the main frame.
@ matrix : the 3x3 matrix
@ list_downoids : list that contains the points to transform
return : list containing all the new points
"""
# Compute the new coordinates of our points
list_points_to_detect = np.float32(list_downoids).reshape(-1, 1, 2)
transformed_points = cv2.perspectiveTransform(list_points_to_detect, matrix)
# Loop over the points and add them to the list that will be returned
transformed_points_list = list()
for i in range(0, transformed_points.shape[0]):
transformed_points_list.append([transformed_points[i][0][0], transformed_points[i][0][1]])
return transformed_points_list
def get_human_box_detection(boxes, scores, classes, width, height):
"""
For each object detected, check if it is a human and if the confidence >> our threshold.
Return 2 coordonates necessary to build the box.
@ boxes : all our boxes coordinates
@ scores : confidence score on how good the prediction is -> between 0 & 1
@ classes : the class of the detected object ( 1 for human )
@ height : of the image -> to get the real pixel value
@ width : of the image -> to get the real pixel value
"""
array_boxes = list() # Create an empty list
for i in range(len(boxes)):
# If the class of the detected object is person and the confidence of the prediction is > 0.8
if classes[i] == 0.0 and scores[i] > 0.4:
# Multiply the X coordonnate by the height of the image and the Y coordonate by the width
# To transform the box value into pixel coordonate values.
box = [boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]] * np.array([height, width, height, width])
# Add the results converted to int
array_boxes.append([int(box[0]), int(box[1]), int(box[2]), int(box[3])])
return array_boxes
def get_centroids_and_groundpoints(array_boxes_detected):
"""
For every bounding box, compute the centroid and the point located on the bottom center of the box
@ array_boxes_detected : list containing all our bounding boxes
"""
array_centroids, array_groundpoints = [], [] # Initialize empty centroid and ground point lists
for index, box in enumerate(array_boxes_detected):
centroid, ground_point = get_points_from_box(box)
array_centroids.append(centroid)
array_groundpoints.append(ground_point)
return array_centroids, array_groundpoints
def get_points_from_box(box):
"""
Get the center of the bounding and the point "on the ground"
@ param = box : 2 points representing the bounding box
@ return = centroid (x1,y1) and ground point (x2,y2)
"""
# Center of the box x = (x1+x2)/2 et y = (y1+y2)/2
# center_x = int(((box[1]+box[3])/2))
# center_y = int(((box[0]+box[2])/2))
# center_y_ground = center_y + ((box[2] - box[0])/2)
# return (center_x, center_y),(center_x, int(center_y_ground))
center_x = int(((box[0] + box[2]) / 2)) # top left -> bottom right corner (bottom right has higher values)
center_y = int(((box[1] + box[3]) / 2))
center_y_ground = center_y + ((box[2] - box[0]) / 2) # box[2] or box[1] - whichever is bottom
return (center_x, center_y), (center_x, int(box[3])) # prev was int(center_y_ground), box[0]
def draw(img, corners):
"""Draw corners of bounding boxs
Args:
img (np.array): 2D images
corners ([]): corner points
"""
cv2.circle(img, (int((corners[0] + corners[2]) / 2), corners[3]), 15, (0, 255, 0), -1)
if __name__ == "__main__":
# detect_with_yolo()
# generate_video("yolo_output/")
json_dict = read_dict("temp_dir_out/json_out.txt")
# print(dict["/Users/victorzhang/Desktop/EyeDK/frames/102.png"]["location_x"][0])
matrix, imgOutput = find_matrix("temp_dir/0.png")
# bottom right clockwise
distance_metric_evaluation(json_dict, matrix, imgOutput)