forked from cuppp1998/360_object_tracking
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObject_Detection.py
More file actions
175 lines (150 loc) · 5.49 KB
/
Object_Detection.py
File metadata and controls
175 lines (150 loc) · 5.49 KB
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
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'detectron2'))
import argparse
import time
import torch
import cv2
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
from detectron2.structures.instances import Instances
from detectron2.structures.boxes import Boxes
from panoramic_detection import improved_OD as OD
from panoramic_detection.improved_OD import load_model
# function used to realize object detection on a panoramic video
def Object_Detection(
input_video_path,
output_video_path,
classes_to_detect=[0, 1, 2, 3, 5, 7, 9],
FOV=120,
THETAs=[0, 90, 180, 270],
PHIs=[-10, -10, -10, -10],
model_type="YOLO",
score_threshold=0.4,
nms_threshold=0.45,
use_mymodel=True,
min_size=640, # min_size will be used as width for resizing
max_size=10000,
):
# read the input panoramic video (of equirectangular projection)
video_capture = cv2.VideoCapture(input_video_path)
# if the input path is not right, warn the user
if video_capture.isOpened() == False:
print("Can not open the video file.")
# if right, get some info about the video (width, height, frame count and fps)
else:
video_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
video_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
video_frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
video_fps = int(round(video_capture.get(cv2.CAP_PROP_FPS)))
# fourcc = cv2.VideoWriter_fourcc(*'MJPG')
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
outputfile = cv2.VideoWriter(
output_video_path, fourcc, video_fps, (video_width, video_height)
)
# print(video_frame_count)
# output the video info
print(
"The input video is "
+ str(video_width)
+ " in width and "
+ str(video_height)
+ " in height."
)
print(f"resizing the width to {min_size}")
print("Loading Model...")
# load the pretrained detection model
model, cfg, yolo_cfg = load_model(model_type, min_size, max_size, video_width / video_height, score_threshold, nms_threshold)
print("Model Loaded!")
# the number of current frame
num_of_frame = 1
# for each image frame in the video
while video_capture.grab():
time1 = time.time()
# get the next image frame
_, im = video_capture.retrieve()
# get the predictions on the current frame
bboxes_all, classes_all, scores_all = OD.predict_one_frame(
FOV,
THETAs,
PHIs,
im,
model,
video_width,
video_height,
classes_to_detect,
False,
use_mymodel,
model_type,
False, # False means do not split image2
yolo_cfg
)
# create an instance of detectron2 so that the output can be visualized
output_new = Instances(
image_size=[video_width, video_height],
pred_boxes=Boxes(torch.tensor(bboxes_all)),
scores=torch.tensor(scores_all),
pred_classes=torch.tensor(classes_all),
)
# use `Visualizer` to draw the predictions on the image
v = Visualizer(
im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.0
)
im = v.draw_instance_predictions(output_new.to("cpu"))
outputfile.write(im.get_image()[:, :, ::-1])
# show the current FPS
time2 = time.time()
if num_of_frame % 5 == 0:
print(num_of_frame, "/", video_frame_count)
print(str(1 / (time2 - time1)) + " fps")
num_of_frame += 1
# release the input and output videos
video_capture.release()
outputfile.release()
print("Output Finished!")
def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument("--input_video_path", required=True, type=str)
parser.add_argument("--output_video_path", required=True, type=str)
parser.add_argument(
"--classes_to_detect", nargs="+", type=int, default=[0, 1, 2, 3, 5, 7, 9]
)
parser.add_argument("--FOV", type=int, default=120)
parser.add_argument("--THETAs", nargs="+", type=int, default=[0, 90, 180, 270])
parser.add_argument("--PHIs", nargs="+", type=int, default=[-10, -10, -10, -10])
parser.add_argument("--sub_image_width", type=int, default=640)
parser.add_argument(
"--model_type", type=str, choices=["YOLO", "Faster RCNN"], default="YOLO"
)
parser.add_argument("--score_threshold", type=float, default=0.5)
parser.add_argument("--nms_threshold", type=float, default=0.5)
parser.add_argument("--use_mymodel", default=True, type=boolean_string)
parser.add_argument(
'-s', '--short_edge_size',
type=int,
default=0,
help='the length of short edge, default to 0 which means use the original image size'
)
opt = parser.parse_args()
# print(opt)
return opt
def boolean_string(s):
if s not in {"False", "True"}:
raise ValueError("Not a valid boolean string")
return s == "True"
def main(opt):
Object_Detection(
opt.input_video_path,
opt.output_video_path,
opt.classes_to_detect,
opt.FOV,
opt.THETAs,
opt.PHIs,
opt.model_type,
opt.score_threshold,
opt.nms_threshold,
opt.use_mymodel,
opt.short_edge_size
)
if __name__ == "__main__":
opt = parse_opt()
main(opt)