forked from platypush/platypush
Pi Camera backend version 0.something
This commit is contained in:
parent
85512424ae
commit
793b4df12d
1 changed files with 27 additions and 29 deletions
|
@ -6,7 +6,6 @@ from threading import Event
|
||||||
|
|
||||||
from platypush.backend import Backend
|
from platypush.backend import Backend
|
||||||
|
|
||||||
|
|
||||||
class CameraPiBackend(Backend):
|
class CameraPiBackend(Backend):
|
||||||
def __init__(self, listen_port, x_resolution=640, y_resolution=480,
|
def __init__(self, listen_port, x_resolution=640, y_resolution=480,
|
||||||
framerate=24, hflip=False, vflip=False,
|
framerate=24, hflip=False, vflip=False,
|
||||||
|
@ -14,7 +13,6 @@ class CameraPiBackend(Backend):
|
||||||
video_stabilization=False, ISO=0, exposure_compensation=0,
|
video_stabilization=False, ISO=0, exposure_compensation=0,
|
||||||
exposure_mode='auto', meter_mode='average', awb_mode='auto',
|
exposure_mode='auto', meter_mode='average', awb_mode='auto',
|
||||||
image_effect='none', color_effects=None, rotation=0,
|
image_effect='none', color_effects=None, rotation=0,
|
||||||
start_recording_on_startup=True,
|
|
||||||
crop=(0.0, 0.0, 1.0, 1.0), **kwargs):
|
crop=(0.0, 0.0, 1.0, 1.0), **kwargs):
|
||||||
""" See https://www.raspberrypi.org/documentation/usage/camera/python/README.md
|
""" See https://www.raspberrypi.org/documentation/usage/camera/python/README.md
|
||||||
for a detailed reference about the Pi camera options """
|
for a detailed reference about the Pi camera options """
|
||||||
|
@ -22,7 +20,6 @@ class CameraPiBackend(Backend):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self.listen_port = listen_port
|
self.listen_port = listen_port
|
||||||
self.start_recording_on_startup = start_recording_on_startup
|
|
||||||
self.server_socket = socket.socket()
|
self.server_socket = socket.socket()
|
||||||
self.server_socket.bind(('0.0.0.0', self.listen_port))
|
self.server_socket.bind(('0.0.0.0', self.listen_port))
|
||||||
self.server_socket.listen(0)
|
self.server_socket.listen(0)
|
||||||
|
@ -45,11 +42,6 @@ class CameraPiBackend(Backend):
|
||||||
self.camera.color_effects = color_effects
|
self.camera.color_effects = color_effects
|
||||||
self.camera.rotation = rotation
|
self.camera.rotation = rotation
|
||||||
self.camera.crop = crop
|
self.camera.crop = crop
|
||||||
self.start_recording_event = Event()
|
|
||||||
self.stop_recording_event = Event()
|
|
||||||
|
|
||||||
if self.start_recording_on_startup:
|
|
||||||
self.start_recording_event.set()
|
|
||||||
|
|
||||||
self.logger.info('Initialized Pi camera backend')
|
self.logger.info('Initialized Pi camera backend')
|
||||||
|
|
||||||
|
@ -58,37 +50,43 @@ class CameraPiBackend(Backend):
|
||||||
self.camera.capture(image_file)
|
self.camera.capture(image_file)
|
||||||
self.logger.info('Captured camera snapshot to {}'.format(image_file))
|
self.logger.info('Captured camera snapshot to {}'.format(image_file))
|
||||||
|
|
||||||
def start_recording(self):
|
def start_recording(self, video_file=None, format='h264'):
|
||||||
if not self.start_recording_event.is_set():
|
self.logger.info('Starting camera recording')
|
||||||
self.start_recording_event.set()
|
|
||||||
self.stop_recording_event.clear()
|
if video_file:
|
||||||
|
self.camera.start_recording(videofile, format=format)
|
||||||
|
else:
|
||||||
|
connection = self.server_socket.accept()[0].makefile('wb')
|
||||||
|
self.logger.info('Accepted client connection on port {}'.
|
||||||
|
format(self.listen_port))
|
||||||
|
|
||||||
|
self.camera.start_recording(connection, format=format)
|
||||||
|
|
||||||
def stop_recording(self):
|
def stop_recording(self):
|
||||||
if not self.stop_recording_event.is_set():
|
self.logger.info('Stopping camera recording')
|
||||||
self.stop_recording_event.set()
|
|
||||||
self.start_recording_event.clear()
|
try:
|
||||||
|
self.camera.stop_recording()
|
||||||
|
except:
|
||||||
|
self.logger.info('No recording currently in progress')
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
super().run()
|
super().run()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if not self.start_recording_event.is_set():
|
restart = True
|
||||||
self.start_recording_event.wait()
|
|
||||||
self.logger.info('Starting camera recording')
|
|
||||||
|
|
||||||
connection = self.server_socket.accept()[0].makefile('wb')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.camera.start_recording(connection, format='h264')
|
self.start_recording()
|
||||||
self.stop_recording_event.wait()
|
while True:
|
||||||
self.logger.info('Stopping camera recording')
|
self.camera.wait_recording(60)
|
||||||
except ConnectionError as e:
|
except ConnectionError:
|
||||||
self.logger.info('Client {} closed connection'.format(connection))
|
self.logger.info('Client closed connection')
|
||||||
finally:
|
finally:
|
||||||
try: connection.close()
|
try:
|
||||||
except: pass
|
self.stop_recording()
|
||||||
|
except Exception as e:
|
||||||
self.camera.stop_recording()
|
self.logger.exception(e)
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
Loading…
Reference in a new issue