From a0230e344d345ed53ef913247428e32b7e64c0c4 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 6 Aug 2020 21:43:02 +0200 Subject: [PATCH] re-introduce video-feed --- raspberry/roberto/__init__.py | 15 ++++++++-- raspberry/roberto/camera/base_camera.py | 2 +- raspberry/roberto/views/frontend/routes.py | 34 +++++++++++----------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/raspberry/roberto/__init__.py b/raspberry/roberto/__init__.py index e8186c9..7fa0228 100644 --- a/raspberry/roberto/__init__.py +++ b/raspberry/roberto/__init__.py @@ -7,8 +7,14 @@ from flask_socketio import SocketIO ####################### #### Configuration #### #https://www.patricksoftwareblog.com/structuring-a-flask-project/ - ####################### + +platform = None +try: + import RPi.GPIO as gpio + platform = 'raspberry' +except (ImportError, RuntimeError): + platform = 'generic' # Create the instances of the Flask extensions (flask-sqlalchemy, flask-login, etc.) in # the global scope, but without any arguments passed in. These instances are not attached @@ -16,8 +22,11 @@ from flask_socketio import SocketIO login = LoginManager() login.login_view = "users.login" socketio = SocketIO() -#from roberto.camera.camera_opencv import Camera -#camera = Camera() +if platform == 'raspberry': + from roberto.camera.camera_pi import Camera +else: + from roberto.camera.camera_opencv import Camera +camera = Camera() from roberto.camera.camera_gstreamer_webrtc import WebRTCCamera webrtccamera = WebRTCCamera() from roberto.Serial import Serial diff --git a/raspberry/roberto/camera/base_camera.py b/raspberry/roberto/camera/base_camera.py index 38c33de..c340508 100644 --- a/raspberry/roberto/camera/base_camera.py +++ b/raspberry/roberto/camera/base_camera.py @@ -97,7 +97,7 @@ class BaseCamera(object): # if there hasn't been any clients asking for frames in # the last 10 seconds then stop the thread - if time.time() - BaseCamera.last_access > 10: + if time.time() - BaseCamera.last_access > 3: frames_iterator.close() print('Stopping camera thread due to inactivity.') break diff --git a/raspberry/roberto/views/frontend/routes.py b/raspberry/roberto/views/frontend/routes.py index 08938b7..908efe1 100644 --- a/raspberry/roberto/views/frontend/routes.py +++ b/raspberry/roberto/views/frontend/routes.py @@ -11,23 +11,23 @@ from flask import Response def index(): return render_template('index.html') -#def gen(camera): -# """Video streaming generator function.""" -# while True: -# frame = camera.get_frame() -# yield (b'--frame\r\n' -# b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') -# -# -#@frontend_blueprint.route('/video_feed') -#def video_feed(): -# """Video streaming route. Put this in the src attribute of an img tag.""" -# from roberto import camera -# camera.__init__() -# response = Response(gen(camera), -# mimetype='multipart/x-mixed-replace; boundary=frame') -# response.headers.add('X-Accel-Buffering', 'no') -# return response +def gen(camera): + """Video streaming generator function.""" + while True: + frame = camera.get_frame() + yield (b'--frame\r\n' + b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') + + +@frontend_blueprint.route('/video_feed') +def video_feed(): + """Video streaming route. Put this in the src attribute of an img tag.""" + from roberto import camera + camera.__init__() + response = Response(gen(camera), + mimetype='multipart/x-mixed-replace; boundary=frame') + response.headers.add('X-Accel-Buffering', 'no') + return response @frontend_blueprint.route('/gamepad.js')