Diego Fernandez commited on
Commit
210ae8c
1 Parent(s): 088dea4

chore: improve code

Browse files
app.py CHANGED
@@ -10,6 +10,13 @@ dd_model = gr.Dropdown(choices=["YoloV7", "YoloV7 Tiny"], value="YoloV7", label=
10
 
11
  cb_motion_estimation = gr.Checkbox(value=True, label="Track camera movement")
12
 
 
 
 
 
 
 
 
13
  cb_path_draw = gr.Checkbox(value=True, label="Draw objects paths")
14
 
15
  dd_track_points = gr.Dropdown(
@@ -33,8 +40,7 @@ iface = gr.Interface(
33
  inputs=[
34
  input_video,
35
  dd_model,
36
- cb_motion_estimation,
37
- cb_path_draw,
38
  dd_track_points,
39
  slide_threshold,
40
  ],
 
10
 
11
  cb_motion_estimation = gr.Checkbox(value=True, label="Track camera movement")
12
 
13
+ features = gr.CheckboxGroup(
14
+ choices=["Track camera movement", "Draw objects paths"],
15
+ value=["Track camera movement", "Draw objects paths"],
16
+ label="Features",
17
+ type="index"
18
+ )
19
+
20
  cb_path_draw = gr.Checkbox(value=True, label="Draw objects paths")
21
 
22
  dd_track_points = gr.Dropdown(
 
40
  inputs=[
41
  input_video,
42
  dd_model,
43
+ features,
 
44
  dd_track_points,
45
  slide_threshold,
46
  ],
inference_utils.py → custom_models/Yolo.py RENAMED
@@ -1,23 +1,9 @@
1
- import argparse
2
- import glob
3
  import os
4
- from enum import Enum
5
  from typing import List, Optional, Union
6
 
7
- import norfair
8
  import numpy as np
9
  import torch
10
- import torchvision.ops.boxes as bops
11
- from norfair import Detection, draw_absolute_grid
12
-
13
- DISTANCE_THRESHOLD_BBOX: float = 3.33
14
- DISTANCE_THRESHOLD_CENTROID: int = 30
15
- MAX_DISTANCE: int = 10000
16
-
17
-
18
- models_path = {"YoloV7": "models/yolov7.pt", "YoloV7 Tiny": "models/yolov7-tiny.pt"}
19
-
20
- style = {"Bounding box": "bbox", "Centroid": "centroid"}
21
 
22
 
23
  class YOLO:
@@ -56,59 +42,6 @@ class YOLO:
56
  return detections
57
 
58
 
59
- def euclidean_distance(detection, tracked_object):
60
- return np.linalg.norm(detection.points - tracked_object.estimate)
61
-
62
-
63
- def center(points):
64
- return [np.mean(np.array(points), axis=0)]
65
-
66
-
67
- def iou_pytorch(detection, tracked_object):
68
- # Slower but simplier version of iou
69
-
70
- detection_points = np.concatenate([detection.points[0], detection.points[1]])
71
- tracked_object_points = np.concatenate([tracked_object.estimate[0], tracked_object.estimate[1]])
72
-
73
- box_a = torch.tensor([detection_points], dtype=torch.float)
74
- box_b = torch.tensor([tracked_object_points], dtype=torch.float)
75
- iou = bops.box_iou(box_a, box_b)
76
-
77
- # Since 0 <= IoU <= 1, we define 1/IoU as a distance.
78
- # Distance values will be in [1, inf)
79
- return np.float(1 / iou if iou else MAX_DISTANCE)
80
-
81
-
82
- def iou(detection, tracked_object):
83
- # Detection points will be box A
84
- # Tracked objects point will be box B.
85
-
86
- box_a = np.concatenate([detection.points[0], detection.points[1]])
87
- box_b = np.concatenate([tracked_object.estimate[0], tracked_object.estimate[1]])
88
-
89
- x_a = max(box_a[0], box_b[0])
90
- y_a = max(box_a[1], box_b[1])
91
- x_b = min(box_a[2], box_b[2])
92
- y_b = min(box_a[3], box_b[3])
93
-
94
- # Compute the area of intersection rectangle
95
- inter_area = max(0, x_b - x_a + 1) * max(0, y_b - y_a + 1)
96
-
97
- # Compute the area of both the prediction and tracker
98
- # rectangles
99
- box_a_area = (box_a[2] - box_a[0] + 1) * (box_a[3] - box_a[1] + 1)
100
- box_b_area = (box_b[2] - box_b[0] + 1) * (box_b[3] - box_b[1] + 1)
101
-
102
- # Compute the intersection over union by taking the intersection
103
- # area and dividing it by the sum of prediction + tracker
104
- # areas - the interesection area
105
- iou = inter_area / float(box_a_area + box_b_area - inter_area)
106
-
107
- # Since 0 <= IoU <= 1, we define 1/IoU as a distance.
108
- # Distance values will be in [1, inf)
109
- return 1 / iou if iou else (MAX_DISTANCE)
110
-
111
-
112
  def yolo_detections_to_norfair_detections(
113
  yolo_detections: torch.tensor, track_points: str = "centroid" # bbox or centroid
114
  ) -> List[Detection]:
@@ -134,35 +67,3 @@ def yolo_detections_to_norfair_detections(
134
  norfair_detections.append(Detection(points=bbox, scores=scores))
135
 
136
  return norfair_detections
137
-
138
-
139
- def clean_videos(path: str):
140
- # Remove past videos
141
- files = glob.glob(f"{path}/*")
142
- for file in files:
143
- if file.endswith(".mp4"):
144
- os.remove(file)
145
-
146
-
147
- def draw(
148
- paths_drawer,
149
- track_points,
150
- frame,
151
- detections,
152
- tracked_objects,
153
- coord_transformations,
154
- fix_paths,
155
- ):
156
- if track_points == "centroid":
157
- norfair.draw_points(frame, detections)
158
- norfair.draw_tracked_objects(frame, tracked_objects)
159
- elif track_points == "bbox":
160
- norfair.draw_boxes(frame, detections)
161
- norfair.draw_tracked_boxes(frame, tracked_objects)
162
-
163
- if fix_paths:
164
- frame = paths_drawer.draw(frame, tracked_objects, coord_transformations)
165
- elif paths_drawer is not None:
166
- frame = paths_drawer.draw(frame, tracked_objects)
167
-
168
- return frame
 
 
 
1
  import os
 
2
  from typing import List, Optional, Union
3
 
 
4
  import numpy as np
5
  import torch
6
+ from norfair import Detection
 
 
 
 
 
 
 
 
 
 
7
 
8
 
9
  class YOLO:
 
42
  return detections
43
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def yolo_detections_to_norfair_detections(
46
  yolo_detections: torch.tensor, track_points: str = "centroid" # bbox or centroid
47
  ) -> List[Detection]:
 
67
  norfair_detections.append(Detection(points=bbox, scores=scores))
68
 
69
  return norfair_detections
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
custom_models/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .Yolo import YOLO, yolo_detections_to_norfair_detections
models/.gitkeep → demo_utils/__init__.py RENAMED
File without changes
demo_utils/configuration.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ DISTANCE_THRESHOLD_BBOX: float = 3.33
2
+ DISTANCE_THRESHOLD_CENTROID: int = 30
3
+ MAX_DISTANCE: int = 10000
4
+
5
+ models_path = {"YoloV7": "custom_models/yolov7.pt", "YoloV7 Tiny": "custom_models/yolov7-tiny.pt"}
6
+
7
+ style = {"Bounding box": "bbox", "Centroid": "centroid"}
demo_utils/distance_function.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import torchvision.ops.boxes as bops
4
+
5
+ from demo_utils.configuration import MAX_DISTANCE
6
+
7
+
8
+ def euclidean_distance(detection, tracked_object):
9
+ return np.linalg.norm(detection.points - tracked_object.estimate)
10
+
11
+
12
+ def iou_pytorch(detection, tracked_object):
13
+ # Slower but simplier version of iou
14
+
15
+ detection_points = np.concatenate([detection.points[0], detection.points[1]])
16
+ tracked_object_points = np.concatenate([tracked_object.estimate[0], tracked_object.estimate[1]])
17
+
18
+ box_a = torch.tensor([detection_points], dtype=torch.float)
19
+ box_b = torch.tensor([tracked_object_points], dtype=torch.float)
20
+ iou = bops.box_iou(box_a, box_b)
21
+
22
+ # Since 0 <= IoU <= 1, we define 1/IoU as a distance.
23
+ # Distance values will be in [1, inf)
24
+ return np.float(1 / iou if iou else MAX_DISTANCE)
25
+
26
+
27
+ def iou(detection, tracked_object):
28
+ # Detection points will be box A
29
+ # Tracked objects point will be box B.
30
+
31
+ box_a = np.concatenate([detection.points[0], detection.points[1]])
32
+ box_b = np.concatenate([tracked_object.estimate[0], tracked_object.estimate[1]])
33
+
34
+ x_a = max(box_a[0], box_b[0])
35
+ y_a = max(box_a[1], box_b[1])
36
+ x_b = min(box_a[2], box_b[2])
37
+ y_b = min(box_a[3], box_b[3])
38
+
39
+ # Compute the area of intersection rectangle
40
+ inter_area = max(0, x_b - x_a + 1) * max(0, y_b - y_a + 1)
41
+
42
+ # Compute the area of both the prediction and tracker
43
+ # rectangles
44
+ box_a_area = (box_a[2] - box_a[0] + 1) * (box_a[3] - box_a[1] + 1)
45
+ box_b_area = (box_b[2] - box_b[0] + 1) * (box_b[3] - box_b[1] + 1)
46
+
47
+ # Compute the intersection over union by taking the intersection
48
+ # area and dividing it by the sum of prediction + tracker
49
+ # areas - the interesection area
50
+ iou = inter_area / float(box_a_area + box_b_area - inter_area)
51
+
52
+ # Since 0 <= IoU <= 1, we define 1/IoU as a distance.
53
+ # Distance values will be in [1, inf)
54
+ return 1 / iou if iou else (MAX_DISTANCE)
demo_utils/draw.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import norfair
2
+ import numpy as np
3
+
4
+
5
+ def draw(
6
+ paths_drawer,
7
+ track_points,
8
+ frame,
9
+ detections,
10
+ tracked_objects,
11
+ coord_transformations,
12
+ fix_paths,
13
+ ):
14
+ if track_points == "centroid":
15
+ norfair.draw_points(frame, detections)
16
+ norfair.draw_tracked_objects(frame, tracked_objects)
17
+ elif track_points == "bbox":
18
+ norfair.draw_boxes(frame, detections)
19
+ norfair.draw_tracked_boxes(frame, tracked_objects)
20
+
21
+ if fix_paths:
22
+ frame = paths_drawer.draw(frame, tracked_objects, coord_transformations)
23
+ elif paths_drawer is not None:
24
+ frame = paths_drawer.draw(frame, tracked_objects)
25
+
26
+ return frame
27
+
28
+
29
+ def center(points):
30
+ return [np.mean(np.array(points), axis=0)]
demo_utils/files.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import glob
2
+ import os
3
+
4
+
5
+ def clean_videos(path: str):
6
+ # Remove past videos
7
+ files = glob.glob(f"{path}/*")
8
+ for file in files:
9
+ if file.endswith(".mp4"):
10
+ os.remove(file)
inference.py CHANGED
@@ -3,31 +3,25 @@ import glob
3
  import os
4
 
5
  import numpy as np
6
- from norfair import AbsolutePaths, FixedCamera, Paths, Tracker, Video
7
  from norfair.camera_motion import HomographyTransformationGetter, MotionEstimator
8
 
9
- from inference_utils import (
10
- YOLO,
11
- center,
12
- clean_videos,
13
- draw,
14
- euclidean_distance,
15
- iou,
16
  models_path,
17
  style,
18
- yolo_detections_to_norfair_detections,
19
  )
20
-
21
- DISTANCE_THRESHOLD_BBOX: float = 3.33
22
- DISTANCE_THRESHOLD_CENTROID: int = 30
23
- MAX_DISTANCE: int = 10000
24
 
25
 
26
  def inference(
27
  input_video: str,
28
  model: str,
29
- motion_estimation: bool,
30
- drawing_paths: bool,
31
  track_points: str,
32
  model_threshold: str,
33
  ):
@@ -42,6 +36,14 @@ def inference(
42
  model = YOLO(models_path[model])
43
  video = Video(input_path=input_video, output_path=output_path)
44
 
 
 
 
 
 
 
 
 
45
  if motion_estimation and drawing_paths:
46
  fix_paths = True
47
 
@@ -56,6 +58,7 @@ def inference(
56
  distance_threshold = (
57
  DISTANCE_THRESHOLD_BBOX if track_points == "bbox" else DISTANCE_THRESHOLD_CENTROID
58
  )
 
59
  tracker = Tracker(
60
  distance_function=distance_function,
61
  distance_threshold=distance_threshold,
 
3
  import os
4
 
5
  import numpy as np
6
+ from norfair import AbsolutePaths, Paths, Tracker, Video
7
  from norfair.camera_motion import HomographyTransformationGetter, MotionEstimator
8
 
9
+ from custom_models import YOLO, yolo_detections_to_norfair_detections
10
+ from demo_utils.configuration import (
11
+ DISTANCE_THRESHOLD_BBOX,
12
+ DISTANCE_THRESHOLD_CENTROID,
 
 
 
13
  models_path,
14
  style,
 
15
  )
16
+ from demo_utils.distance_function import euclidean_distance, iou
17
+ from demo_utils.draw import center, draw
18
+ from demo_utils.files import clean_videos
 
19
 
20
 
21
  def inference(
22
  input_video: str,
23
  model: str,
24
+ features: str,
 
25
  track_points: str,
26
  model_threshold: str,
27
  ):
 
36
  model = YOLO(models_path[model])
37
  video = Video(input_path=input_video, output_path=output_path)
38
 
39
+ motion_estimation = len(features) > 0 and (
40
+ features[0] == 0 or (len(features) > 1 and features[1] == 0)
41
+ )
42
+
43
+ drawing_paths = len(features) > 0 and (
44
+ features[0] == 1 or (len(features) > 1 and features[1] == 1)
45
+ )
46
+
47
  if motion_estimation and drawing_paths:
48
  fix_paths = True
49
 
 
58
  distance_threshold = (
59
  DISTANCE_THRESHOLD_BBOX if track_points == "bbox" else DISTANCE_THRESHOLD_CENTROID
60
  )
61
+
62
  tracker = Tracker(
63
  distance_function=distance_function,
64
  distance_threshold=distance_threshold,