use a lock and condition to signal to motion detection processes

This commit is contained in:
blakeblackshear 2019-02-17 11:52:56 -06:00
parent 284a96d5c7
commit b67f323215

View File

@ -200,11 +200,15 @@ def main():
shared_arr = mp.Array(ctypes.c_uint16, flat_array_length) shared_arr = mp.Array(ctypes.c_uint16, flat_array_length)
# create shared value for storing the frame_time # create shared value for storing the frame_time
shared_frame_time = mp.Value('d', 0.0) shared_frame_time = mp.Value('d', 0.0)
# Lock to control access to the frame while writing
frame_lock = mp.Lock()
# Condition for notifying that a new frame is ready
frame_ready = mp.Condition()
# 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)
capture_process = mp.Process(target=fetch_frames, args=(shared_arr, capture_process = mp.Process(target=fetch_frames, args=(shared_arr,
shared_frame_time, [region['ready_for_frame'] for region in regions], frame_shape)) shared_frame_time, frame_lock, frame_ready, frame_shape))
capture_process.daemon = True capture_process.daemon = True
detection_processes = [] detection_processes = []
@ -222,7 +226,7 @@ def main():
for index, region in enumerate(regions): for index, region in enumerate(regions):
motion_process = mp.Process(target=detect_motion, args=(shared_arr, motion_process = mp.Process(target=detect_motion, args=(shared_arr,
shared_frame_time, shared_frame_time,
region['ready_for_frame'], frame_lock, frame_ready,
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'],
@ -311,7 +315,7 @@ def tonumpyarray(mp_arr):
# fetch the frames as fast a possible, only decoding the frames when the # fetch the frames as fast a possible, only decoding the frames when the
# detection_process has consumed the current frame # detection_process has consumed the current frame
def fetch_frames(shared_arr, shared_frame_time, ready_for_frame_flags, frame_shape): def fetch_frames(shared_arr, shared_frame_time, frame_lock, frame_ready, frame_shape):
# convert shared memory array into numpy and shape into image array # convert shared memory array into numpy and shape into image array
arr = tonumpyarray(shared_arr).reshape(frame_shape) arr = tonumpyarray(shared_arr).reshape(frame_shape)
@ -326,20 +330,17 @@ def fetch_frames(shared_arr, shared_frame_time, ready_for_frame_flags, frame_sha
# snapshot the time the frame was grabbed # snapshot the time the frame was grabbed
frame_time = datetime.datetime.now() frame_time = datetime.datetime.now()
if ret: if ret:
# if the anyone is ready for the next frame decode it # go ahead and decode the current frame
# otherwise skip this frame and move onto the next one ret, frame = video.retrieve()
if any(flag.value == 1 for flag in ready_for_frame_flags): if ret:
# go ahead and decode the current frame # Lock access and update frame
ret, frame = video.retrieve() frame_lock.acquire()
if ret: arr[:] = frame
arr[:] = frame shared_frame_time.value = frame_time.timestamp()
shared_frame_time.value = frame_time.timestamp() frame_lock.release()
# signal to the detection_processes by setting the shared_frame_time # Notify with the condition that a new frame is ready
for flag in ready_for_frame_flags: with frame_ready:
flag.value = 0 frame_ready.notify_all()
else:
# sleep a little to reduce CPU usage
time.sleep(0.1)
video.release() video.release()
@ -404,7 +405,7 @@ def process_frames(shared_arr, shared_output_arr, shared_frame_time, shared_moti
shared_output_arr[:] = objects + [0.0] * (60-len(objects)) shared_output_arr[:] = objects + [0.0] * (60-len(objects))
# do the actual motion detection # do the actual motion detection
def detect_motion(shared_arr, shared_frame_time, ready_for_frame, shared_motion, frame_shape, region_size, region_x_offset, region_y_offset, min_motion_area, debug): def detect_motion(shared_arr, shared_frame_time, frame_lock, frame_ready, shared_motion, frame_shape, region_size, region_x_offset, region_y_offset, min_motion_area, debug):
# shape shared input array into frame for processing # shape shared input array into frame for processing
arr = tonumpyarray(shared_arr).reshape(frame_shape) arr = tonumpyarray(shared_arr).reshape(frame_shape)
@ -419,39 +420,17 @@ def detect_motion(shared_arr, shared_frame_time, ready_for_frame, shared_motion,
if last_motion > 0 and (now - last_motion) > 2: if last_motion > 0 and (now - last_motion) > 2:
last_motion = -1 last_motion = -1
shared_motion.value = 0 shared_motion.value = 0
# if there isnt a frame ready for processing
if shared_frame_time.value == frame_time:
# save the first time there were no frames available
if no_frames_available == -1:
no_frames_available = now
# if there havent been any frames available in 30 seconds,
# sleep to avoid using so much cpu if the camera feed is down
if no_frames_available > 0 and (now - no_frames_available) > 30:
time.sleep(1)
print("sleeping because no frames have been available in a while")
else:
# rest a little bit to avoid maxing out the CPU
time.sleep(0.1)
if ready_for_frame.value == 0:
ready_for_frame.value = 1
continue
# we got a valid frame, so reset the timer with frame_ready:
no_frames_available = -1 # if there isnt a frame ready for processing or it is old, wait for a signal
if shared_frame_time.value == frame_time or (now - shared_frame_time.value) > 0.5:
# if the frame is more than 0.5 second old, discard it frame_ready.wait()
if (now - shared_frame_time.value) > 0.5:
# signal that we need a new frame
ready_for_frame.value = 1
# rest a little bit to avoid maxing out the CPU
time.sleep(0.1)
continue
# make a copy of the cropped frame # lock and make a copy of the cropped frame
frame_lock.acquire()
cropped_frame = arr[region_y_offset:region_y_offset+region_size, region_x_offset:region_x_offset+region_size].copy().astype('uint8') cropped_frame = arr[region_y_offset:region_y_offset+region_size, region_x_offset:region_x_offset+region_size].copy().astype('uint8')
frame_time = shared_frame_time.value frame_time = shared_frame_time.value
# signal that the frame has been used so a new one will be ready frame_lock.release()
ready_for_frame.value = 1
# convert to grayscale # convert to grayscale
gray = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2GRAY)