This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
142 lines (126 loc) · 4.4 KB
/
Main.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
"""
Main entrypoint
"""
import argparse
import asyncio
import Constants
from utils.timer import print_timing_results, timing
@timing
def process_video():
"""
Processes the video defined in Constants.
Saves frames for debugging, outputs video and saves results as csv.
"""
output_video = prepare_video_output(Constants.INPUT_PATH, Constants.MATCHER_TYPE.value, Constants.FROM_SEC_OR_IMAGE, Constants.TO_SEC_OR_IMAGE, Constants.OUTPUT_FPS, Constants.INPUT_DIMENSIONS)
detected_objects = DetectedObjects()
for frame_number, frame in enumerate(get_frames(Constants.INPUT_DATA_TYPE, Constants.INPUT_PATH, Constants.FROM_SEC_OR_IMAGE, Constants.TO_SEC_OR_IMAGE, undistort=False)):
result = detect(frame)
newly_detected_objects = create_objects(result, frame)
detected_objects.add_objects(newly_detected_objects)
result_frame = visualize.draw_instances(frame, detected_objects)
print(f"Frame {frame_number}: detected {len(newly_detected_objects)} objects. {len(detected_objects.objects)} total objects")
show(result_frame, "Frame", await_keypress=False)
asyncio.run(save_debug_image(result_frame, "frame_" + str(frame_number)))
output_video.write(result_frame)
output_video.release()
write_detected_objects_to_csv(detected_objects, "test")
def main():
"""
Main entry point.
"""
process_video()
print_timing_results()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process video")
parser.add_argument(
"input",
type=str,
help="Video file or directory of images to be processed"
)
parser.add_argument(
"--from",
dest="from_sec_or_image",
type=int,
default=0,
help="From video second or image number"
)
parser.add_argument(
"--to",
dest="to_sec_or_image",
type=int,
default=None,
help="To video second or image number"
)
parser.add_argument(
"--inputType",
dest="inputType",
type=Constants.InputDataType,
choices=list(Constants.InputDataType),
default=Constants.InputDataType.VIDEO,
help="Input type can be a VIDEO or a directory with IMAGEs",
)
parser.add_argument(
"--inputDimensions",
dest="inputDimensions",
nargs=2,
metavar=("x", "y"),
type=int,
help="Input dimensions for video or image series"
)
parser.add_argument(
"--inputScale",
dest="inputScale",
type=float,
default=1,
help="Scale compared to original video (e.g. 0.5)"
)
parser.add_argument(
"--matcherType",
dest="matcherType",
type=Constants.MatcherType,
choices=list(Constants.MatcherType),
default=Constants.MatcherType.ORB,
help="Matcher type can be SIFT, SURF or ORB"
)
parser.add_argument(
"--cameraType",
dest="cameraType",
type=Constants.CameraType,
choices=list(Constants.CameraType),
default=Constants.CameraType.IPHONE_XR_4K_60,
help="Camera type can be IPHONE_XR_4K_60, IPHONE_8_PLUS_4K_60 or FL2_14S3C_C",
)
parser.add_argument(
"--inputFps",
dest="inputFps",
type=int,
default=60,
help="Fps of input video"
)
parser.add_argument(
"--outputFps",
dest="outputFps",
type=int,
default=10,
help="Fps for output video"
)
args = parser.parse_args()
print(args)
Constants.INPUT_PATH = args.input
Constants.FROM_SEC_OR_IMAGE = args.from_sec_or_image
Constants.TO_SEC_OR_IMAGE = args.to_sec_or_image
Constants.INPUT_DATA_TYPE = args.inputType
Constants.INPUT_DIMENSIONS = tuple(args.inputDimensions)
Constants.VIDEO_SCALE = args.inputScale
Constants.MATCHER_TYPE = args.matcherType
Constants.CAMERA_TYPE = args.cameraType
Constants.INPUT_FPS = args.inputFps
Constants.OUTPUT_FPS = args.outputFps
# Constants need to be set before imports so that they are taken into account
from data_model.DetectedObjects import DetectedObjects
from data_model.ObjectInstance import create_objects
from mrcnn import visualize
from mrcnn.Mask_R_CNN_COCO import detect
from utils.image_utils import save_debug_image, show, prepare_video_output, get_frames
from utils.export_utils import write_detected_objects_to_csv
main()