diff --git a/raspberry/roberto/__init__.py b/raspberry/roberto/__init__.py index 7fa0228..5c24005 100644 --- a/raspberry/roberto/__init__.py +++ b/raspberry/roberto/__init__.py @@ -23,7 +23,7 @@ login = LoginManager() login.login_view = "users.login" socketio = SocketIO() if platform == 'raspberry': - from roberto.camera.camera_pi import Camera + from roberto.camera.camera_pi_new import Camera else: from roberto.camera.camera_opencv import Camera camera = Camera() diff --git a/raspberry/roberto/camera/camera_pi_new.py b/raspberry/roberto/camera/camera_pi_new.py new file mode 100644 index 0000000..5e23964 --- /dev/null +++ b/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'