-
Notifications
You must be signed in to change notification settings - Fork 15
/
mwt.py
245 lines (188 loc) · 7.19 KB
/
mwt.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
"""Near-shore Wave Tracking module.
A module for recognition and tracking of multiple nearshore waves
from input videos.
Performance:
mwt.py achieves realtime inference in the presence of multiple tracked
objects for input videos of 1280x720 that are downscaled by a factor of
four at runtime on consumer hardware.
System | Step Time (sec/frame) | Performance
--------------------------------------------------------------------
1 CPU 2.6 GHz Intel Core i5 | 0.015 - 0.030 | 30Hz - 60Hz
Usage:
Please see the README for how to compile the program and run the model.
Created by Justin Fung on 9/1/17.
Copyright 2017 justin fung. All rights reserved.
"""
from __future__ import division
import argparse
import sys
import time
import cv2
import mwt_detection
import mwt_preprocessing
import mwt_tracking
import mwt_io
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"video_path", metavar="path", type=str, help="the path to the input video"
)
def status_update(frame_number, tot_frames):
"""Update status to stdout.
A simple inline status update for stdout.
Prints frame number for every 100 frames completed.
Args:
frame_number: number of frames completed
tot_frames: total number of frames to analyze
"""
if frame_number == 1:
sys.stdout.write("Starting analysis of %d frames...\n" % tot_frames)
sys.stdout.flush()
if frame_number % 100 == 0:
sys.stdout.write("%d" % frame_number)
sys.stdout.flush()
elif frame_number % 10 == 0:
sys.stdout.write(".")
sys.stdout.flush()
if frame_number == tot_frames:
print("End of video reached successfully.")
return
def analyze(video, write_output=True):
"""Analyze the video.
Main routine for analyzing nearshore wave videos. Overlays detected waves
onto orginal frames and writes to a new video. Returns a log with
detected wave attrbutes, frame by frame.
Args:
video: mp4 video
write_output: boolean indicating if a video with tracking overlay
is to be written out.
Returns:
recognized_waves: list of recognized wave objects
wave_log: list of list of wave attributes for csv
time_elapsed: performance of the program in frames/second
"""
# Initiate an empty list of tracked waves, ultimately recognized
# waves, and a log of all tracked waves in each frame.
tracked_waves = []
recognized_waves = []
wave_log = []
# Initialize frame counters.
frame_num = 1
num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
# If an output video is to be made:
if write_output is True:
out = mwt_io.create_video_writer(video)
# Initiate a timer for program performance:
time_start = time.time()
# The main loop is here:
while True:
# Write status update to stdio.
status_update(frame_num, num_frames)
# Read frames until end of clip.
successful_read, original_frame = video.read()
if not successful_read:
break
# Preprocess frames.
analysis_frame = mwt_preprocessing.preprocess(original_frame)
# Detect all sections.
sections = mwt_detection.detect_sections(analysis_frame, frame_num)
# Track all waves in tracked_waves.
mwt_tracking.track(
tracked_waves, analysis_frame, frame_num, num_frames
)
# Write tracked wave stats to wave_log.
for wave in tracked_waves:
wave_log.append(
(
frame_num,
wave.name,
wave.mass,
wave.max_mass,
wave.displacement,
wave.max_displacement,
wave.birth,
wave.death,
wave.recognized,
wave.centroid,
)
)
# Remove dead waves from tracked_waves.
dead_recognized_waves = [
wave
for wave in tracked_waves
if wave.death is not None and wave.recognized is True
]
recognized_waves.extend(dead_recognized_waves)
tracked_waves = [wave for wave in tracked_waves if wave.death is None]
# Remove duplicate waves, keeping earliest wave.
tracked_waves.sort(key=lambda x: x.birth, reverse=True)
for wave in tracked_waves:
other_waves = [wav for wav in tracked_waves if not wav == wave]
if mwt_tracking.will_be_merged(wave, other_waves):
wave.death = frame_num
tracked_waves = [wave for wave in tracked_waves if wave.death is None]
tracked_waves.sort(key=lambda x: x.birth, reverse=False)
# Check sections for any new potential waves and add to
# tracked_waves.
for section in sections:
if not mwt_tracking.will_be_merged(section, tracked_waves):
tracked_waves.append(section)
# analysis_frame = cv2.cvtColor(analysis_frame, cv2.COLOR_GRAY2RGB)
if write_output is True:
# Draw detection boxes on original frame for visualization.
original_frame = mwt_io.draw(
tracked_waves,
original_frame,
# 1)
1 / mwt_preprocessing.RESIZE_FACTOR,
)
# Write frame to output video.
# out.write(analysis_frame)
out.write(original_frame)
# Increment the frame count.
frame_num += 1
# Stop timer here and calc performance.
time_elapsed = time.time() - time_start
performance = num_frames / time_elapsed
# Provide update to user here.
if recognized_waves is not None:
print(f"{len(recognized_waves)} wave(s) recognized.")
print("Program performance: %0.1f frames per second." % performance)
for i, wave in enumerate(recognized_waves):
print(
f"[Wave #{i + 1}] "
f"ID: {wave.name}, "
f"Birth: {wave.birth}, "
f"Death: {wave.death}, "
f"Max Displacement: {wave.max_displacement}, "
f"Max Mass: {wave.max_mass}"
)
else:
print("No waves recognized.")
# Clean-up resources.
if write_output is True:
out.release()
return recognized_waves, wave_log, performance
def main():
"""Define main."""
# CLI.
args = arg_parser.parse_args()
inputfile = args.video_path
# Read video.
print("Checking video from", inputfile)
inputvideo = cv2.VideoCapture(inputfile)
# Exit if video cannot be opened.
if not inputvideo.isOpened():
sys.exit("Could not open video. Exiting.")
# Get a wave log, list of recognized waves, and program performance
# from analyze, as well as create a visualization video.
recognized_waves, wave_log, program_speed = analyze(
inputvideo, write_output=True
)
# Write the wave log to csv.
mwt_io.write_log(wave_log, output_format="json")
# Write the analysis report to txt.
mwt_io.write_report(recognized_waves, program_speed)
# Clean-up resources.
inputvideo.release()
if __name__ == "__main__":
main()