-
Notifications
You must be signed in to change notification settings - Fork 2
/
heat_mapping.py
73 lines (50 loc) · 1.97 KB
/
heat_mapping.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
import argparse
import cv2
from tqdm import tqdm
import utils.processing as p
from core.stream import Stream
def get_arguments():
"""Gets arguments from the command line.
Returns:
A parser with the input arguments.
"""
parser = argparse.ArgumentParser(usage='Loads a video and builds its heat map.')
parser.add_argument('source', help='Identifier to file that should be loaded as a video.', type=str)
parser.add_argument('--write', help='Writes heat map to disk.', action='store_true')
return parser.parse_args()
if __name__ == '__main__':
# Gathers the input arguments
args = get_arguments()
# Gathers variables from arguments
source = args.source
write = args.write
# Starts an instance from the `Stream` class
v = Stream(source)
# Iterates over amount of possible frames
for i in tqdm(range(v.total_frames)):
# Reads a new frame
valid, frame = v.read()
# Checks if frame is valid
if valid:
# Creates a masked frame
v.masked_frame = cv2.add(v.masked_frame, p.threshold(p.remove_background(frame)))
# Colorizes the masked frame
v.color_masked_frame = p.colorize(v.masked_frame)
# Post-processed frame will be a weighted frame between current frame and colorized mask
v.frame = cv2.addWeighted(frame, 0.5, v.color_masked_frame, 0.5, 0)
# Checks if post-processed frame should be written to disk
if write:
# Outputs post-processed frame to disk
v.output_frame(i+1)
else:
# Shows the post-processed frame
cv2.imshow('video', v.frame)
# If the `q` key is inputted, breaks the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Stops the instance
v.stop()
# Outputs the masks as images
v.output_masks()
# Destroys all windows for cleaning up memory
cv2.destroyAllWindows()