Browse Source

speed up camera_pi mjpeg stream

main
Hendrik Langer 4 years ago
parent
commit
d222e93159
  1. 2
      raspberry/roberto/__init__.py
  2. 46
      raspberry/roberto/camera/camera_pi_new.py

2
raspberry/roberto/__init__.py

@ -23,7 +23,7 @@ login = LoginManager()
login.login_view = "users.login" login.login_view = "users.login"
socketio = SocketIO() socketio = SocketIO()
if platform == 'raspberry': if platform == 'raspberry':
from roberto.camera.camera_pi import Camera from roberto.camera.camera_pi_new import Camera
else: else:
from roberto.camera.camera_opencv import Camera from roberto.camera.camera_opencv import Camera
camera = Camera() camera = Camera()

46
raspberry/roberto/camera/camera_pi_new.py

@ -0,0 +1,46 @@
import io
import time
import picamera
from .base_camera import BaseCamera
from threading import Condition
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# New frame, copy the existing buffer's content and notify all
# clients it's available
self.buffer.truncate()
with self.condition:
self.frame = self.buffer.getvalue()
self.condition.notify_all()
self.buffer.seek(0)
return self.buffer.write(buf)
class Camera(BaseCamera):
@staticmethod
def frames():
with picamera.PiCamera() as camera:
camera.resolution = (640, 480)
camera.exposure_mode = 'sports'
camera.framerate = 15
camera.rotation = 180
# let camera warm up
time.sleep(2)
output = StreamingOutput()
camera.start_recording(output, format='mjpeg')
while True:
with output.condition:
output.condition.wait()
frame = output.frame
#yield b'--FRAME\r\n'
yield frame
#yield b'\r\n'
Loading…
Cancel
Save