-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstereoscopics_server_thread_pool.py
More file actions
372 lines (317 loc) · 11.8 KB
/
Copy pathstereoscopics_server_thread_pool.py
File metadata and controls
372 lines (317 loc) · 11.8 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Imports
import cv2
import time
from threading import Event, Thread, Lock
import imutils
import argparse
import sys
from collections import deque
import numpy as np
import socket
import io
HOST = '169.254.116.12'
PORT = 5025
BUFFER_SIZE = 24
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def Stereoscopics(send_stereo_data, led_color, kill_event,send_data_flag):
Old_val = 1
focalsize = 3.04e-03
pixelsize = 1.12e-06
baseline = 0.737
send_stereo_data = send_stereo_data
GREEN = led_color
kill_event = kill_event
send_data_flag = send_data_flag
# Camera settings go here
imageWidth = 1008
imageHeight = 256
frameRate = 20
processingThreads = 4
# Shared values
global running
global cap
global frameLock
global processorPool
running = True
frameLock = Lock()
right_x = 1600
COLOR = "PINK"
# Setup the camera
cap = cv2.VideoCapture(0)
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, imageWidth);
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, imageHeight);
cap.set(cv2.CAP_PROP_FPS, frameRate);
if not cap.isOpened():
cap.open()
if COLOR == "PINK":
hsvLower = (160, 60, 60)
hsvUpper = (170, 255, 255)
if COLOR == "RED":
hsvLower = (0, 50, 50) # currently set for red
hsvUpper = (5, 255, 255)
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
if not (self.isEmpty()):
return self.items[len(self.items) - 1]
else:
return None
def size(self):
return len(self.items)
output_data = Stack()
def gpio_blinker(led_color, loop_count):
if loop_count % 2 == 0:
led_color.on()
else:
led_color.off()
def loop_counter(loop_number):
loop_number += 1
if loop_number >= 10:
loop_number = 1
return loop_number
# Image processing thread, self-starting
class ImageProcessor(Thread):
def __init__(self, name, output_data, autoRun=True):
super(ImageProcessor, self).__init__()
self.event = Event()
self.output_data = output_data
self.eventWait = (2.0 * processingThreads) / frameRate
self.name = str(name)
self.imageWidth = 1008
self.imageHeight = 256
print('Processor thread %s started with idle time of %.2fs' % (self.name, self.eventWait))
self.start()
def run(self):
# This method runs in a separate thread
global running
global frameLock
global processorPool
while running:
# Wait for an image to be written to the stream
self.event.wait(self.eventWait)
if self.event.isSet():
if not running:
break
try:
self.ProcessImage(self.nextFrame)
finally:
# Reset the event
self.nextFrame = None
self.event.clear()
# Return ourselves to the pool at the back
with frameLock:
processorPool.insert(0, self)
print('Processor thread %s terminated' % (self.name))
def ProcessImage(self, image):
frame = image
frame = imutils.resize(frame, width=self.imageWidth,)
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, hsvLower, hsvUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
M = cv2.moments(c)
centroid = (round((M["m10"] / M["m00"]), 3), round((M["m01"] / M["m00"]), 3))
centroid = (centroid[0] * 3280 / imageWidth, centroid[1] * 2464 / imageHeight)
self.output_data.push(int(centroid[0]))
if processingThreads ==1:
cv2.imshow("Frame", mask) # mask
key = cv2.waitKey(1) & 0xFF
# Image capture thread, self-starting
class ImageCapture(Thread):
def __init__(self):
super(ImageCapture, self).__init__()
self.start()
# Stream delegation loop
def run(self):
# This method runs in a separate thread
global running
global cap
global processorPool
global frameLock
while running:
# Grab the oldest unused processor thread
with frameLock:
if processorPool:
processor = processorPool.pop()
else:
processor = None
if processor:
# Grab the next frame and send it to the processor
success, frame = cap.read()
if success:
processor.nextFrame = frame
processor.event.set()
else:
print('Capture stream lost...')
running = False
else:
# When the pool is starved we wait a while to allow a processor to finish
time.sleep(0.01)
print('Capture thread terminated')
def receive_data():
try:
start1 = time.time()
data = clientPort.recv(BUFFER_SIZE)
client_data = data.decode()
# print(client_data)
tempData = "".join(client_data)
tempData = tempData.strip("<")
tempData = tempData.strip(">")
tempData = tempData.split("><")
# print(tempData)
try:
index = int(len(tempData) - 1)
# print(tempData[index])
if tempData[index] is not None:
right_x = float(tempData[index])
# print(right_x)
# Old_val = right_x
return right_x
except ValueError:
# right_x = Old_val
# return right_x
pass
except socket.error:
print("[Stereo] : missed client data")
return 'NULL'
else:
loop = time.time() - start1
if loop > 0.1:
print("[Stereo]: Receive TCP data: ", round(loop,2))
connected = False
while not connected and not kill_event.is_set(): # and not shutdown_event.is_set() and not kill_event.is_set():
try:
s.bind((HOST, PORT))
s.listen(1)
clientPort, addr = s.accept()
connected = True
print("[Stereo] : Client connected")
# return clientPort
except socket.error as se:
print('[Stereo] : No Client', se)
sys.stdout.flush()
time.sleep(3)
continue
except Exception as e:
print('[Stereo] : No Client' + str(e))
sys.stdout.flush()
time.sleep(3)
continue
# Create some threads for processing and frame grabbing
processorPool = [ImageProcessor(i + 1, output_data, ) for i in range(processingThreads)]
allProcessors = processorPool[:]
captureThread = ImageCapture()
# Main loop ___________________________________________________________
# The captureThread gets the frames and passes them to an unused processing thread
try:
time.sleep(2) #<< WAIT FOR CAMERA TO STARTUP
start_time = time.time()
stereo_loop_count =1
while running:
try:
time_since_last = time.time()
gpio_blinker(GREEN, stereo_loop_count)
stereo_loop_count = loop_counter(stereo_loop_count)
# LIMIT DATA SPEED:
while (time.time() - time_since_last) <= 0.06:
time.sleep(0.01)
left_x = output_data.peek()
right_x = receive_data()
# print("[Stereo]: receive_data: ", round(time.time() - time_since_last,4))
# print("Left: ", left_x," Right: ",right_x)
disparity = abs(left_x - float(right_x))
if disparity == 0:
disparity = 1
distance = round((focalsize * baseline) / (disparity * pixelsize), 2)
# print("[STEREO:] Distance: ", distance)
data = "<",str(right_x), ",", str(left_x), ",", str(distance),">"
if send_data_flag.is_set():
# print(right_x)
send_stereo_data.put(data)
send_data_flag.clear()
# print("[Stereo]: Put to Queue: ", round(time.time() - time_since_last,4))
# print("[Stereo]: Full Loop: ", round(time.time() - time_since_last,4))
# AVG FPS COUNTER:
# time_per_loop = round((time.time() - start_time)/stereo_loop_count,4)
# avg_fps = round(1/time_per_loop,2)
# print("[ImageProcessors] Avg FPS: ", avg_fps)
except IndexError as i:
pass
except KeyboardInterrupt:
print('\nUser shutdown')
except:
e = sys.exc_info()
print()
print(e)
print('\nUnexpected error, shutting down!')
# Cleanup all processing threads
running = False
while allProcessors:
# Get the next running thread
with frameLock:
processor = allProcessors.pop()
# Send an event and wait until it finishes
processor.event.set()
processor.join()
# Cleanup the capture thread
captureThread.join()
# Cleanup the camera object
cap.release()
if __name__ == "__main__":
import multiprocessing as mp
from gpiozero import LED
stereo_data = mp.Queue()
kill_event = mp.Event()
data_flag = mp.Event()
GREEN = LED(16)
YELLOW = LED(26)
led_color = GREEN
def gpio_blinker(led_color, loop_count):
if loop_count % 2 == 0:
led_color.on()
else:
led_color.off()
def loop_counter(loop_number):
loop_number += 1
if loop_number >= 10:
loop_number = 1
return loop_number
Stereo = mp.Process(target=Stereoscopics, args=[stereo_data, led_color, kill_event, data_flag])
Stereo.daemon = True
Stereo.start()
# print("starting loop")
stereo_loop_count = 1
while True:
start_time = time.time()
gpio_blinker(YELLOW, stereo_loop_count)
stereo_loop_count = loop_counter(stereo_loop_count)
while (time.time() - start_time)< 0.06:
time.sleep(0.01)
# print("Getting Data")
data_flag.set()
try:
data = stereo_data.get(timeout=0.05)
except:
pass
else:
tempData = "".join(data)
tempData = tempData.strip("<")
tempData = tempData.strip(">")
tempData = tempData.split(",")
RightX = int(float(tempData[0]))
LeftX = int(float(tempData[1]))
stereoDist = float(tempData[2])
# print("Distance: ",stereoDist," LeftX: ", LeftX, " RightX: ", RightX)