-
Notifications
You must be signed in to change notification settings - Fork 14
/
stand_alone_threaded.py
289 lines (214 loc) · 8.49 KB
/
stand_alone_threaded.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
276
277
278
279
280
281
282
283
284
285
286
287
288
from __future__ import print_function
import time
import multiprocessing
from multiprocessing import Process, Queue, Value
import numpy as np
from numpy import int16, uint8, log2
import cv2
import pydvs.generate_spikes as gs
import sys
import argparse
MODE_128 = "128"
MODE_64 = "64"
MODE_32 = "32"
MODE_16 = "16"
UP_POLARITY = "UP"
DOWN_POLARITY = "DOWN"
MERGED_POLARITY = "MERGED"
POLARITY_DICT = {UP_POLARITY: uint8(0),
DOWN_POLARITY: uint8(1),
MERGED_POLARITY: uint8(2),
0: UP_POLARITY,
1: DOWN_POLARITY,
2: MERGED_POLARITY}
OUTPUT_RATE = "RATE"
OUTPUT_TIME = "TIME"
OUTPUT_TIME_BIN = "TIME_BIN"
OUTPUT_TIME_BIN_THR = "TIME_BIN_THR"
BEHAVE_MICROSACCADE = "SACCADE"
BEHAVE_ATTENTION = "ATTENTION"
BEHAVE_TRAVERSE = "TRAVERSE"
BEHAVE_FADE = "FADE"
IMAGE_TYPES = ["png", 'jpeg', 'jpg']
# -------------------------------------------------------------------- #
# grab / rescale frame #
def grab_first(dev, res):
_, raw = dev.read()
height, width, _ = raw.shape
new_height = res
new_width = int( float(new_height*width)/float(height) )
col_from = (new_width - res)//2
col_to = col_from + res
img = cv2.resize(cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY).astype(int16),
(new_width, new_height))[:, col_from:col_to]
return img, new_width, new_height, col_from, col_to
def grab_frame(dev, width, height, col_from, col_to):
_, raw = dev.read()
img = cv2.resize(cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY).astype(int16),
(width, height))[:, col_from:col_to]
return img
# -------------------------------------------------------------------- #
# process image thread function #
def processing_thread(img_queue, spike_queue, running, max_time_ms):
#~ start_time = time.time()
WINDOW_NAME = 'spikes'
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_AUTOSIZE)
cv2.startWindowThread()
spikes = np.zeros(shape, dtype=int16)
diff = np.zeros(shape, dtype=int16)
abs_diff = np.zeros(shape, dtype=int16)
# just to see things in a window
spk_img = np.zeros((height, width, 3), uint8)
num_bits = 6 # how many bits are used to represent exceeded thresholds
num_active_bits = 2 # how many of bits are active
log2_table = gs.generate_log2_table(num_active_bits, num_bits)[num_active_bits - 1]
spike_lists = None
pos_spks = None
neg_spks = None
max_diff = 0
while True:
img = img_queue.get()
if img is None or running.value == 0:
running.value = 0
break
# do the difference
diff[:], abs_diff[:], spikes[:] = gs.thresholded_difference(img, ref, threshold)
# inhibition ( optional )
if is_inh_on:
spikes[:] = gs.local_inhibition(spikes, abs_diff, inh_coords,
width, height, inh_width)
# update the reference
ref[:] = gs.update_reference_time_binary_thresh(abs_diff, spikes, ref,
threshold, max_time_ms,
num_active_bits,
history_weight,
log2_table)
# convert into a set of packages to send out
neg_spks, pos_spks, max_diff = gs.split_spikes(spikes, abs_diff, polarity)
# this takes too long, could be parallelized at expense of memory
spike_lists = gs.make_spike_lists_time_bin_thr(pos_spks, neg_spks,
max_diff,
up_down_shift, data_shift, data_mask,
max_time_ms,
threshold,
max_threshold,
num_bits,
log2_table)
spike_queue.put(spike_lists)
spk_img[:] = gs.render_frame(spikes, img, cam_res, cam_res, polarity)
cv2.imshow (WINDOW_NAME, spk_img.astype(uint8))
if cv2.waitKey(1) & 0xFF == ord('q'):
running.value = 0
break
#~ end_time = time.time()
#~
#~ if end_time - start_time >= 1.0:
#~ print("%d frames per second"%(frame_count))
#~ frame_count = 0
#~ start_time = time.time()
#~ else:
#~ frame_count += 1
cv2.destroyAllWindows()
cv2.waitKey(1)
running.value = 0
# -------------------------------------------------------------------- #
# send image thread function #
def emitting_thread(spike_queue, running):
while True:
spikes = spike_queue.get()
if spikes is None or running.value == 0:
running.value = 0
break
# Add favourite mechanisms to get spikes out of the pc
# print("sending!")
running.value = 0
#----------------------------------------------------------------------#
# global variables #
parser = argparse.ArgumentParser()
parser.add_argument("--video_id", default='0', required=False, type=str)
parser.add_argument("--res", default=MODE_128, required=False, type=int)
args = parser.parse_args()
video_dev_id = args.video_id
if len(video_dev_id) < 4:
# Assume that urls have at least 4 characters
video_dev_id = int(video_dev_id)
mode = args.res
cam_res = int(mode)
#cam_res = 256 <- can be done, but spynnaker doesn't suppor such resolution
width = cam_res # square output
height = cam_res
shape = (height, width)
data_shift = uint8( log2(cam_res) )
up_down_shift = uint8(2*data_shift)
data_mask = uint8(cam_res - 1)
polarity = POLARITY_DICT[ MERGED_POLARITY ]
output_type = OUTPUT_TIME
history_weight = 1.0
threshold = 12 # ~ 0.05*255
max_threshold = 180 # 12*15 ~ 0.7*255
scale_width = 0
scale_height = 0
col_from = 0
col_to = 0
curr = np.zeros(shape, dtype=int16)
ref = 128*np.ones(shape, dtype=int16)
# -------------------------------------------------------------------- #
# inhibition related #
inh_width = 2
is_inh_on = False
inh_coords = gs.generate_inh_coords(width, height, inh_width)
def main():
# -------------------------------------------------------------------- #
# camera/frequency related #
video_dev = cv2.VideoCapture(video_dev_id) # webcam
#~ video_dev = cv2.VideoCapture('./120fps HFR Sample.mp4') # webcam
#ps3 eyetoy can do 125fps
try:
video_dev.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
video_dev.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
video_dev.set(cv2.CAP_PROP_FPS, 125)
except:
pass
fps = video_dev.get(cv2.CAP_PROP_FPS)
max_time_ms = int(1000./fps)
# -------------------------------------------------------------------- #
# threading related #
running = Value('i', 1)
spike_queue = Queue()
spike_emitting_proc = Process(target=emitting_thread,
args=(spike_queue, running))
spike_emitting_proc.start()
img_queue = Queue()
#~ spike_gen_proc = Process(target=self.process_frame, args=(img_queue,))
spike_gen_proc = Process(target=processing_thread,
args=(img_queue, spike_queue, running, max_time_ms))
spike_gen_proc.start()
# -------------------------------------------------------------------- #
# main loop #
is_first_pass = True
while(running.value == 1):
# get an image from video source
if is_first_pass:
curr, scale_width, scale_height, col_from, col_to = grab_first(video_dev, cam_res)
is_first_pass = False
else:
curr = grab_frame(video_dev, scale_width, scale_height, col_from, col_to)
img_queue.put(curr)
img_queue.put(None)
spike_gen_proc.join()
print("generation thread stopped")
spike_queue.put(None)
spike_emitting_proc.join()
print("emission thread stopped")
if video_dev is not None:
video_dev.release()
if __name__ == '__main__':
if sys.platform.startswith('win'):
# This allows the cv2 window to update.
main()
elif sys.version_info[0] >= 3 and sys.version_info[1] >= 4:
# This allows the cv2 window to update.
multiprocessing.set_start_method('spawn')
main()
else:
print ("This demo must be run in Python 3.4 and higher or Windows")