2019-02-26 03:27:02 +01:00
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import cv2
|
2019-02-27 03:29:52 +01:00
|
|
|
import threading
|
2019-02-26 03:27:02 +01:00
|
|
|
from . util import tonumpyarray
|
|
|
|
|
|
|
|
# fetch the frames as fast a possible, only decoding the frames when the
|
|
|
|
# detection_process has consumed the current frame
|
|
|
|
def fetch_frames(shared_arr, shared_frame_time, frame_lock, frame_ready, frame_shape, rtsp_url):
|
|
|
|
# convert shared memory array into numpy and shape into image array
|
|
|
|
arr = tonumpyarray(shared_arr).reshape(frame_shape)
|
|
|
|
|
|
|
|
# start the video capture
|
|
|
|
video = cv2.VideoCapture()
|
|
|
|
video.open(rtsp_url)
|
|
|
|
# keep the buffer small so we minimize old data
|
|
|
|
video.set(cv2.CAP_PROP_BUFFERSIZE,1)
|
|
|
|
|
2019-02-28 03:55:07 +01:00
|
|
|
bad_frame_counter = 0
|
2019-02-26 03:27:02 +01:00
|
|
|
while True:
|
|
|
|
# check if the video stream is still open, and reopen if needed
|
|
|
|
if not video.isOpened():
|
|
|
|
success = video.open(rtsp_url)
|
|
|
|
if not success:
|
|
|
|
time.sleep(1)
|
|
|
|
continue
|
|
|
|
# grab the frame, but dont decode it yet
|
|
|
|
ret = video.grab()
|
|
|
|
# snapshot the time the frame was grabbed
|
|
|
|
frame_time = datetime.datetime.now()
|
|
|
|
if ret:
|
|
|
|
# go ahead and decode the current frame
|
|
|
|
ret, frame = video.retrieve()
|
|
|
|
if ret:
|
|
|
|
# Lock access and update frame
|
|
|
|
with frame_lock:
|
|
|
|
arr[:] = frame
|
|
|
|
shared_frame_time.value = frame_time.timestamp()
|
|
|
|
# Notify with the condition that a new frame is ready
|
|
|
|
with frame_ready:
|
|
|
|
frame_ready.notify_all()
|
2019-02-28 03:55:07 +01:00
|
|
|
bad_frame_counter = 0
|
|
|
|
else:
|
|
|
|
print("Unable to decode frame")
|
|
|
|
bad_frame_counter += 1
|
|
|
|
else:
|
|
|
|
print("Unable to grab a frame")
|
|
|
|
bad_frame_counter += 1
|
|
|
|
|
|
|
|
if bad_frame_counter > 100:
|
|
|
|
video.release()
|
2019-02-26 03:27:02 +01:00
|
|
|
|
2019-02-27 03:29:52 +01:00
|
|
|
video.release()
|
|
|
|
|
2019-02-28 03:55:07 +01:00
|
|
|
# Stores 2 seconds worth of frames when motion is detected so they can be used for other threads
|
2019-02-27 03:29:52 +01:00
|
|
|
class FrameTracker(threading.Thread):
|
2019-03-27 12:17:00 +01:00
|
|
|
def __init__(self, shared_frame, frame_time, frame_ready, frame_lock, recent_frames):
|
2019-02-27 03:29:52 +01:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.shared_frame = shared_frame
|
|
|
|
self.frame_time = frame_time
|
|
|
|
self.frame_ready = frame_ready
|
|
|
|
self.frame_lock = frame_lock
|
|
|
|
self.recent_frames = recent_frames
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
frame_time = 0.0
|
|
|
|
while True:
|
2019-03-27 12:17:00 +01:00
|
|
|
now = datetime.datetime.now().timestamp()
|
|
|
|
# wait for a frame
|
|
|
|
with self.frame_ready:
|
|
|
|
# if there isnt a frame ready for processing or it is old, wait for a signal
|
|
|
|
if self.frame_time.value == frame_time or (now - self.frame_time.value) > 0.5:
|
|
|
|
self.frame_ready.wait()
|
|
|
|
|
|
|
|
# lock and make a copy of the frame
|
|
|
|
with self.frame_lock:
|
|
|
|
frame = self.shared_frame.copy()
|
|
|
|
frame_time = self.frame_time.value
|
|
|
|
|
|
|
|
# add the frame to recent frames
|
|
|
|
self.recent_frames[frame_time] = frame
|
2019-02-27 03:29:52 +01:00
|
|
|
|
2019-03-27 12:17:00 +01:00
|
|
|
# delete any old frames
|
|
|
|
stored_frame_times = list(self.recent_frames.keys())
|
|
|
|
for k in stored_frame_times:
|
|
|
|
if (now - k) > 2:
|
|
|
|
del self.recent_frames[k]
|