-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_recorder.py
87 lines (69 loc) · 3.33 KB
/
simple_recorder.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
# Copyright (c) Prophesee S.A.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
"""
Sample code that demonstrates how to use Metavision SDK to record events from a live camera in a RAW file
"""
import sys
print(sys.path)
# Add /usr/lib/python3/dist-packages/ to PYTHONPATH if the output of print(sys.path) does not mention it.
sys.path.append("/usr/lib/python3/dist-packages/")
from metavision_core.event_io.raw_reader import initiate_device
from metavision_core.event_io import EventsIterator
from metavision_sdk_core import PeriodicFrameGenerationAlgorithm, ColorPalette
from metavision_sdk_ui import EventLoop, BaseWindow, MTWindow, UIAction, UIKeyEvent
import argparse
import time
import os
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description='Metavision RAW file Recorder sample.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-o', '--output-dir', default="", help="Directory where to create RAW file with recorded event data")
args = parser.parse_args()
return args
def main():
""" Main """
args = parse_args()
# HAL Device on live camera
device = initiate_device("")
# Start the recording
if device.get_i_events_stream():
log_path = "recording_" + time.strftime("%y%m%d_%H%M%S", time.localtime()) + ".raw"
if args.output_dir != "":
log_path = os.path.join(args.output_dir, log_path)
print(f'Recording to {log_path}')
device.get_i_events_stream().log_raw_data(log_path)
# Events iterator on Device
mv_iterator = EventsIterator.from_device(device=device)
height, width = mv_iterator.get_size() # Camera Geometry
# Window - Graphical User Interface
with MTWindow(title="Metavision Events Viewer", width=width, height=height,
mode=BaseWindow.RenderMode.BGR) as window:
def keyboard_cb(key, scancode, action, mods):
if key == UIKeyEvent.KEY_ESCAPE or key == UIKeyEvent.KEY_Q:
window.set_close_flag()
window.set_keyboard_callback(keyboard_cb)
# Event Frame Generator
event_frame_gen = PeriodicFrameGenerationAlgorithm(sensor_width=width, sensor_height=height, fps=25,
palette=ColorPalette.Dark)
def on_cd_frame_cb(ts, cd_frame):
window.show_async(cd_frame)
event_frame_gen.set_output_callback(on_cd_frame_cb)
# Process events
for evs in mv_iterator:
# Dispatch system events to the window
EventLoop.poll_and_dispatch()
event_frame_gen.process_events(evs)
if window.should_close():
# Stop the recording
device.get_i_events_stream().stop_log_raw_data()
break
if __name__ == "__main__":
main()