looping over all regions with motion. ugly, but working

This commit is contained in:
blakeblackshear 2019-03-20 07:11:38 -05:00
parent c406fda288
commit 7d3027e056
2 changed files with 62 additions and 57 deletions

View File

@ -52,11 +52,12 @@ def main():
'mask': region_mask, 'mask': region_mask,
# Event for motion detection signaling # Event for motion detection signaling
'motion_detected': mp.Event(), 'motion_detected': mp.Event(),
# create shared array for storing 10 detected objects # array for prepped frame with shape (1, 300, 300, 3)
# note: this must be a double even though the value you are storing 'prepped_frame_array': mp.Array(ctypes.c_uint8, 300*300*3),
# is a float. otherwise it stops updating the value in shared # shared value for storing the prepped_frame_time
# memory. probably something to do with the size of the memory block 'prepped_frame_time': mp.Value('d', 0.0),
'output_array': mp.Array(ctypes.c_double, 6*10) # Lock to control access to the prepped frame
'prepped_frame_lock': mp.Lock()
}) })
# capture a single frame and check the frame shape so the correct array # capture a single frame and check the frame shape so the correct array
# size can be allocated in memory # size can be allocated in memory
@ -85,16 +86,6 @@ def main():
objects_parsed = mp.Condition() objects_parsed = mp.Condition()
# Queue for detected objects # Queue for detected objects
object_queue = mp.Queue() object_queue = mp.Queue()
# array for prepped frame with shape (1, 300, 300, 3)
prepped_frame_array = mp.Array(ctypes.c_uint8, 300*300*3)
# shared value for storing the prepped_frame_time
prepped_frame_time = mp.Value('d', 0.0)
# Condition for notifying that a new prepped frame is ready
prepped_frame_ready = mp.Condition()
# Lock to control access to the prepped frame
prepped_frame_lock = mp.Lock()
# array for prepped frame box [x1, y1, x2, y2]
prepped_frame_box = mp.Array(ctypes.c_uint16, 4)
# shape current frame so it can be treated as an image # shape current frame so it can be treated as an image
frame_arr = tonumpyarray(shared_arr).reshape(frame_shape) frame_arr = tonumpyarray(shared_arr).reshape(frame_shape)
@ -116,8 +107,8 @@ def main():
region['motion_detected'], region['motion_detected'],
frame_shape, frame_shape,
region['size'], region['x_offset'], region['y_offset'], region['size'], region['x_offset'], region['y_offset'],
prepped_frame_array, prepped_frame_time, prepped_frame_ready, region['prepped_frame_array'], region['prepped_frame_time'],
prepped_frame_lock, prepped_frame_box)) region['prepped_frame_lock']))
detection_prep_process.daemon = True detection_prep_process.daemon = True
detection_prep_processes.append(detection_prep_process) detection_prep_processes.append(detection_prep_process)
@ -135,9 +126,12 @@ def main():
# create a process for object detection # create a process for object detection
detection_process = mp.Process(target=detect_objects, args=( detection_process = mp.Process(target=detect_objects, args=(
prepped_frame_array, prepped_frame_time, [region['prepped_frame_array'] for region in regions],
prepped_frame_lock, prepped_frame_ready, [region['prepped_frame_time'] for region in regions],
prepped_frame_box, object_queue, DEBUG [region['prepped_frame_lock'] for region in regions],
[[region['size'], region['x_offset'], region['y_offset']] for region in regions],
motion_changed, [region['motion_detected'] for region in regions],
object_queue, DEBUG
)) ))
detection_process.daemon = True detection_process.daemon = True

View File

@ -19,48 +19,64 @@ def ReadLabelFile(file_path):
ret[int(pair[0])] = pair[1].strip() ret[int(pair[0])] = pair[1].strip()
return ret return ret
def detect_objects(prepped_frame_array, prepped_frame_time, prepped_frame_lock, def detect_objects(prepped_frame_arrays, prepped_frame_times, prepped_frame_locks,
prepped_frame_ready, prepped_frame_box, object_queue, debug): prepped_frame_boxes, motion_changed, motion_regions, object_queue, debug):
prepped_frame_np = tonumpyarray(prepped_frame_array) prepped_frame_nps = [tonumpyarray(prepped_frame_array) for prepped_frame_array in prepped_frame_arrays]
# Load the edgetpu engine and labels # Load the edgetpu engine and labels
engine = DetectionEngine(PATH_TO_CKPT) engine = DetectionEngine(PATH_TO_CKPT)
labels = ReadLabelFile(PATH_TO_LABELS) labels = ReadLabelFile(PATH_TO_LABELS)
frame_time = 0.0 frame_time = 0.0
region_box = [0,0,0,0] region_box = [0,0,0]
while True: while True:
with prepped_frame_ready: # while there is motion
prepped_frame_ready.wait() while len([r for r in motion_regions if r.is_set()]) > 0:
# loop over all the motion regions and look for objects
for i, motion_region in enumerate(motion_regions):
# skip the region if no motion
if not motion_region.is_set():
continue
# make a copy of the cropped frame # make a copy of the cropped frame
with prepped_frame_lock: with prepped_frame_locks[i]:
prepped_frame_copy = prepped_frame_np.copy() prepped_frame_copy = prepped_frame_nps[i].copy()
frame_time = prepped_frame_time.value frame_time = prepped_frame_times[i].value
region_box[:] = prepped_frame_box region_box[:] = prepped_frame_boxes[i]
# Actual detection. # Actual detection.
objects = engine.DetectWithInputTensor(prepped_frame_copy, threshold=0.5, top_k=3) objects = engine.DetectWithInputTensor(prepped_frame_copy, threshold=0.5, top_k=3)
# print(engine.get_inference_time()) # print(engine.get_inference_time())
# put detected objects in the queue # put detected objects in the queue
if objects: if objects:
# assumes square
region_size = region_box[2]-region_box[0]
for obj in objects: for obj in objects:
box = obj.bounding_box.flatten().tolist() box = obj.bounding_box.flatten().tolist()
object_queue.put({ object_queue.put({
'frame_time': frame_time, 'frame_time': frame_time,
'name': str(labels[obj.label_id]), 'name': str(labels[obj.label_id]),
'score': float(obj.score), 'score': float(obj.score),
'xmin': int((box[0] * region_size) + region_box[0]), 'xmin': int((box[0] * region_box[0]) + region_box[1]),
'ymin': int((box[1] * region_size) + region_box[1]), 'ymin': int((box[1] * region_box[0]) + region_box[2]),
'xmax': int((box[2] * region_size) + region_box[0]), 'xmax': int((box[2] * region_box[0]) + region_box[1]),
'ymax': int((box[3] * region_size) + region_box[1]) 'ymax': int((box[3] * region_box[0]) + region_box[2])
}) })
else:
object_queue.put({
'frame_time': frame_time,
'name': 'dummy',
'score': 0.99,
'xmin': int(0 + region_box[1]),
'ymin': int(0 + region_box[2]),
'xmax': int(10 + region_box[1]),
'ymax': int(10 + region_box[2])
})
# wait for the global motion flag to change
with motion_changed:
motion_changed.wait()
def prep_for_detection(shared_whole_frame_array, shared_frame_time, frame_lock, frame_ready, def prep_for_detection(shared_whole_frame_array, shared_frame_time, frame_lock, frame_ready,
motion_detected, frame_shape, region_size, region_x_offset, region_y_offset, motion_detected, frame_shape, region_size, region_x_offset, region_y_offset,
prepped_frame_array, prepped_frame_time, prepped_frame_ready, prepped_frame_lock, prepped_frame_array, prepped_frame_time, prepped_frame_lock):
prepped_frame_box):
# shape shared input array into frame for processing # shape shared input array into frame for processing
shared_whole_frame = tonumpyarray(shared_whole_frame_array).reshape(frame_shape) shared_whole_frame = tonumpyarray(shared_whole_frame_array).reshape(frame_shape)
@ -94,9 +110,4 @@ def prep_for_detection(shared_whole_frame_array, shared_frame_time, frame_lock,
# copy the prepped frame to the shared output array # copy the prepped frame to the shared output array
with prepped_frame_lock: with prepped_frame_lock:
shared_prepped_frame[:] = frame_expanded shared_prepped_frame[:] = frame_expanded
prepped_frame_time = frame_time prepped_frame_time.value = frame_time
prepped_frame_box[:] = [region_x_offset, region_y_offset, region_x_offset+region_size, region_y_offset+region_size]
# signal that a prepped frame is ready
with prepped_frame_ready:
prepped_frame_ready.notify_all()