Playout video at constant frames per sec
Change-Id: Ia81412abe2b2ffb17151d490691625aa784ef4c0
diff --git a/person_detection/app.py b/person_detection/app.py
index 1386d63..83a8d3d 100644
--- a/person_detection/app.py
+++ b/person_detection/app.py
@@ -8,6 +8,9 @@
from flask import Flask, render_template, Response
from argparse import ArgumentParser, SUPPRESS
+import fpstimer
+
+
# import camera driver
if os.environ.get('CAMERA'):
Camera = import_module('camera_' + os.environ['CAMERA']).Camera
@@ -29,10 +32,12 @@
def gen(camera):
"""Video streaming generator function."""
+ timer = fpstimer.FPSTimer(fps)
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
+ timer.sleep()
@app.route('/video_feed/<device>')
def video_feed(device):
@@ -56,11 +61,13 @@
default = "gstreamer", type=str)
args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering",
default=0.0, type=float)
- args.add_argument("--idle", action='store_true', help="Idle if no clients connected")
+ args.add_argument("-fps", "--frames_per_sec", help="Frames per second",
+ default=15, type=float)
return parser
if __name__ == '__main__':
args = build_argparser().parse_args()
+ fps = args.frames_per_sec
app.run(host='0.0.0.0', debug=True)
diff --git a/person_detection/base_camera.py b/person_detection/base_camera.py
index 28ee57d..fd4d653 100644
--- a/person_detection/base_camera.py
+++ b/person_detection/base_camera.py
@@ -4,28 +4,38 @@
class BaseCamera(object):
process = {} # background process that reads frames from camera
frame = {} # frame queue
+ last_frame = {}
- def __init__(self, device=None, idle=False):
+ def __init__(self, device):
"""Start the background camera process if it isn't running yet."""
self.device = device
+ BaseCamera.last_frame[self.device] = None
+
if self.device not in BaseCamera.process:
BaseCamera.process[self.device] = None
+
if BaseCamera.process[self.device] is None:
- self.frame[device] = Queue(100)
+ BaseCamera.frame[device] = Queue(100)
# start background frame process
BaseCamera.process[self.device] = Process(target=self._process, args=(self.device))
BaseCamera.process[self.device].start()
# wait until frames are available
- _ = self.get_frame()
+ BaseCamera.last_frame[self.device] = self.get_frame()
def get_frame(self):
"""Return the current camera frame."""
- # blocks
- return BaseCamera.frame[self.device].get(block=True)
+ if BaseCamera.last_frame[self.device] is None:
+ BaseCamera.last_frame[self.device] = BaseCamera.frame[self.device].get(block=True)
+ return BaseCamera.last_frame[self.device]
+ elif not BaseCamera.frame[self.device].empty():
+ BaseCamera.last_frame[self.device] = BaseCamera.frame[self.device].get()
+ return BaseCamera.last_frame[self.device]
+ else:
+ return BaseCamera.last_frame[self.device]
def frames(self):
""""Generator that returns frames from the camera."""
diff --git a/person_detection/person_detection.py b/person_detection/person_detection.py
index 8d8597f..73895a7 100644
--- a/person_detection/person_detection.py
+++ b/person_detection/person_detection.py
@@ -36,7 +36,7 @@
self.device = device
- super(Camera, self).__init__(device, args.idle)
+ super(Camera, self).__init__(device)
def __del__(self):
# stream.release()
diff --git a/requirements.txt b/requirements.txt
index c91b2ad..702752e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,7 @@
click==8.0.1
dataclasses==0.8
Flask==2.0.1
+fpstimer==0.0.1
importlib-metadata==4.8.1
imutils==0.5.4
itsdangerous==2.0.1