-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer.py
executable file
·275 lines (240 loc) · 11.2 KB
/
producer.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
producer of DVS frames for classification of hand symbols by consumer processs
Authors: Tobi Delbruck Nov 2020, Oct 2024
"""
import atexit
import pickle
from pathlib import Path
from typing import Optional, Union
import cv2
import sys
import math
import time
import numpy.ma as ma
import socket
import numpy as np
from globals_and_utils import *
from engineering_notation import EngNumber as eng # only from pip
import argparse
import multiprocessing.connection as mpc
from multiprocessing import Pipe,Queue
from pyaer.davis import DAVIS
from pyaer.dvs128 import DVS128
from pyaer import libcaer
from my_logger import my_logger
log=my_logger(__name__)
def producer(queue:Queue):
""" produce frames for consumer
:param queue: possible Queue to send to instead of UDP socket
"""
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_address = ('', PORT)
EVENT_COUNT_PER_FRAME = 1500
record=None
spacebar_records=True
log.info(f'recording to {record} with spacebar_records={spacebar_records}')
recording_folder = None
if record is not None:
recording_folder = os.path.join(DATA_FOLDER, 'recordings', record)
log.info(f'recording frames to {recording_folder}')
Path(recording_folder).mkdir(parents=True, exist_ok=True)
def open_camera()->Optional[Union[DVS128,DAVIS]]:
""" Opens the DAVIS camera, set biases, and returns device handle to it
:return: device handle
"""
nonlocal EVENT_COUNT_PER_FRAME
dvs=None
for c in CAMERA_TYPES:
try:
dvs=c(noise_filter=True) # open the camera
log.info(f'opened camera {dvs}')
break
except Exception as e:
log.warning(f'cannot open camera type {c}: {e}')
if dvs is None:
return None
EVENT_COUNT_PER_FRAME=None
if type(dvs) is DVS128:
EVENT_COUNT_PER_FRAME=1500
elif type(dvs) is DAVIS:
EVENT_COUNT_PER_FRAME=5000
else:
raise Exception(f'{dvs} type is unknown')
log.info(f'set accumulated event count/frame to {EVENT_COUNT_PER_FRAME} for camera {dvs}')
print("DVS USB ID:", dvs.device_id)
if dvs.device_is_master:
print("DVS is master.")
else:
print("DVS is slave.")
print("DVS Serial Number:", dvs.device_serial_number)
print("DVS String:", dvs.device_string)
print("DVS USB bus Number:", dvs.device_usb_bus_number)
print("DVS USB device address:", dvs.device_usb_device_address)
print("DVS size X:", dvs.dvs_size_X)
print("DVS size Y:", dvs.dvs_size_Y)
try:
print("Logic Version:", dvs.logic_version)
except:
print("DVS128 camera has no logic version information available")
try:
print("Background Activity Filter:",
dvs.dvs_has_background_activity_filter)
except:
print("DVS128 has no background activity denoising filter")
try:
print("Color Filter", dvs.aps_color_filter, type(dvs.aps_color_filter))
print(dvs.aps_color_filter == 1)
except:
print("DVS128 has no color filter setting to print or set")
# device.start_data_stream()
assert (dvs.send_default_config())
# attempt to set up USB host buffers for acquisition thread to minimize latency
assert (dvs.set_config(
libcaer.CAER_HOST_CONFIG_USB,
libcaer.CAER_HOST_CONFIG_USB_BUFFER_NUMBER,
8))
assert (dvs.set_config(
libcaer.CAER_HOST_CONFIG_USB,
libcaer.CAER_HOST_CONFIG_USB_BUFFER_SIZE,
4096))
assert (dvs.data_start())
assert (dvs.set_config(
libcaer.CAER_HOST_CONFIG_PACKETS,
libcaer.CAER_HOST_CONFIG_PACKETS_MAX_CONTAINER_INTERVAL,
1000)) # set max interval to this value in us. Set to not produce too many packets/sec here, not sure about reasoning
assert (dvs.set_data_exchange_blocking())
# setting bias after data stream started
bias_json_file=CAMERA_TO_BIASES_DICT[type(dvs).__name__]
log.info(f'setting biases from {bias_json_file}')
dvs.set_bias_from_json(bias_json_file)
return dvs
def write_next_image(dir:str, idx:int, img):
""" Saves data sample image
:param dir: the folder
:param idx: the current index number
:param img: the image to save, which should be monochrome uint8 and which is saved as default png format
:returns: the next index
"""
while True:
n=f'{dir}/{idx:04d}.png'
if not os.path.isfile(n):
break
idx+=1
try:
cv2.imwrite(n, img)
except Exception as e:
log.error(f'error saving {n}: caught {e}')
return idx
dvs=None
histrange = [(0, v) for v in (IMSIZE, IMSIZE)] # allocate DVS frame histogram to desired output size
npix = IMSIZE * IMSIZE
cv2_resized = False
last_cv2_frame_time = time.time()
frame=None
frame_number=0
recording_frame_number = 0
time_last_frame_sent=time.time()
frames_dropped_counter=0
save_next_frame=not spacebar_records # if we don't supply the option, it will be False and we want to then save all frames
def cleanup():
log.info('closing {}'.format(dvs))
dvs.shutdown()
cv2.destroyAllWindows()
atexit.register(cleanup)
try:
numpy_file = None # TODO uncomment to save data f'{DATA_FOLDER}/producer-frame-rate-{timestr}.npy'
while True:
if dvs is None:
dvs=open_camera()
if dvs is None:
time.sleep(5)
continue
with Timer('overall producer frame rate', numpy_file=numpy_file , show_hist=SHOW_STATISTICS_AT_END) as timer_overall:
with Timer('accumulate DVS'):
events = None
while events is None or len(events)<EVENT_COUNT_PER_FRAME:
# pol_events, num_pol_event,_, _, _, _, _, _ = dvs.get_event()
pol_events, num_pol_event, *_ = dvs.get_event() # ignore other return values since only brightness change events are used from DVS and DAVIS
# assemble 'frame' of EVENT_COUNT events
if num_pol_event>0:
if events is None:
events=pol_events
else:
events = np.vstack([events, pol_events]) # otherwise tack new events to end
# log.debug('got {} events (total so far {}/{} events)'
# .format(num_pol_event, 0 if events is None else len(events), EVENT_COUNT))
dtMs = (time.time() - time_last_frame_sent)*1e3
if recording_folder is None and dtMs<MIN_PRODUCER_FRAME_INTERVAL_MS:
log.debug(f'frame #{frames_dropped_counter} after only {dtMs:.3f}ms, discarding to collect newer frame')
frames_dropped_counter+=1
continue # just collect another frame since it will be more timely
log.debug(f'after dropping {frames_dropped_counter} frames, got one after {dtMs:.1f}ms')
frames_dropped_counter=0
with Timer('normalization'):
# if frame is None: # debug timing
# take DVS coordinates and scale x and y to output frame dimensions using flooring math
xfac = float(IMSIZE) / dvs.dvs_size_X
yfac = float(IMSIZE) / dvs.dvs_size_Y
events[:,1]=np.floor(events[:,1]*xfac)
events[:,2]=np.floor(events[:,2]*yfac)
frame, _, _ = np.histogram2d(events[:, 2], events[:, 1], bins=(IMSIZE, IMSIZE), range=histrange)
fmax_count=np.max(frame)
frame[frame > EVENT_COUNT_CLIP_VALUE]=EVENT_COUNT_CLIP_VALUE
frame= (255. / EVENT_COUNT_CLIP_VALUE) * frame # max pixel will have value 255
# statistics
focc=np.count_nonzero(frame)
frame=frame.astype('uint8')
log.debug('from {} events, frame has occupancy {}% max_count {:.1f} events'.format(len(events), eng((100.*focc)/npix), fmax_count))
with Timer('send frame'):
time_last_frame_sent=time.time()
data = pickle.dumps((frame_number, time_last_frame_sent, frame)) # send frame_number to allow determining dropped frames in consumer
frame_number+=1
client_socket.sendto(data, udp_address)
if recording_folder is not None and save_next_frame:
recording_frame_number=write_next_image(recording_folder,recording_frame_number,frame)
print('.',end='')
if recording_frame_number%80==0:
print('')
if SHOW_DVS_OUTPUT:
t=time.time()
if t-last_cv2_frame_time>1./MAX_SHOWN_DVS_FRAME_RATE_HZ:
last_cv2_frame_time=t
with Timer('show DVS image'):
# min = np.min(frame)
# img = ((frame - min) / (np.max(frame) - min))
cv2.namedWindow('DVS', cv2.WINDOW_NORMAL)
cv2.imshow('DVS', 1-frame.astype('float')/255)
if not cv2_resized:
cv2.resizeWindow('DVS', 600, 600)
cv2_resized = True
k= cv2.waitKey(1) & 0xFF
if k== ord('q'):
if recording_folder is not None:
log.info(f'*** recordings of {recording_frame_number - 1} frames saved in {recording_folder}')
break
elif k==ord('p'):
print_timing_info()
elif spacebar_records and k==ord(' '):
save_next_frame=True
else:
save_next_frame=not spacebar_records
except KeyboardInterrupt:
dvs.shutdown()
if recording_folder is not None:
log.info(f'*** recordings of {recording_frame_number-1} frames saved in {recording_folder}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='producer: Generates DVS frames for roshambo to process in consumer', allow_abbrev=True,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"--record", type=str, default=None,
help="record DVS frames into folder DATA_FOLDER/collected/<name>")
parser.add_argument(
"--spacebar_records", action='store_true',
help="only record when spacebar pressed down")
args = parser.parse_args()
try:
producer(queue=None)
except Exception as e:
log.error(f'Error: {e}')
sys.exit()