From f0122b159d84df94cf9a909a72b75f1faa7d43cd Mon Sep 17 00:00:00 2001 From: mryel00 Date: Sun, 29 Jan 2023 21:34:11 +0100 Subject: [PATCH 01/18] feat: Add basic USB-Camera support --- spyglass/camera.py | 6 ++- spyglass/cli.py | 50 ++++++++++++++------- spyglass/server.py | 102 +++++++++++++++++++++++------------------- spyglass/usbCamera.py | 28 ++++++++++++ spyglass/usbServer.py | 22 +++++++++ 5 files changed, 144 insertions(+), 64 deletions(-) create mode 100644 spyglass/usbCamera.py create mode 100644 spyglass/usbServer.py diff --git a/spyglass/camera.py b/spyglass/camera.py index 5b5733b..2040480 100644 --- a/spyglass/camera.py +++ b/spyglass/camera.py @@ -8,8 +8,10 @@ def init_camera( fps: int, autofocus: str, lens_position: float, - autofocus_speed: str): - picam2 = Picamera2() + autofocus_speed: str, + camera_num: int): + + picam2 = Picamera2(camera_num) controls = {'FrameRate': fps} diff --git a/spyglass/cli.py b/spyglass/cli.py index 5c9b9cf..3b7c49b 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -7,8 +7,10 @@ import libcamera import re from spyglass.camera import init_camera +from spyglass.usbCamera import init_usb_camera from spyglass.server import StreamingOutput from spyglass.server import run_server +from spyglass.usbServer import run_usb_server from picamera2.encoders import MJPEGEncoder from picamera2.outputs import FileOutput @@ -30,21 +32,37 @@ def main(args=None): width, height = split_resolution(parsed_args.resolution) stream_url = parsed_args.stream_url snapshot_url = parsed_args.snapshot_url - picam2 = init_camera( - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed)) - - output = StreamingOutput() - picam2.start_recording(MJPEGEncoder(), FileOutput(output)) - - try: - run_server(bind_address, port, output, stream_url, snapshot_url) - finally: - picam2.stop_recording() + is_usbcam = parsed_args.is_usbcam + + if is_usbcam: + picam2 = init_usb_camera( + width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.camera_num) + try: + picam2.start() + run_usb_server(bind_address, port, picam2, stream_url, snapshot_url) + finally: + picam2.stop() + else: + picam2 = init_camera( + width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.camera_num) + try: + output = StreamingOutput() + picam2.start_recording(MJPEGEncoder(), FileOutput(output)) + run_server(bind_address, port, output, stream_url, snapshot_url) + finally: + picam2.stop_recording() # region args parsers @@ -114,6 +132,8 @@ def get_parser(): 'Only used with Autofocus manual') parser.add_argument('-s', '--autofocusspeed', type=str, default='normal', choices=['normal', 'fast'], help='Autofocus speed. Only used with Autofocus continuous') + parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') + parser.add_argument('-u', '--is_usbcam', action='store_true', help='Make MJPEG USB-Cams usable. Only use if normal method doesn\'t work') return parser # endregion cli args diff --git a/spyglass/server.py b/spyglass/server.py index 0e37a04..0459026 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -23,63 +23,71 @@ class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True +class StreamingHandler(server.BaseHTTPRequestHandler): + output = None + stream_url = '' + snapshot_url = '' -def run_server(bind_address, port, output, stream_url='/stream', snapshot_url='/snapshot'): - class StreamingHandler(server.BaseHTTPRequestHandler): - def do_GET(self): - if self.path == stream_url: - self.start_streaming() - elif self.path == snapshot_url: - self.send_snapshot() - else: - self.send_error(404) - self.end_headers() - - def start_streaming(self): - try: - self.send_response(200) - self.send_default_headers() - self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') - self.end_headers() - while True: - with output.condition: - output.condition.wait() - frame = output.frame - self.wfile.write(b'--FRAME\r\n') - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) - self.wfile.write(b'\r\n') - except Exception as e: - logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) + def do_GET(self): + if self.path == self.stream_url: + self.start_streaming() + elif self.path == self.snapshot_url: + self.send_snapshot() + else: + self.send_error(404) + self.end_headers() - def send_snapshot(self): - try: - self.send_response(200) - self.send_default_headers() - with output.condition: - output.condition.wait() - frame = output.frame + def start_streaming(self): + try: + self.send_response(200) + self.send_default_headers() + self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') + self.end_headers() + while True: + frame = self.get_frame() + self.wfile.write(b'--FRAME\r\n') self.send_jpeg_content_headers(frame) self.end_headers() self.wfile.write(frame) - except Exception as e: - logging.warning( - 'Removed client %s: %s', - self.client_address, str(e)) + self.wfile.write(b'\r\n') + except Exception as e: + logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) - def send_default_headers(self): - self.send_header('Age', 0) - self.send_header('Cache-Control', 'no-cache, private') - self.send_header('Pragma', 'no-cache') + def send_snapshot(self): + try: + self.send_response(200) + self.send_default_headers() + frame = self.get_frame() + self.send_jpeg_content_headers(frame) + self.end_headers() + self.wfile.write(frame) + except Exception as e: + logging.warning( + 'Removed client %s: %s', + self.client_address, str(e)) - def send_jpeg_content_headers(self, frame): - self.send_header('Content-Type', 'image/jpeg') - self.send_header('Content-Length', len(frame)) + def send_default_headers(self): + self.send_header('Age', 0) + self.send_header('Cache-Control', 'no-cache, private') + self.send_header('Pragma', 'no-cache') + def send_jpeg_content_headers(self, frame): + self.send_header('Content-Type', 'image/jpeg') + self.send_header('Content-Length', len(frame)) + + def get_frame(self): + with self.output.condition: + self.output.condition.wait() + return self.output.frame + +def run_server(bind_address, port, output, stream_url='/stream', snapshot_url='/snapshot'): logger.info('Server listening on %s:%d', bind_address, port) logger.info('Streaming endpoint: %s', stream_url) logger.info('Snapshot endpoint: %s', snapshot_url) address = (bind_address, port) - current_server = StreamingServer(address, StreamingHandler) + streaming_handler = StreamingHandler + streaming_handler.output = output + streaming_handler.stream_url = stream_url + streaming_handler.snapshot_url = snapshot_url + current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() diff --git a/spyglass/usbCamera.py b/spyglass/usbCamera.py new file mode 100644 index 0000000..4ecad1c --- /dev/null +++ b/spyglass/usbCamera.py @@ -0,0 +1,28 @@ +import libcamera +from picamera2 import Picamera2 + + +def init_usb_camera( + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str, + camera_num: int): + + picam2 = Picamera2(camera_num) + + controls = {} + + if 'AfMode' in picam2.camera_controls: + controls['AfMode'] = autofocus + controls['AfSpeed'] = autofocus_speed + if autofocus == libcamera.controls.AfModeEnum.Manual: + controls['LensPosition'] = lens_position + else: + print('Attached camera does not support autofocus') + + picam2.configure(picam2.create_preview_configuration(main={'size': (width, height), 'format': 'MJPEG'}, controls=controls)) + + return picam2 diff --git a/spyglass/usbServer.py b/spyglass/usbServer.py new file mode 100644 index 0000000..05733af --- /dev/null +++ b/spyglass/usbServer.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +from spyglass.server import StreamingHandler, StreamingServer +from . import logger + +class StreamingHandlerUSB(StreamingHandler): + def get_frame(self): + #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer + return self.output.capture_buffer() + + +def run_usb_server(bind_address, port, cam, stream_url='/stream', snapshot_url='/snapshot'): + logger.info('Server listening on %s:%d', bind_address, port) + logger.info('Streaming endpoint: %s', stream_url) + logger.info('Snapshot endpoint: %s', snapshot_url) + address = (bind_address, port) + streaming_handler = StreamingHandlerUSB + streaming_handler.output = cam + streaming_handler.stream_url = stream_url + streaming_handler.snapshot_url = snapshot_url + current_server = StreamingServer(address, streaming_handler) + current_server.serve_forever() \ No newline at end of file From d7cedac5f800b93ec5de74a59bd25a99db37e513 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Wed, 3 Apr 2024 21:33:55 +0200 Subject: [PATCH 02/18] refactor: refactor camera code Signed-off-by: Patrick Gehrsitz --- spyglass/camera.py | 28 --------------- spyglass/camera/__init__.py | 22 ++++++++++++ spyglass/camera/camera.py | 68 +++++++++++++++++++++++++++++++++++++ spyglass/camera/csi.py | 48 ++++++++++++++++++++++++++ spyglass/camera/usb.py | 37 ++++++++++++++++++++ spyglass/cli.py | 52 +++++++--------------------- spyglass/server.py | 14 -------- spyglass/usbCamera.py | 28 --------------- spyglass/usbServer.py | 22 ------------ 9 files changed, 187 insertions(+), 132 deletions(-) delete mode 100644 spyglass/camera.py create mode 100644 spyglass/camera/__init__.py create mode 100644 spyglass/camera/camera.py create mode 100644 spyglass/camera/csi.py create mode 100644 spyglass/camera/usb.py delete mode 100644 spyglass/usbCamera.py delete mode 100644 spyglass/usbServer.py diff --git a/spyglass/camera.py b/spyglass/camera.py deleted file mode 100644 index 2040480..0000000 --- a/spyglass/camera.py +++ /dev/null @@ -1,28 +0,0 @@ -import libcamera -from picamera2 import Picamera2 - - -def init_camera( - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str, - camera_num: int): - - picam2 = Picamera2(camera_num) - - controls = {'FrameRate': fps} - - if 'AfMode' in picam2.camera_controls: - controls['AfMode'] = autofocus - controls['AfSpeed'] = autofocus_speed - if autofocus == libcamera.controls.AfModeEnum.Manual: - controls['LensPosition'] = lens_position - else: - print('Attached camera does not support autofocus') - - picam2.configure(picam2.create_video_configuration(main={'size': (width, height)}, controls=controls)) - - return picam2 diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py new file mode 100644 index 0000000..cf877d4 --- /dev/null +++ b/spyglass/camera/__init__.py @@ -0,0 +1,22 @@ +from .camera import Camera +from .csi import CSI +from .usb import USB + +from picamera2 import Picamera2 + +def init_camera( + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str, + camera_num: int) -> Camera: + + picam2 = Picamera2(camera_num) + if picam2._is_rpi_camera(): + cam = CSI(width, height, fps, autofocus, lens_position, autofocus_speed) + else: + cam = USB(width, height, fps, autofocus, lens_position, autofocus_speed) + cam.configure() + return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py new file mode 100644 index 0000000..e0cbad3 --- /dev/null +++ b/spyglass/camera/camera.py @@ -0,0 +1,68 @@ +from abc import ABC, abstractmethod +from picamera2 import Picamera2 +from .. import logger +from .. server import StreamingServer, StreamingHandler + +class Camera(ABC): + def __init__(self, + picam2: Picamera2, + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str): + + self.picam2 = picam2 + self.width = width + self.height = height + self.fps = fps + self.autofocus = autofocus + self.lens_position = lens_position + self.autofocus_speed = autofocus_speed + + def create_controls(self): + controls = {'FrameRate': self.fps} + + if 'AfMode' in self.picam2.camera_controls: + controls['AfMode'] = self.autofocus + controls['AfSpeed'] = self.autofocus_speed + if self.autofocus == self.libcamera.controls.AfModeEnum.Manual: + controls['LensPosition'] = self.lens_position + else: + print('Attached camera does not support autofocus') + + return controls + + def _run_server(self, + bind_address, + port, + output, + streaming_handler: StreamingHandler, + stream_url='/stream', + snapshot_url='/snapshot'): + logger.info('Server listening on %s:%d', bind_address, port) + logger.info('Streaming endpoint: %s', stream_url) + logger.info('Snapshot endpoint: %s', snapshot_url) + address = (bind_address, port) + streaming_handler.output = output + streaming_handler.stream_url = stream_url + streaming_handler.snapshot_url = snapshot_url + current_server = StreamingServer(address, streaming_handler) + current_server.serve_forever() + + @abstractmethod + def configure(self): + pass + + @abstractmethod + def stop(self): + pass + + @abstractmethod + def start_and_run_server(self, + bind_address, + port, + stream_url='/stream', + snapshot_url='/snapshot'): + pass diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py new file mode 100644 index 0000000..a584300 --- /dev/null +++ b/spyglass/camera/csi.py @@ -0,0 +1,48 @@ +import io + +from picamera2.encoders import MJPEGEncoder +from picamera2.outputs import FileOutput +from threading import Condition + +from . import camera +from .. server import StreamingHandler + +class CSI(camera.Camera): + def configure(self): + controls = self.create_controls() + + self.picam2.configure( + self.picam2.create_video_configuration( + main={'size': (self.width, self.height)}, + controls=controls + ) + ) + + def start_and_run_server(self, + bind_address, + port, + stream_url='/stream', + snapshot_url='/snapshot'): + + class StreamingOutput(io.BufferedIOBase): + def __init__(self): + self.frame = None + self.condition = Condition() + + def write(self, buf): + with self.condition: + self.frame = buf + self.condition.notify_all() + output = StreamingOutput() + self.picam2.start_recording(MJPEGEncoder(), FileOutput(output)) + self._run_server( + bind_address, + port, + output, + StreamingHandler(), + stream_url=stream_url, + snapshot_url=snapshot_url + ) + + def stop(self): + self.picam2.stop_recording() diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py new file mode 100644 index 0000000..b7a07d9 --- /dev/null +++ b/spyglass/camera/usb.py @@ -0,0 +1,37 @@ +from . import camera + +from spyglass.server import StreamingHandler, StreamingServer +from .. import logger + +class USB(camera.Camera): + def configure(self): + controls = self.create_controls() + + self.picam2.configure( + self.picam2.create_preview_configuration( + main={'size': (self.width, self.height), 'format': 'MJPEG'}, + controls=controls + ) + ) + + def start_and_run_server(self, + bind_address, + port, + stream_url='/stream', + snapshot_url='/snapshot'): + class StreamingHandlerUSB(StreamingHandler): + def get_frame(self): + #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer + return self.output.capture_buffer() + self.picam2.start() + self._run_server( + bind_address, + port, + self.picam2, + StreamingHandlerUSB(), + stream_url=stream_url, + snapshot_url=snapshot_url + ) + + def stop(self): + self.picam2.stop() diff --git a/spyglass/cli.py b/spyglass/cli.py index 3b7c49b..c8f7973 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -6,14 +6,7 @@ import sys import libcamera import re -from spyglass.camera import init_camera -from spyglass.usbCamera import init_usb_camera -from spyglass.server import StreamingOutput -from spyglass.server import run_server -from spyglass.usbServer import run_usb_server -from picamera2.encoders import MJPEGEncoder -from picamera2.outputs import FileOutput - +from .camera import init_camera def main(args=None): """Entry point for hello cli. @@ -32,38 +25,18 @@ def main(args=None): width, height = split_resolution(parsed_args.resolution) stream_url = parsed_args.stream_url snapshot_url = parsed_args.snapshot_url - is_usbcam = parsed_args.is_usbcam - - if is_usbcam: - picam2 = init_usb_camera( - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.camera_num) - try: - picam2.start() - run_usb_server(bind_address, port, picam2, stream_url, snapshot_url) - finally: - picam2.stop() - else: - picam2 = init_camera( - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.camera_num) - try: - output = StreamingOutput() - picam2.start_recording(MJPEGEncoder(), FileOutput(output)) - run_server(bind_address, port, output, stream_url, snapshot_url) - finally: - picam2.stop_recording() + cam = init_camera(width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.camera_num) + try: + cam.start_and_run_server(bind_address, port, stream_url, snapshot_url) + finally: + cam.stop() # region args parsers @@ -133,7 +106,6 @@ def get_parser(): parser.add_argument('-s', '--autofocusspeed', type=str, default='normal', choices=['normal', 'fast'], help='Autofocus speed. Only used with Autofocus continuous') parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') - parser.add_argument('-u', '--is_usbcam', action='store_true', help='Make MJPEG USB-Cams usable. Only use if normal method doesn\'t work') return parser # endregion cli args diff --git a/spyglass/server.py b/spyglass/server.py index 0459026..64619a1 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -1,24 +1,10 @@ #!/usr/bin/python3 -import io import logging import socketserver from http import server -from threading import Condition from . import logger - -class StreamingOutput(io.BufferedIOBase): - def __init__(self): - self.frame = None - self.condition = Condition() - - def write(self, buf): - with self.condition: - self.frame = buf - self.condition.notify_all() - - class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True diff --git a/spyglass/usbCamera.py b/spyglass/usbCamera.py deleted file mode 100644 index 4ecad1c..0000000 --- a/spyglass/usbCamera.py +++ /dev/null @@ -1,28 +0,0 @@ -import libcamera -from picamera2 import Picamera2 - - -def init_usb_camera( - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str, - camera_num: int): - - picam2 = Picamera2(camera_num) - - controls = {} - - if 'AfMode' in picam2.camera_controls: - controls['AfMode'] = autofocus - controls['AfSpeed'] = autofocus_speed - if autofocus == libcamera.controls.AfModeEnum.Manual: - controls['LensPosition'] = lens_position - else: - print('Attached camera does not support autofocus') - - picam2.configure(picam2.create_preview_configuration(main={'size': (width, height), 'format': 'MJPEG'}, controls=controls)) - - return picam2 diff --git a/spyglass/usbServer.py b/spyglass/usbServer.py deleted file mode 100644 index 05733af..0000000 --- a/spyglass/usbServer.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/python3 - -from spyglass.server import StreamingHandler, StreamingServer -from . import logger - -class StreamingHandlerUSB(StreamingHandler): - def get_frame(self): - #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer - return self.output.capture_buffer() - - -def run_usb_server(bind_address, port, cam, stream_url='/stream', snapshot_url='/snapshot'): - logger.info('Server listening on %s:%d', bind_address, port) - logger.info('Streaming endpoint: %s', stream_url) - logger.info('Snapshot endpoint: %s', snapshot_url) - address = (bind_address, port) - streaming_handler = StreamingHandlerUSB - streaming_handler.output = cam - streaming_handler.stream_url = stream_url - streaming_handler.snapshot_url = snapshot_url - current_server = StreamingServer(address, streaming_handler) - current_server.serve_forever() \ No newline at end of file From b136f9556b0c9169e8802864aa78cd4c16484e21 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 17:30:00 +0200 Subject: [PATCH 03/18] Merge branch 'expand_camera_control_ability' into add_usbcam_support Signed-off-by: Patrick Gehrsitz --- .editorconfig | 19 + .github/CODEOWNERS | 2 +- .../pull_request_template.md | 7 + .github/workflows/build-and-package.yml | 29 - .github/workflows/check-commit-message.yml | 32 + .github/workflows/pull-request.yml | 49 +- .github/workflows/release.yml | 53 ++ .gitignore | 2 + .pre-commit-config.yaml | 8 + CHANGELOG.md | 71 ++ CONTRIBUTING.md | 20 - Contributors.md | 3 - LICENSE | 875 ++++++++++++++---- Makefile | 63 ++ README.md | 204 +++- docs/CONTRIBUTING.md | 107 +++ docs/contributors.md | 9 + docs/developer-certificate-of-origin.md | 34 + pyproject.toml | 20 + requirements-dev.txt | 2 + requirements-test.txt | 4 + requirements.txt | 2 +- resources/controls_style.css | 69 ++ resources/spyglass.conf | 73 ++ resources/spyglass.service | 26 + run.py | 2 +- scripts/spyglass | 145 +++ setup.cfg | 4 +- spyglass/__version__.py | 1 + spyglass/camera.py | 0 spyglass/camera/__init__.py | 20 +- spyglass/camera/camera.py | 17 +- spyglass/camera/csi.py | 26 +- spyglass/camera/usb.py | 28 +- spyglass/camera_options.py | 105 +++ spyglass/cli.py | 105 ++- spyglass/exif.py | 47 + spyglass/server.py | 121 ++- spyglass/url_parsing.py | 49 + tests/test_cli.py | 474 ++++++++++ tests/test_exif.py | 17 + tests/test_url_parsing.py | 137 +++ 42 files changed, 2709 insertions(+), 372 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/PULL_REQUEST_TEMPLATE/pull_request_template.md delete mode 100644 .github/workflows/build-and-package.yml create mode 100644 .github/workflows/check-commit-message.yml create mode 100644 .github/workflows/release.yml create mode 100644 .pre-commit-config.yaml create mode 100644 CHANGELOG.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Contributors.md create mode 100644 Makefile create mode 100644 docs/CONTRIBUTING.md create mode 100644 docs/contributors.md create mode 100644 docs/developer-certificate-of-origin.md create mode 100644 requirements-dev.txt create mode 100644 requirements-test.txt create mode 100644 resources/controls_style.css create mode 100644 resources/spyglass.conf create mode 100644 resources/spyglass.service create mode 100755 scripts/spyglass create mode 100644 spyglass/__version__.py create mode 100644 spyglass/camera.py create mode 100644 spyglass/camera_options.py create mode 100644 spyglass/exif.py create mode 100644 spyglass/url_parsing.py create mode 100644 tests/test_cli.py create mode 100644 tests/test_exif.py create mode 100644 tests/test_url_parsing.py diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..cfc425a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,19 @@ +# EditorConfig is awesome: https://EditorConfig.org +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[{Makefile,**.mk}] +indent_style = tab + +[*.py] +indent_size = 4 +indent_style = space + +[{**.yaml,**.yml}] +indent_style = space +indent_size = 2 diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 305d07f..e6ded5c 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @roamingthings @mryel00 +* @roamingthings @mryel00 @KwadFan diff --git a/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md new file mode 100644 index 0000000..5786bda --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md @@ -0,0 +1,7 @@ +Describe what the submission changes. + +What is its intention? + +What benefit does it bring to the project? + +Signed-off-by: My Name diff --git a/.github/workflows/build-and-package.yml b/.github/workflows/build-and-package.yml deleted file mode 100644 index e9471d3..0000000 --- a/.github/workflows/build-and-package.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Build and Package - -on: - push: - branches: [ main ] - -jobs: - build: - name: build and package - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - - name: Install dependencies - run: | - pip install --upgrade pip - pip install build - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - - name: Build and package - run: | - python -m build diff --git a/.github/workflows/check-commit-message.yml b/.github/workflows/check-commit-message.yml new file mode 100644 index 0000000..9c38cfa --- /dev/null +++ b/.github/workflows/check-commit-message.yml @@ -0,0 +1,32 @@ +name: Enforce conventional commits + +on: + pull_request_review: + types: [submitted] + +jobs: + validate: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + ref: ${{ github.head_ref }} + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install commitizen + + - name: Validate pull request title and commit messages + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo "${{ github.event.pull_request.title }}" | cz check diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 3dc41f8..4be90e0 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -2,7 +2,7 @@ name: Pull Request on: pull_request: - branches: [ master ] + branches: [ main ] jobs: build: @@ -10,27 +10,30 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v3 + - name: Checkout code + uses: actions/checkout@v3 - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.10' + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' - - name: Install dependencies - run: | - pip install --upgrade pip - pip install flake8 - pip install build - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Lint with flake8 - run: | - # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - - name: Build and test - run: | - python -m build - python -m pytest + - name: Install dependencies + run: | + pip install --upgrade pip + pip install flake8 + pip install build + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi + + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + + - name: Test with pytest + run: | + pytest diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..aef0699 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,53 @@ +name: Release version + +on: + push: + branches: [ main ] + +permissions: + contents: write + +jobs: + release-version: + if: "!startsWith(github.event.head_commit.message, 'bump:')" + runs-on: ubuntu-latest + name: "Bump version, create changelog and release" + + steps: + - name: Check out + uses: actions/checkout@v3 + with: + token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}" + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install build + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + + - name: Create bump and changelog + uses: commitizen-tools/commitizen-action@master + id: bump + with: + github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} + changelog_increment_filename: release_body.md + + - name: Build package + run: | + python -m build + + - name: Release + uses: softprops/action-gh-release@v1 + with: + body_path: release_body.md + tag_name: ${{ env.REVISION }} + files: dist/* + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 259d74e..5408877 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,5 @@ cover/ # JetBrains .idea/ + +venv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..efe4887 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,8 @@ +repos: +- hooks: + - id: commitizen + - id: commitizen-branch + stages: + - push + repo: https://github.com/commitizen-tools/commitizen + rev: v2.42.0 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..13f31c3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,71 @@ +## v0.13.0 (2023-07-22) + +### Feat + +- add picamera2 tuning filters option (#55) + +## v0.12.0 (2023-07-06) + +### Feat + +- Add compatibility with moonraker update manager (#54) + +## v0.11.2 (2023-07-02) + +### Fix + +- fix url parsing (#53) + +## v0.11.1 (2023-06-10) + +### Fix + +- URL routing to accept requests that contain unused parameters (#42) + +## v0.11.0 (2023-03-19) + +### Feat + +- add exif based image rotation (#37) + +## v0.10.3 (2023-03-05) + +### Fix + +- fix issue with python2 (#40) + +## v0.10.2 (2023-02-19) + +### Fix + +- fix version project configuration + +## v0.10.1 (2023-02-19) + +### Fix + +- build packages after version bump + +## v0.10.0 (2023-02-19) + +### Feat + +- Restrict resolution to 1920x1920 (#31) +- Add testing to project (#7) +- add basic service and installer (#17) + +## v0.0.0 (2023-02-02) + +### Feat + +- Improve project structure and setup (#4) + +### Fix + +- Fix vertical flip (#16) +- Make run.py executable +- Fix python version in GitHub actions + +### Refactor + +- Rename GitHub Action jobs (#6) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 6bf37d6..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,20 +0,0 @@ -# How to Contribute - -:tada: Fist off, thank you very much that you want to contribute! :tada: - -Please help to keep the project maintainable, easy to contribute to, and more secure by following this guide. - -## Pull Requests - -If you want to contribute to the project, making a fork and pull request: - -> 1. Create your own fork -> 2. Clone the fork locally -> 3. Make changes in your local clone -> 4. Push the changes from local to your fork -> 5. Create a pull request to pull the changes from your fork back into the -> upstream repository - -Please use clear commit messages, so we can understand what each commit does. -We'll review every PR and might offer feedback or request changes before -merging. diff --git a/Contributors.md b/Contributors.md deleted file mode 100644 index b2a751c..0000000 --- a/Contributors.md +++ /dev/null @@ -1,3 +0,0 @@ -# Contributors - -* [mryel00](https://github.com/mryel00) diff --git a/LICENSE b/LICENSE index 67dcfaa..2485f00 100644 --- a/LICENSE +++ b/LICENSE @@ -1,201 +1,674 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2023 Alexander Sparkowsky - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. \ No newline at end of file + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Spyglass Copyright (C) 2023 Alexander Sparkowsky + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..de8ac99 --- /dev/null +++ b/Makefile @@ -0,0 +1,63 @@ +## Spyglass Installer +## +## Selfdocumenting Makefile +## Based on https://www.freecodecamp.org/news/self-documenting-makefile/ + + +.PHONY: help install uninstall update + + +#### Install Paths +USER = $(shell whoami) +SYSTEMD = /etc/systemd/system +BIN_PATH = /usr/local/bin +PRINTER_DATA_PATH = /home/$(USER)/printer_data +CONF_PATH = $(PRINTER_DATA_PATH)/config + +all: + $(MAKE) help + +install: ## Install Spyglass as service + @printf "\nCopying systemd service file ...\n" + @sudo cp -f "${PWD}/resources/spyglass.service" $(SYSTEMD) + @sudo sed -i "s/%USER%/$(USER)/g" $(SYSTEMD)/spyglass.service + @printf "\nCopying Spyglass launch script ...\n" + @sudo ln -sf "${PWD}/scripts/spyglass" $(BIN_PATH) + @printf "\nCopying basic configuration file ...\n" + @cp -f "${PWD}/resources/spyglass.conf" $(CONF_PATH) + @printf "\nPopulate new service file ... \n" + @sudo systemctl daemon-reload + @sudo echo "spyglass" >> $(PRINTER_DATA_PATH)/moonraker.asvc + @printf "\nEnable Spyglass service ... \n" + @sudo systemctl enable spyglass + @printf "\nTo be sure, everything is setup please reboot ...\n" + @printf "Thanks for choosing Spyglass ...\n" + +uninstall: ## Uninstall Spyglass + @printf "\nDisable Spyglass service ... \n" + @sudo systemctl disable spyglass + @printf "\nRemove systemd service file ...\n" + @sudo rm -f $(SYSTEMD)/spyglass.service + @printf "\nRemoving Spyglass launch script ...\n" + @sudo rm -f $(BIN_PATH)/spyglass + @sudo sed '/spyglass/d' $(PRINTER_DATA_PATH)/moonraker.asvc > $(PRINTER_DATA_PATH)/moonraker.asvc + +update: ## Update Spyglass (via git Repository) + @git fetch && git pull + +upgrade-moonraker: ## In case of old version of Spyglass being upgraded to newer version with Moonraker update manager compatibility + @printf "Upgrading systemctl ...\n" + @sudo cp -f "${PWD}/resources/spyglass.service" $(SYSTEMD) + @sudo sed -i "s/%USER%/$(USER)/g" $(SYSTEMD)/spyglass.service + @printf "Saving backup of moonraker.asvc file as %s ...\n" $(PRINTER_DATA_PATH)/moonraker.asvc.bak + @sudo cp -f $(PRINTER_DATA_PATH)/moonraker.asvc $(PRINTER_DATA_PATH)/moonraker.asvc.bak + @printf "Upgrading Moonraker update manager authorization ...\n" + @sudo sed -i '/spyglass/d' $(PRINTER_DATA_PATH)/moonraker.asvc + @sudo echo "spyglass" >> $(PRINTER_DATA_PATH)/moonraker.asvc + @printf "You can now include the configuration in moonraker.conf to manage Spyglass updates ...\n" + @printf "Upgrade completed ...\n" + @printf "Thanks for choosing Spyglass ...\n" + +help: ## Show this help + @printf "\nSpyglass Install Helper:\n" + @grep -E -h '\s##\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' diff --git a/README.md b/README.md index 628500b..1f94e62 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,210 @@ # Spyglass ---- -**Please note that this project is in a very early stage. Use at your own risk. Think about contributing to the project -if you find that something is not working, and you are able to fix it. Every contribution is appreciated.** - ---- +> **Please note that this project is in a very early stage. Use at your own risk. Think about contributing to the project +if you find that something is not working, and you are able to fix it. Every contribution is appreciated.** A simple mjpeg server for Picamera2. With Spyglass you are able to stream videos from a camera that is supported by [libcamera](http://libcamera.org) like the [Raspberry Pi Camera Module 3](https://www.raspberrypi.com/products/camera-module-3/). +Current version: 0.13.0 + ## Prerequisites -* Raspberry Pi OS Bullseye -* [Picamera2](https://github.com/raspberrypi/picamera2) - Already installed on Raspberry Pi OS Bullseye -* Python 3 -* A camera supported by libcamera and connected to the Raspberry Pi +- Raspberry Pi OS Bullseye +- [Picamera2](https://github.com/raspberrypi/picamera2) - Already installed on Raspberry Pi OS Bullseye +- Python 3.8+ +- A camera supported by libcamera and connected to the Raspberry Pi ## Quick Start The server can be started with + ```shell ./run.py ``` This will start the server with the following default configuration: -* Address the server binds to: 0.0.0.0 -* Port: 8080 -* Resolution: 640x480 -* Framerate: 15fps -* Stream URL: /stream -* Snapshot URL: /snapshot + +- Address the server binds to: 0.0.0.0 +- Port: 8080 +- Resolution: 640x480 +- Framerate: 15fps +- Stream URL: /stream +- Snapshot URL: /snapshot ## Configuration On startup the following arguments are supported: -| Argument | Description | Default | -|--------------------------|-----------------------------------------------------------------------------------------------------|--------------| -| `-b`, `--bindaddress` | Address where the server will listen for new incoming connections. | `0.0.0.0` | -| `-p`, `--port` | Port where the server will listen for new incoming connections. | `8080` | -| `-r`, `--resolution` | Resolution of the captured frames. This argument expects the format x | `640x480` | -| `-f`, `--fps` | Framerate in frames per second (fps). | `15` | -| `-st`, `--stream_url` | Sets the URL for the mjpeg stream. | `/stream` | -| `-sn`, `--snapshot_url` | Sets the URL for snapshots (single frame of stream). | `/snapshot` | -| `-af`, `--autofocus` | Autofocus mode. Supported modes: `manual`, `continous` | `continuous` | -| `-l`, `--lensposition` | Set focal distance. 0 for infinite focus, 0.5 for approximate 50cm. Only used with Autofocus manual | `0.0` | -| `-s`, `--autofocusspeed` | Autofocus speed. Supported values: `normal`, `fast`. Only used with Autofocus continuous | `normal` | +| Argument | Description | Default | +|-------------------------------|------------------------------------------------------------------------------------------------------------------------------------|--------------| +| `-b`, `--bindaddress` | Address where the server will listen for new incoming connections. | `0.0.0.0` | +| `-p`, `--port` | Port where the server will listen for new incoming connections. | `8080` | +| `-r`, `--resolution` | Resolution of the captured frames. This argument expects the format \x\ | `640x480` | +| `-f`, `--fps` | Framerate in frames per second (fps). | `15` | +| `-st`, `--stream_url` | Sets the URL for the mjpeg stream. | `/stream` | +| `-sn`, `--snapshot_url` | Sets the URL for snapshots (single frame of stream). | `/snapshot` | +| `-af`, `--autofocus` | Autofocus mode. Supported modes: `manual`, `continuous`. | `continuous` | +| `-l`, `--lensposition` | Set focal distance. 0 for infinite focus, 0.5 for approximate 50cm. Only used with Autofocus manual. | `0.0` | +| `-s`, `--autofocusspeed` | Autofocus speed. Supported values: `normal`, `fast`. Only used with Autofocus continuous | `normal` | +| `-ud`, `--upsidedown` | Rotate the image by 180° (see below) | | +| `-fh`, `--flip_horizontal` | Mirror the image horizontally (see below) | | +| `-fv`, `--flip_vertical` | Mirror the image vertically (see below) | | +| `-or`, `--orientation_exif` | Set the image orientation using an EXIF header (see below) | | +| `-c`, `--controls` | Define camera controls to start spyglass with. Can be used multiple times. This argument expects the format \=\. | | +| `--list-controls` | List all available libcamera controls onto the console. Those can be used with `--controls` | | +| `-tf`, `--tuning_filter` | Set a tuning filter file name. | | +| `-tfd`, `--tuning_filter_dir` | Set the directory to look for tuning filters. | | Starting the server without any argument is the same as + ```shell -./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continous -l 0.0 -s normal +./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continuous -l 0.0 -s normal ``` The stream can then be accessed at `http://:8080/stream` +### Maximum resolution + +Please note that the maximum recommended resolution is 1920x1080 (16:9). + +The absolute maximum resolution is 1920x1920. If you choose a higher resolution spyglass may crash. + +### Image Orientation + +There are two ways to change the image orientation. + +To use the ability of picamera2 to transform the image you can use the following options when starting spyglass: + * `-ud` or `--upsidedown` - Rotate the image by 180° + * `-fh` or `--flip_horizontal` - Mirror the image horizontally + * `-fv` or `--flip_vertical` - Mirror the image vertically + +Alternatively you can create an EXIF header to modify the image orientation. Most modern browsers should respect the +this header. + +Use the `-or` or `--orientation_exif` option and choose from one of the following orientations + * `h` - Horizontal (normal) + * `mh` - Mirror horizontal + * `r180` - Rotate 180 + * `mv` - Mirror vertical + * `mhr270` - Mirror horizontal and rotate 270 CW + * `r90` - Rotate 90 CW + * `mhr90` - Mirror horizontal and rotate 90 CW + * `r270` - Rotate 270 CW + +For example to rotate the image 90 degree clockwise you would start spyglass the following way: +```shell +./run.py -or r90 +``` + +### Tuning filter +Tuning filters are used to normalize or modify the camera image output, for example, using an NoIR camera can lead to a pink color, whether applying a filter to it you could remove its tone pink. More information here: https://github.com/raspberrypi/picamera2/blob/main/examples/tuning_file.py + + +Predefined filters can be found at one of the picamera2 directories: +- ~/libcamera/src/ipa/rpi/vc4/data +- /usr/local/share/libcamera/ipa/rpi/vc4 +- /usr/share/libcamera/ipa/rpi/vc4 +- /usr/share/libcamera/ipa/raspberrypi + +You can also define your own directory of filter by using parameter `tuning_filter_dir`. + +list the files present there and you can use it in our config. eg.: `ov5647_noir.json` + + ## Using Spyglass with Mainsail -If you ant to use Spyglass as a webcam source for [Mainsail]() add a webcam with the following configuration: -* URL Stream: `/webcam/stream` -* URL Snapshot: `/webcam/snapshot` -* Service: `V4L-MJPEG` +If you want to use Spyglass as a webcam source for [Mainsail]() add a webcam with the following configuration: + +- URL Stream: `/webcam/stream` +- URL Snapshot: `/webcam/snapshot` +- Service: `V4L-MJPEG` ## Install as application If you want to install Spyglass globally on your machine you can use `python -m pip install .` to do so. + +## Install and run as a service + +### Install + +Quite simple: + +```bash +cd ~/spyglass +make install +``` + +This will ask you for your `sudo` password.\ +After install is done, please reboot to ensure service starts properly + +To uninstall the service simply use + +```bash +cd ~/spyglass +make uninstall +``` + +#### Using Moonraker Update Manager + +To be able to use Moonraker update manager, add the following lines to moonraker.conf: + +```conf +[update_manager spyglass] +type: git_repo +channel: beta +path: ~/spyglass +origin: https://github.com/roamingthings/spyglass.git +managed_services: spyglass +``` +> Make sure moonraker.asvc contains `spyglass` in the list: `cat ~/printer_data/moonraker.asvc | grep spyglass`. +> +> If not there execute: `make upgrade-moonraker` + +### Configuration + +You should find a configuration file in `~/printer_data/config/spyglass.conf`. + +Please see [spyglass.conf](resources/spyglass.conf) for an example + +### Restart the service + +To restart the service use `systemctl`: + +```shell +sudo systemctl restart spyglass +``` + +## Start Developing + +If you want to setup your environment for development perform the following steps: + +Setup your Python virtual environment: +```shell +python -m venv .venv # Create a new virtual environment +. .venv/bin/activate # Activate virtual environment +python -m pip install --upgrade pip # Upgrade PIP to the current version +pip install -r requirements.txt # Install application dependencies +pip install -r requirements-test.txt # Install test dependencies +pip install -r requirements-dev.txt # Install development dependencies +``` + +The project uses [commitizen](https://github.com/commitizen-tools/commitizen) to check commit messages to comply with +[Conventional Commits](http://conventionalcommits.org). When installing the development dependencies git-hooks will be +set up to check commit messages pre commit. + +### Problems when Committing to your Branch + + You may get the following error message when you try to push to your branch: + ``` + fatal: ambiguous argument 'origin/HEAD..HEAD': unknown revision or path not in the working tree. + Use '--' to separate paths from revisions, like this: + 'git [...] -- [...]' + ``` + This error occurs when your HEAD branch is not properly set ([Stack Overflow](https://stackoverflow.com/a/8841024)) + To fix this run the following command: + ```shell + git remote set-head origin -a + ``` diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md new file mode 100644 index 0000000..b427718 --- /dev/null +++ b/docs/CONTRIBUTING.md @@ -0,0 +1,107 @@ +# How to Contribute + +:tada: Fist off, thank you very much that you want to contribute! :tada: + +Please help to keep the project maintainable, easy to contribute to, and more secure by following this guide. + +## The Contribution Process + +If you want to contribute to the project, making a fork and pull request: + +1. Create your own fork. +2. Clone the fork locally. +3. Make changes in your local clone. +4. Push the changes from local to your fork. +5. Create a [GitHub Pull Request](https://github.com/roamingthings/spyglass/pulls) when your submission is ready to + to be deployed into the project. +6. A [reviewer](contributors.md) that is ready to review your submission will assign themselves to the Pull Request on + GitHub. The review process aims at checking the submission for defects, completeness and overall fit into the + general architecture + of the project. +7. After a successful review the Pull Request will be 'approved' on GitHub and will be committed to the main branch by + a [contributor](contributors.md). + +## About the Review Process + +Every contribution to Spyglass will be reviewed before it is merged into the main branch. The review aims to check for +defects and to ensure that the submission follows the general style and architecture of the project. + +It is understood that there is not a single 'best way' to accomplish a task. Therefore, it is not intended to discuss +if the submission is the 'best' implementation. + +Most of the time you will receive feedback from the review. Please be prepared to provide more information or details +and update your submission, if required. + +Common aspects that a review looks for: + +1. Are there any defects and is the submission ready to be widely distributed? +2. Does the submission provide real additional value to the project that will improve what users will get out of the + software? +3. Does the submission include automated tests (e.g. unit test) when applicable that will ensure that the implementation + does what it + is intended to do? +4. Is the copyright of the submission clear, compatible with the project and non-gratuitous? +5. Commits well formatted, cover a single topic and independent? +6. Is the documentation updated to reflect the changes? +7. Does the implementation follow the general style of the project? + +## Format of Commit Messages + +The header of the commit should be conformal with [conventional commits](https://www.conventionalcommits.org) and the +description should be contained in the commit message body. + +``` +: lowercase, present form, short summary (the 'what' of the change) + +Optional, more detailed explanation of what the commit does (the 'why' and 'how'). + +Signed-off-by: My Name +``` + +The `` may be one of the following list: + +* `feat` - A new feature +* `fix` - A bug fix +* `test` - Adding a new test or improve an existing one +* `docs` - Changes or additions to the documentation +* `refactor` - Refactoring of the code or other project elements +* `chore` - Other modifications that do not modify implementation, test or documentations + +It is important to have a "Signed-off-by" line on each commit to certify that you agree to the +[developer certificate of origin](developer-certificate-of-origin.md). Depending on your IDE or editor you can +automatically add this submission line with each commit. +It has to contain your real name (please don't use pseudonyms) and contain a current email address. Unfortunately, we +cannot accept anonymous submissions. + +You can use `git commit -s` to sign off a commit. + +## Format of the Pull Request + +The pull request title and description will help the contributors to describe the change that is finally merged into +the main branch. Each submission will be squashed into a single commit before the merge. + +This project follows the [Conventional Commits specification](https://www.conventionalcommits.org) to help to easily +understand the intention and change of a commit. + +When crating the pull request we ask you to use the following guideline: + +The title of the pull request has the following format: + +``` +: lowercase, present form, short summary (the 'what' of the change) +``` + +If your submission does introduce a breaking change please add `BREAKING CHANGE` to the beginning of the description. + +Similar to a commit the description describes the overall change of the submission and include a `Signed-off-by` line +at the end: + +``` +Describe what the submission changes. + +What is its intention? + +What benefit does it bring to the project? + +Signed-off-by: My Name +``` diff --git a/docs/contributors.md b/docs/contributors.md new file mode 100644 index 0000000..66850c2 --- /dev/null +++ b/docs/contributors.md @@ -0,0 +1,9 @@ +# Main Contributors + +In alphabetical order of GitHub handles: + +| GitHub Handle | Name | +|---------------------------------------------------|----------------------| +| [KwadFan](https://github.com/KwadFan) | Stephan Wendel | +| [mryel00](https://github.com/mryel00) | Patrick Gehrsitz | +| [roamingthings](https://github.com/roamingthings) | Alexander Sparkowsky | diff --git a/docs/developer-certificate-of-origin.md b/docs/developer-certificate-of-origin.md new file mode 100644 index 0000000..49b8cb0 --- /dev/null +++ b/docs/developer-certificate-of-origin.md @@ -0,0 +1,34 @@ +Developer Certificate of Origin +Version 1.1 + +Copyright (C) 2004, 2006 The Linux Foundation and its contributors. + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + + +Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +(a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +(b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +(c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +(d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. diff --git a/pyproject.toml b/pyproject.toml index bf91296..1d11a0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,23 @@ [build-system] build-backend = "setuptools.build_meta" requires = ["setuptools", "wheel"] + +[tool.pytest.ini_options] +addopts = [ + "--import-mode=importlib", +] +pythonpath = [ + "." +] +mock_use_standalone_module = true + +[tool.commitizen] +name = "cz_conventional_commits" +version = "0.13.0" +tag_format = "v$version" +version_files = [ + "spyglass/__version__.py", + "setup.cfg:version", + "pyproject.toml:version", + "README.md:Current version" +] diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..817f696 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +commitizen +pre-commit diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..36ce862 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,4 @@ +pylint +pytest +mock +pytest-mock diff --git a/requirements.txt b/requirements.txt index 8e7d3dc..0612eb5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -setuptools~=66.1.1 +setuptools~=69.0.3 diff --git a/resources/controls_style.css b/resources/controls_style.css new file mode 100644 index 0000000..e88c32e --- /dev/null +++ b/resources/controls_style.css @@ -0,0 +1,69 @@ +body { + font-family: Arial, sans-serif; + background-color: #f6f6f6; + margin: 40px; + display: flex; + flex-direction: column; + align-items: center; +} + +.card-container { + display: flex; + justify-content: center; + align-items: center; + width: 100%; + max-width: 1100px; +} + +.card-container:nth-child(odd) .card { + background-color: white; +} + +.card-container:nth-child(even) .card { + background-color: #f0f0f0; +} + +.card { + border-radius: 5px; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); + padding: 0px 20px 20px 20px; /* top right bottom left */ + width: 80%; + box-sizing: border-box; + transition: 0.3s; +} + +.card:hover { + box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); +} + +.card h2 { + color: #2C3E50; + border-bottom: 1px solid #2C3E50; + padding-bottom: 10px; + margin-bottom: 10px; +} + +.card-content { + display: flex; + margin-bottom: 1px; +} + +.setting { + flex: 1; + display: flex; + margin: 0 5px; +} + +.label, +.value { + font-size: 14px; +} + +.label { + font-weight: bold; + color: #7F8C8D; +} + +.value { + margin-left: 4px; +} diff --git a/resources/spyglass.conf b/resources/spyglass.conf new file mode 100644 index 0000000..1072b56 --- /dev/null +++ b/resources/spyglass.conf @@ -0,0 +1,73 @@ +#### spyglass - Picamera2 MJPG Streamer +#### +#### https://github.com/roamingthings/spyglass +#### +#### This File is distributed under GPLv3 +#### + +#### NOTE: Please ensure parameters are in capital letters and values in lowercase! +#### NOTE: Values has to be surrounded by double quotes! ("value") +#### NOTE: If commented out or includes typos it will use hardcoded defaults! + +#### Running Spyglass with proxy or Standalone (BOOL)[default: true] +NO_PROXY="true" + +#### HTTP Port to listen on (INTEGER)[default: 8080] +HTTP_PORT="8080" + +#### Resolution (INTEGERxINTEGER)[default: 640x480] +RESOLUTION="640x480" +#### NOTE: the maximum supported resolution is 1920x1920 (recommended maximum 1920x1080) + +#### Frames per second (INTEGER)[default: 15] +FPS="15" + +#### Stream URL (STRING)[default: /stream] +STREAM_URL="/stream" +#### NOTE: use format as shown below to stay MJPG-Streamer URL compatible +## STREAM_URL="/?action=stream" + +#### Snapshot URL (STRING)[default: /snapshot] +SNAPSHOT_URL="/snapshot" +#### NOTE: use format as shown below to stay MJPG-Streamer URL compatible +## SNAPSHOT_URL="/?action=snapshot" + +#### Autofocus behavior (STRING:manual,continuous)[default: continuous] +AUTO_FOCUS="continuous" + +#### Focal Distance (float:0.0)[default: 0.0] +#### NOTE: Set focal distance. 0 for infinite focus, 0.5 for approximate 50cm. +#### Only used with Autofocus manual +FOCAL_DIST="0.0" + +#### Auto Focus Speed (STRING:normal,fast)[default: normal] +#### NOTE: Autofocus speed. Supported values: normal, fast. +#### Only used with Autofocus continuous +AF_SPEED="normal" + +#### EXIF Orientation (STRING:h,mh,r180,mv,mhr270,r90,mhr90,r270)[default: h] +#### NOTE: Set the image orientation using an EXIF header. +#### h - Horizontal (normal) +#### mh - Mirror horizontal +#### r180 - Rotate 180 +#### mv - Mirror vertical +#### mhr270 - Mirror horizontal and rotate 270 CW +#### r90 - Rotate 90 CW +#### mhr90 - Mirror horizontal and rotate 90 CW +#### r270 - Rotate 270 CW +ORIENTATION_EXIF="h" + +#### Camera Controls +#### NOTE: Set v4l2 controls your camera supports at startup +#### EXAMPLE: CONTROLS="brightness=0,awbenable=false" +CONTROLS="" + +#### Tuning Filter Directory (STRING)[default: none] +#### NOTE: Directory where to search for tuning filters(if defined). +#### Directory only used if TUNING_FILTER is defined +# TUNING_FILTER_DIR="/usr/share/libcamera/ipa/raspberrypi" + +#### Tuning Filter (STRING)[default: none] +#### NOTE: Name of the file to be used to apply tuning filter. +#### If dir not defined, default pycamera2 directories will be used. +# TUNING_FILTER="ov5647_noir.json" diff --git a/resources/spyglass.service b/resources/spyglass.service new file mode 100644 index 0000000..6843152 --- /dev/null +++ b/resources/spyglass.service @@ -0,0 +1,26 @@ +#### spyglass - Picamera2 MJPG Streamer +#### +#### https://github.com/roamingthings/spyglass +#### +#### This File is distributed under GPLv3 +#### + +[Unit] +Description=spyglass - Picamera2 MJPG Streamer +Documentation=https://github.com/roamingthings/spyglass +After=udev.service network-online.target nss-lookup.target +Wants=udev.service network-online.target +StartLimitBurst=10 +StartLimitIntervalSec=180 + +[Install] +WantedBy=multi-user.target + +[Service] +Type=simple +User=%USER% +RemainAfterExit=Yes +WorkingDirectory=/home/%USER%/spyglass +ExecStart= /usr/local/bin/spyglass +Restart=on-failure +RestartSec=5 diff --git a/run.py b/run.py index f5965e0..4e9d235 100755 --- a/run.py +++ b/run.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 from spyglass.cli import main diff --git a/scripts/spyglass b/scripts/spyglass new file mode 100755 index 0000000..46b6261 --- /dev/null +++ b/scripts/spyglass @@ -0,0 +1,145 @@ +#!/usr/bin/env bash +#### spyglass - Picamera2 MJPG Streamer +#### +#### https://github.com/roamingthings/spyglass +#### +#### This File is distributed under GPLv3 +#### + +# shellcheck enable=require-variable-braces + +### Error Handling +set -Eeou pipefail + + +### Global Variables +BASE_SPY_PATH="$(dirname "$(readlink -f "${0}")")" +PY_BIN="$(command -v python)" +SPYGLASS_CFG="${HOME}/printer_data/config/spyglass.conf" + +### Helper Messages +debug_msg() { + printf "DEBUG: %s\n" "${1}" +} + +help_msg() { + echo -e "spyglass - Picamera2 MJPG Streamer\nUsage:" + echo -e "\t spyglass [Options]" + echo -e "\n\t\t-h Prints this help." + echo -e "\n\t\t-c \n\t\t\tPath to your spyglass.conf" + echo -e "\n\t\t-v Show spyglass version\n" +} + +wrong_args_msg() { + echo -e "spyglass: Wrong Arguments!" + echo -e "\n\tTry: spyglass -h\n" +} + +### Helper Funcs + +## Version of spyglass +self_version() { + pushd "${BASE_SPY_PATH}" &> /dev/null + git describe --always --tags + popd &> /dev/null +} + +check_py_version() { + local version + if [[ -n "${PY_BIN}" ]]; then + version=$("${PY_BIN}" -V | cut -d" " -f2 | cut -d"." -f1) + else + printf "ERROR: Python interpreter is not installed! [EXITING]\n" + exit 1 + fi + if [[ -n "${version}" ]] && [[ "${version}" = "3" ]]; then + printf "INFO: Python interpreter Version %s found ... [OK]\n" "$("${PY_BIN}" -V)" + elif [[ -n "${version}" ]] && [[ "${version}" = "2" ]]; then + printf "ERROR: Python interpreter Version 3 is required! [EXITING]\n" + exit 1 + fi +} + +get_config() { + if [[ -n "${SPYGLASS_CFG}" ]] && [[ -f "${SPYGLASS_CFG}" ]]; then + printf "INFO: Configuration file found in %s\n" "${SPYGLASS_CFG}" + print_config + # shellcheck disable=SC1090 + . "${SPYGLASS_CFG}" + else + printf "ERROR: No configuration file found in %s! [EXITING]\n" "${SPYGLASS_CFG}" + exit 1 + fi +} + +print_config() { + local prefix + prefix="\t\t" + printf "INFO: Print Configfile: '%s'\n" "${SPYGLASS_CFG}" + (sed '/^#.*/d;/./,$!d;/^$/d' | cut -d'#' -f1) < "${SPYGLASS_CFG}" | \ + while read -r line; do + printf "%b%s\n" "${prefix}" "${line}" + done +} + +run_spyglass() { + local bind_adress + # ensure default for NO_PROXY + [[ -n "${NO_PROXY}" ]] || NO_PROXY="true" + + if [[ "${NO_PROXY}" != "true" ]]; then + bind_adress="127.0.0.1" + else + bind_adress="0.0.0.0" + fi + + "${PY_BIN}" "$(dirname "${BASE_SPY_PATH}")/run.py" \ + --bindaddress "${bind_adress}" \ + --port "${HTTP_PORT:-8080}" \ + --resolution "${RESOLUTION:-640x480}" \ + --fps "${FPS:-15}" \ + --stream_url "${STREAM_URL:-\/stream}" \ + --snapshot_url "${SNAPSHOT_URL:-\/snapshot}" \ + --autofocus "${AUTO_FOCUS:-continuous}" \ + --lensposition "${FOCAL_DIST:-0.0}" \ + --autofocusspeed "${AF_SPEED:-normal}" \ + --orientation_exif "${ORIENTATION_EXIF:-h}" \ + --tuning_filter "${TUNING_FILTER:-}"\ + --tuning_filter_dir "${TUNING_FILTER_DIR:-}" \ + --controls-string "${CONTROLS:-0=0}" # 0=0 to prevent error on empty string +} + +#### MAIN +## Parse Args +while getopts ":vhc:d" arg; do + case "${arg}" in + v ) + echo -e "\nspyglass Version: $(self_version)\n" + exit 0 + ;; + h ) + help_msg + exit 0 + ;; + c ) + SPYGLASS_CFG="${OPTARG}" + ;; + d ) + set -x + ;; + \?) + wrong_args_msg + exit 1 + ;; + esac +done + + +check_py_version +get_config +run_spyglass + +### Loop to keep running +while true; do + sleep 1 +done diff --git a/setup.cfg b/setup.cfg index 057cd83..1abba7d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,9 +1,9 @@ [metadata] name = spyglass -version = 0.0.1 +version = 0.13.0 description = A simple mjpeg server for Picamera2 url = https://github.com/roamingthings/spyglass -license = Apache License 2.0 +license = GPL-3.0-only license_files = LICENSE NOTICE diff --git a/spyglass/__version__.py b/spyglass/__version__.py new file mode 100644 index 0000000..f23a6b3 --- /dev/null +++ b/spyglass/__version__.py @@ -0,0 +1 @@ +__version__ = "0.13.0" diff --git a/spyglass/camera.py b/spyglass/camera.py new file mode 100644 index 0000000..e69de29 diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index cf877d4..a277d81 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -5,18 +5,32 @@ from picamera2 import Picamera2 def init_camera( + camera_num: int, width: int, height: int, fps: int, autofocus: str, lens_position: float, autofocus_speed: str, - camera_num: int) -> Camera: + upsidedown=False, + flip_horizontal=False, + flip_vertical=False, + control_list: list[list[str]]=[], + tuning_filter=None, + tuning_filter_dir=None + ) -> Camera: + tuning = None - picam2 = Picamera2(camera_num) + if tuning_filter: + params = {'tuning_file': tuning_filter} + if tuning_filter_dir: + params['dir'] = tuning_filter_dir + tuning = Picamera2.load_tuning_file(**params) + + picam2 = Picamera2(camera_num, tuning=tuning) if picam2._is_rpi_camera(): cam = CSI(width, height, fps, autofocus, lens_position, autofocus_speed) else: cam = USB(width, height, fps, autofocus, lens_position, autofocus_speed) - cam.configure() + cam.configure(control_list, upsidedown, flip_horizontal, flip_vertical) return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index e0cbad3..71ba375 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -19,7 +19,7 @@ def __init__(self, self.fps = fps self.autofocus = autofocus self.lens_position = lens_position - self.autofocus_speed = autofocus_speed + self.autofocus_speed = autofocus_speed def create_controls(self): controls = {'FrameRate': self.fps} @@ -40,19 +40,27 @@ def _run_server(self, output, streaming_handler: StreamingHandler, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): logger.info('Server listening on %s:%d', bind_address, port) logger.info('Streaming endpoint: %s', stream_url) logger.info('Snapshot endpoint: %s', snapshot_url) + logger.info('Controls endpoint: %s', '/controls') address = (bind_address, port) streaming_handler.output = output + streaming_handler.picam2 = self.picam2 streaming_handler.stream_url = stream_url streaming_handler.snapshot_url = snapshot_url + streaming_handler.exif_header = orientation_exif current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() @abstractmethod - def configure(self): + def configure(self, + control_list: list[list[str]]=[], + flip_horizontal=False, + flip_vertical=False, + upsidedown=False): pass @abstractmethod @@ -64,5 +72,6 @@ def start_and_run_server(self, bind_address, port, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): pass diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index a584300..f5b3cf0 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -1,20 +1,34 @@ import io +import libcamera from picamera2.encoders import MJPEGEncoder from picamera2.outputs import FileOutput from threading import Condition from . import camera -from .. server import StreamingHandler +from ..server import StreamingHandler +from ..camera_options import process_controls class CSI(camera.Camera): - def configure(self): + def configure(self, + control_list: list[list[str]]=[], + flip_horizontal=False, + flip_vertical=False, + upsidedown=False): controls = self.create_controls() + c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) + controls.update(c) + + transform = libcamera.Transform( + hflip=int(flip_horizontal or upsidedown), + vflip=int(flip_vertical or upsidedown) + ) self.picam2.configure( self.picam2.create_video_configuration( main={'size': (self.width, self.height)}, - controls=controls + controls=controls, + transform=transform ) ) @@ -22,7 +36,8 @@ def start_and_run_server(self, bind_address, port, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): class StreamingOutput(io.BufferedIOBase): def __init__(self): @@ -41,7 +56,8 @@ def write(self, buf): output, StreamingHandler(), stream_url=stream_url, - snapshot_url=snapshot_url + snapshot_url=snapshot_url, + orientation_exif=orientation_exif ) def stop(self): diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index b7a07d9..8a44419 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -1,16 +1,30 @@ +import libcamera + from . import camera from spyglass.server import StreamingHandler, StreamingServer -from .. import logger +from ..camera_options import process_controls class USB(camera.Camera): - def configure(self): + def configure(self, + control_list: list[list[str]]=[], + flip_horizontal=False, + flip_vertical=False, + upsidedown=False): controls = self.create_controls() + c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) + controls.update(c) + + transform = libcamera.Transform( + hflip=int(flip_horizontal or upsidedown), + vflip=int(flip_vertical or upsidedown) + ) self.picam2.configure( self.picam2.create_preview_configuration( main={'size': (self.width, self.height), 'format': 'MJPEG'}, - controls=controls + controls=controls, + transform=transform ) ) @@ -18,7 +32,8 @@ def start_and_run_server(self, bind_address, port, stream_url='/stream', - snapshot_url='/snapshot'): + snapshot_url='/snapshot', + orientation_exif=0): class StreamingHandlerUSB(StreamingHandler): def get_frame(self): #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer @@ -30,8 +45,9 @@ def get_frame(self): self.picam2, StreamingHandlerUSB(), stream_url=stream_url, - snapshot_url=snapshot_url + snapshot_url=snapshot_url, + orientation_exif=orientation_exif ) - + def stop(self): self.picam2.stop() diff --git a/spyglass/camera_options.py b/spyglass/camera_options.py new file mode 100644 index 0000000..f104c39 --- /dev/null +++ b/spyglass/camera_options.py @@ -0,0 +1,105 @@ +import libcamera +import ast + +def parse_dictionary_to_html_page(camera, parsed_controls='None', processed_controls='None'): + html = """ + + + """ + html += f""" + + + + Camera Settings + + + """ + html += f""" + +

Available camera options

+

Parsed Controls: {parsed_controls}

+

Processed Controls: {processed_controls}

+ """ + for control, values in camera.camera_controls.items(): + html += f""" +
+
+

{control}

+
+
+ Min: + {values[0]} +
+
+ Max: + {values[1]} +
+
+ Default: + {values[2]} +
+
+
+
+ """ + html += """ + + + """ + return html + +def get_style(): + with (open('resources/controls_style.css', 'r')) as f: + return f.read() + +def process_controls(camera, controls: list[tuple[str, str]]) -> dict[str, any]: + controls_dict_lower = { k.lower(): k for k in camera.camera_controls.keys() } + if controls == None: + return {} + processed_controls = {} + for key, value in controls: + key = key.lower().strip() + if key.lower() in controls_dict_lower.keys(): + value = value.lower().strip() + k = controls_dict_lower[key] + v = parse_from_string(value) + processed_controls[k] = v + return processed_controls + +def parse_from_string(input_string: str) -> any: + try: + return ast.literal_eval(input_string) + except (ValueError, TypeError, SyntaxError): + pass + + if input_string.lower() in ['true', 'false']: + return input_string.lower() == 'true' + + return input_string + +def get_type_str(obj) -> str: + return str(type(obj)).split('\'')[1] + +def get_libcamera_controls_string(camera_path: str) -> str: + ctrls_str = "" + libcam_cm = libcamera.CameraManager.singleton() + cam = libcam_cm.cameras[0] + for k, v in cam.controls.items(): + def rectangle_to_tuple(rectangle): + return (rectangle.x, rectangle.y, rectangle.width, rectangle.height) + + if isinstance(v.min, libcamera.Rectangle): + min = rectangle_to_tuple(v.min) + max = rectangle_to_tuple(v.max) + default = rectangle_to_tuple(v.default) + else: + min = v.min + max = v.max + default = v.default + + str_first = f"{k.name} ({get_type_str(min)})" + str_second = f"min={min} max={max} default={default}" + str_indent = (30 - len(str_first)) * ' ' + ': ' + ctrls_str += str_first + str_indent + str_second + '\n' + + return ctrls_str.strip() diff --git a/spyglass/cli.py b/spyglass/cli.py index c8f7973..087ba45 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -1,13 +1,24 @@ -"""cli entry point for hellocli. +"""cli entry point for spyglass. -Parse command line arguments in, invoke hello. +Parse command line arguments in, invoke server. """ import argparse +import logging +import re import sys + import libcamera -import re + +from . import camera_options +from .exif import option_to_exif_orientation +from .__version__ import __version__ from .camera import init_camera + +MAX_WIDTH = 1920 +MAX_HEIGHT = 1920 + + def main(args=None): """Entry point for hello cli. @@ -15,6 +26,8 @@ def main(args=None): becomes sys.exit(main()). The __main__ entry point similarly wraps sys.exit(). """ + logging.info(f"Spyglass {__version__}") + if args is None: args = sys.argv[1:] @@ -25,16 +38,30 @@ def main(args=None): width, height = split_resolution(parsed_args.resolution) stream_url = parsed_args.stream_url snapshot_url = parsed_args.snapshot_url - - cam = init_camera(width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.camera_num) + orientation_exif = parsed_args.orientation_exif + controls = parsed_args.controls + if parsed_args.controls_string: + controls += [c.split('=') for c in parsed_args.controls_string.split(',')] + if parsed_args.list_controls: + print('Available controls:\n'+camera_options.get_libcamera_controls_string(0)) + return + + cam = init_camera( + parsed_args.camera_num, + width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.upsidedown, + parsed_args.flip_horizontal, + parsed_args.flip_vertical, + parsed_args.controls, + parsed_args.tuning_filter, + parsed_args.tuning_filter_dir) try: - cam.start_and_run_server(bind_address, port, stream_url, snapshot_url) + cam.start_and_run_server(bind_address, port, stream_url, snapshot_url, orientation_exif) finally: cam.stop() @@ -47,12 +74,20 @@ def resolution_type(arg_value, pat=re.compile(r"^\d+x\d+$")): return arg_value +def orientation_type(arg_value): + if arg_value in option_to_exif_orientation: + return option_to_exif_orientation[arg_value] + else: + raise argparse.ArgumentTypeError(f"invalid value: unknown orientation {arg_value}.") + + def parse_autofocus(arg_value): if arg_value == 'manual': return libcamera.controls.AfModeEnum.Manual elif arg_value == 'continuous': return libcamera.controls.AfModeEnum.Continuous - raise argparse.ArgumentTypeError("invalid value: manual or continuous expected.") + else: + raise argparse.ArgumentTypeError("invalid value: manual or continuous expected.") def parse_autofocus_speed(arg_value): @@ -60,16 +95,26 @@ def parse_autofocus_speed(arg_value): return libcamera.controls.AfSpeedEnum.Normal elif arg_value == 'fast': return libcamera.controls.AfSpeedEnum.Fast - raise argparse.ArgumentTypeError("invalid value: normal or fast expected.") + else: + raise argparse.ArgumentTypeError("invalid value: normal or fast expected.") def split_resolution(res): parts = res.split('x') w = int(parts[0]) h = int(parts[1]) + if w > MAX_WIDTH or h > MAX_HEIGHT: + raise argparse.ArgumentTypeError("Maximum supported resolution is 1920x1920") return w, h +def control_type(arg_value: str): + if '=' in arg_value: + return arg_value.split('=') + else: + raise argparse.ArgumentTypeError(f"Invalid control: Missing value: {arg_value}") + + # endregion args parsers @@ -92,7 +137,7 @@ def get_parser(): 'connections') parser.add_argument('-p', '--port', type=int, default=8080, help='Bind to port for incoming connections') parser.add_argument('-r', '--resolution', type=resolution_type, default='640x480', - help='Resolution of the images width x height') + help='Resolution of the images width x height. Maximum is 1920x1920.') parser.add_argument('-f', '--fps', type=int, default=15, help='Frames per second to capture') parser.add_argument('-st', '--stream_url', type=str, default='/stream', help='Sets the URL for the mjpeg stream') @@ -105,6 +150,36 @@ def get_parser(): 'Only used with Autofocus manual') parser.add_argument('-s', '--autofocusspeed', type=str, default='normal', choices=['normal', 'fast'], help='Autofocus speed. Only used with Autofocus continuous') + parser.add_argument('-ud', '--upsidedown', action='store_true', + help='Rotate the image by 180° (sensor level)') + parser.add_argument('-fh', '--flip_horizontal', action='store_true', + help='Mirror the image horizontally (sensor level)') + parser.add_argument('-fv', '--flip_vertical', action='store_true', + help='Mirror the image vertically (sensor level)') + parser.add_argument('-or', '--orientation_exif', type=orientation_type, default='h', + help='Set the image orientation using an EXIF header:\n' + ' h - Horizontal (normal)\n' + ' mh - Mirror horizontal\n' + ' r180 - Rotate 180\n' + ' mv - Mirror vertical\n' + ' mhr270 - Mirror horizontal and rotate 270 CW\n' + ' r90 - Rotate 90 CW\n' + ' mhr90 - Mirror horizontal and rotate 90 CW\n' + ' r270 - Rotate 270 CW' + ) + parser.add_argument('-c', '--controls', default=[], type=control_type, action='extend', nargs='*', + help='Define camera controls to start with spyglass. ' + 'Can be used multiple times.\n' + 'Format: =') + parser.add_argument('-cs', '--controls-string', default='', type=str, + help='Define camera controls to start with spyglass. ' + 'Input as a long string.\n' + 'Format: = =') + parser.add_argument('-tf', '--tuning_filter', type=str, default=None, nargs='?', const="", + help='Set a tuning filter file name.') + parser.add_argument('-tfd', '--tuning_filter_dir', type=str, default=None, nargs='?',const="", + help='Set the directory to look for tuning filters.') + parser.add_argument('--list-controls', action='store_true', help='List available camera controls and exits.') parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') return parser diff --git a/spyglass/exif.py b/spyglass/exif.py new file mode 100644 index 0000000..3c1fa74 --- /dev/null +++ b/spyglass/exif.py @@ -0,0 +1,47 @@ +def create_exif_header(orientation: int): + if orientation <= 0: + return None + + return b''.join([ + b'\xFF\xD8', # Start of Image (SOI) marker + b'\xFF\xE1', # APP1 marker + b'\x00\x62', # Length of APP 1 segment (98 bytes) + b'\x45\x78\x69\x66', # EXIF identifier ("Exif" in ASCII) + b'\x00\x00', # Padding bytes + # TIFF header (with big-endian indicator) + b'\x4D\x4D', # Big endian + b'\x00\x2A', # TIFF magic number + b'\x00\x00\x00\x08', # Offset to first IFD (8 bytes) + # Image File Directory (IFD) + b'\x00\x05', # Number of entries in the IFD (5) + # v-- Orientation tag (tag number = 0x0112, type = USHORT, count = 1) + b'\x01\x12', b'\x00\x03', b'\x00\x00\x00\x01', + b'\x00', orientation.to_bytes(1, 'big'), b'\x00\x00', # Tag data + # v-- XResolution tag (tag number = 0x011A, type = UNSIGNED RATIONAL, count = 1) + b'\x01\x1A', b'\x00\x05', b'\x00\x00\x00\x01', + b'\x00\x00\x00\x4A', # Tag data (address) + # v-- YResolution tag (tag number = 0x011B, type = UNSIGNED RATIONAL, count = 1) + b'\x01\x1B', b'\x00\x05', b'\x00\x00\x00\x01', + b'\x00\x00\x00\x52', # Tag data (address) + # v-- ResolutionUnit tag (tag number = 0x0128, type = USHORT, count = 1) + b'\x01\x28', b'\x00\x03', b'\x00\x00\x00\x01', + b'\x00\x02\x00\x00', # 2 - Inch + # v-- YCbCrPositioning tag (tag number = 0x0213, type = USHORT, count = 1) + b'\x02\x13', b'\x00\x03', b'\x00\x00\x00\x01', + b'\x00\x01\x00\x00', # center of pixel array + b'\x00\x00\x00\x00', # Offset to next IFD 0 + b'\x00\x00\x00\x48\x00\x00\x00\x01', # XResolution value + b'\x00\x00\x00\x48\x00\x00\x00\x01' # YResolution value + ]) + + +option_to_exif_orientation = { + 'h': 1, + 'mh': 2, + 'r180': 3, + 'mv': 4, + 'mhr270': 5, + 'r90': 6, + 'mhr90': 7, + 'r270': 8 +} diff --git a/spyglass/server.py b/spyglass/server.py index 64619a1..67ac25a 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -3,22 +3,88 @@ import logging import socketserver from http import server -from . import logger +import io +import logging +import socketserver +from http import server +from threading import Condition +from spyglass.url_parsing import check_urls_match, get_url_params +from spyglass.exif import create_exif_header +from spyglass.camera_options import parse_dictionary_to_html_page, process_controls class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True class StreamingHandler(server.BaseHTTPRequestHandler): - output = None - stream_url = '' - snapshot_url = '' + @property + def output(self): + return self._output + + @output.setter + def output(self, value): + self._output = value + + @property + def picam2(self): + return self._picam2 + + @picam2.setter + def picam2(self, value): + self._picam2 = value + + @property + def stream_url(self): + try: + return self._stream_url + except NameError: + return '/stream' + + @stream_url.setter + def stream_url(self, value): + self._stream_url = value + + @property + def snapshot_url(self): + try: + return self._snapshot_url + except NameError: + return '/snapshot' + + @snapshot_url.setter + def snapshot_url(self, value): + self._snapshot_url = value + + @property + def exif_header(self): + try: + return self._exif_header + except NameError: + return None + + @exif_header.setter + def exif_header(self, orientation_exif): + if orientation_exif > 0: + self._exif_header = create_exif_header(orientation_exif) + else: + self._exif_header = None def do_GET(self): - if self.path == self.stream_url: + if check_urls_match(self.stream_url, self.path): self.start_streaming() - elif self.path == self.snapshot_url: + elif check_urls_match(self.snapshot_url, self.path): self.send_snapshot() + elif check_urls_match('/controls', self.path): + parsed_controls = get_url_params(self.path) + parsed_controls = parsed_controls if parsed_controls else None + processed_controls = process_controls(self.picam2, parsed_controls) + self.picam2.set_controls(processed_controls) + content = parse_dictionary_to_html_page(self.picam2, parsed_controls, processed_controls).encode('utf-8') + self.send_response(200) + self.send_header('Content-Type', 'text/html') + self.send_header('Content-Length', len(content)) + self.end_headers() + self.wfile.write(content) else: self.send_error(404) self.end_headers() @@ -32,10 +98,17 @@ def start_streaming(self): while True: frame = self.get_frame() self.wfile.write(b'--FRAME\r\n') - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) - self.wfile.write(b'\r\n') + if self.exif_header is None: + self.send_jpeg_content_headers(frame) + self.end_headers() + self.wfile.write(frame) + self.wfile.write(b'\r\n') + else: + self.send_jpeg_content_headers(frame, len(self.exif_header) - 2) + self.end_headers() + self.wfile.write(self.exif_header) + self.wfile.write(frame[2:]) + self.wfile.write(b'\r\n') except Exception as e: logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) @@ -44,9 +117,15 @@ def send_snapshot(self): self.send_response(200) self.send_default_headers() frame = self.get_frame() - self.send_jpeg_content_headers(frame) - self.end_headers() - self.wfile.write(frame) + if self.exif_header is None: + self.send_jpeg_content_headers(frame) + self.end_headers() + self.wfile.write(frame) + else: + self.send_jpeg_content_headers(frame, len(self.exif_header) - 2) + self.end_headers() + self.wfile.write(self.exif_header) + self.wfile.write(frame[2:]) except Exception as e: logging.warning( 'Removed client %s: %s', @@ -57,23 +136,11 @@ def send_default_headers(self): self.send_header('Cache-Control', 'no-cache, private') self.send_header('Pragma', 'no-cache') - def send_jpeg_content_headers(self, frame): + def send_jpeg_content_headers(self, frame, extra_len=0): self.send_header('Content-Type', 'image/jpeg') - self.send_header('Content-Length', len(frame)) + self.send_header('Content-Length', str(len(frame) + extra_len)) def get_frame(self): with self.output.condition: self.output.condition.wait() return self.output.frame - -def run_server(bind_address, port, output, stream_url='/stream', snapshot_url='/snapshot'): - logger.info('Server listening on %s:%d', bind_address, port) - logger.info('Streaming endpoint: %s', stream_url) - logger.info('Snapshot endpoint: %s', snapshot_url) - address = (bind_address, port) - streaming_handler = StreamingHandler - streaming_handler.output = output - streaming_handler.stream_url = stream_url - streaming_handler.snapshot_url = snapshot_url - current_server = StreamingServer(address, streaming_handler) - current_server.serve_forever() diff --git a/spyglass/url_parsing.py b/spyglass/url_parsing.py new file mode 100644 index 0000000..3c07126 --- /dev/null +++ b/spyglass/url_parsing.py @@ -0,0 +1,49 @@ +from urllib.parse import urlparse, parse_qsl + +def check_paths_match(expected_url, incoming_url): + # Assign paths from URL into list + exp_paths = urlparse(expected_url.strip("/")).path.split("/") + inc_paths = urlparse(incoming_url.strip("/")).path.split("/") + + # Drop ip/hostname if present in path + if '.' in exp_paths[0]: exp_paths.pop(0) + if '.' in inc_paths[0]: inc_paths.pop(0) + + # Filter out empty strings + # This allows e.g. /stream/?action=stream for /stream?action=stream + exp_paths = list(filter(None, exp_paths)) + inc_paths = list(filter(None, inc_paths)) + + # Determine if match + if len(exp_paths)==len(inc_paths): + return all([exp == inc for exp, inc in zip(exp_paths, inc_paths)]) + + return False + +def get_url_params(url): + # Get URL params + params = parse_qsl(urlparse(url).query) + + return params + +def check_params_match(expected_url, incoming_url): + # Check URL params + exp_params = get_url_params(expected_url) + inc_params = get_url_params(incoming_url) + + # Create list of matching params + matching_params = set(exp_params) & set(inc_params) + + # Update list order for expected params + exp_params = set(exp_params) + + return matching_params==exp_params + +def check_urls_match(expected_url, incoming_url): + # Check URL paths + paths_match = check_paths_match(expected_url, incoming_url) + + # Check URL params + params_match = check_params_match(expected_url, incoming_url) + + return paths_match and params_match diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..a0a3a4f --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,474 @@ +import argparse +import pytest +from unittest.mock import MagicMock, ANY, patch + +AF_SPEED_ENUM_NORMAL = 1 +AF_SPEED_ENUM_FAST = 2 +AF_MODE_ENUM_CONTINUOUS = 2 +AF_MODE_ENUM_MANUAL = 3 + +DEFAULT_HEIGHT = 480 +DEFAULT_WIDTH = 640 +DEFAULT_FLIP_VERTICALLY = False +DEFAULT_FLIP_HORIZONTALLY = False +DEFAULT_UPSIDE_DOWN = False +DEFAULT_LENS_POSITION = 0.0 +DEFAULT_FPS = 15 +DEFAULT_AF_SPEED = AF_SPEED_ENUM_NORMAL +DEFAULT_AUTOFOCUS_MODE = AF_MODE_ENUM_CONTINUOUS +DEFAULT_CONTROLS = [] +DEFAULT_TUNING_FILTER = None +DEFAULT_TUNING_FILTER_DIR = None + + +@pytest.fixture(autouse=True) +def mock_libraries(mocker): + mock_libcamera = MagicMock() + mock_picamera2 = MagicMock() + mock_picamera2_encoders = MagicMock() + mock_picamera2_outputs = MagicMock() + mocker.patch.dict('sys.modules', { + 'libcamera': mock_libcamera, + 'picamera2': mock_picamera2, + 'picamera2.encoders': mock_picamera2_encoders, + 'picamera2.outputs': mock_picamera2_outputs, + }) + mocker.patch('libcamera.controls.AfModeEnum.Manual', AF_MODE_ENUM_MANUAL) + mocker.patch('libcamera.controls.AfModeEnum.Continuous', AF_MODE_ENUM_CONTINUOUS) + mocker.patch('libcamera.controls.AfSpeedEnum.Normal', AF_SPEED_ENUM_NORMAL) + mocker.patch('libcamera.controls.AfSpeedEnum.Fast', AF_SPEED_ENUM_FAST) + + +def test_parse_bindaddress(): + from spyglass import cli + args = cli.get_args(['-b', '1.2.3.4']) + assert args.bindaddress == '1.2.3.4' + + +def test_parse_port(): + from spyglass import cli + args = cli.get_args(['-p', '123']) + assert args.port == 123 + + +def test_parse_resolution(): + from spyglass import cli + args = cli.get_args(['-r', '100x200']) + assert args.resolution == '100x200' + + +def test_split_resolution(): + from spyglass import cli + (width, height) = cli.split_resolution('100x200') + assert width == 100 + assert height == 200 + + +def test_parse_tuning_filter(): + from spyglass import cli + args = cli.get_args(['-tf', 'filter']) + assert args.tuning_filter == 'filter' + + +def test_parse_tuning_filter_dir(): + from spyglass import cli + args = cli.get_args(['-tfd', 'dir']) + assert args.tuning_filter_dir == 'dir' + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_with_defaults(mock_spyglass_camera, mock_spyglass_server): + from spyglass import cli + import spyglass.camera + cli.main(args=[]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_resolution(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-r', '200x100' + ]) + spyglass.camera.init_camera.assert_called_once_with( + 200, + 100, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +def test_raise_error_when_width_greater_than_maximum(): + from spyglass import cli + with pytest.raises(argparse.ArgumentTypeError): + cli.main(args=[ + '-r', '1921x1920' + ]) + + +def test_raise_error_when_height_greater_than_maximum(): + from spyglass import cli + with pytest.raises(argparse.ArgumentTypeError): + cli.main(args=[ + '-r', '1920x1921' + ]) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_fps(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-f', '20' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + 20, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_manual(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-af', 'manual' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + AF_MODE_ENUM_MANUAL, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_continuous(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-af', 'continuous' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + AF_MODE_ENUM_CONTINUOUS, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_lens_position(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-l', '1.0' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + 1.0, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_speed_normal(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-s', 'normal' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + AF_SPEED_ENUM_NORMAL, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_af_speed_fast(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-s', 'fast' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + AF_SPEED_ENUM_FAST, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_upside_down(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-ud' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + True, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_flip_horizontal(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-fh' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + True, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_flip_vertical(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-fv' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + True, + DEFAULT_CONTROLS, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_controls(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-c', 'brightness=-0.4', + '-c', 'awbenable=false' + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + [['brightness', '-0.4'],['awbenable', 'false']], + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.server + cli.main(args=[ + '-b', '1.2.3.4', + '-p', '1234', + '-st', 'streaming-url', + '-sn', 'snapshot-url', + '-or', 'h' + ]) + spyglass.server.run_server.assert_called_once_with('1.2.3.4', 1234, ANY, ANY, 'streaming-url', 'snapshot-url', 1) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +@pytest.mark.parametrize("input_value, expected_output", [ + ('h', 1), + ('mh', 2), + ('r180', 3), + ('mv', 4), + ('mhr270', 5), + ('r90', 6), + ('mhr90', 7), + ('r270', 8), +]) +def test_run_server_with_orientation(mock_spyglass_server, mock_spyglass_camera, input_value, expected_output): + from spyglass import cli + import spyglass.server + cli.main(args=[ + '-b', '1.2.3.4', + '-p', '1234', + '-st', 'streaming-url', + '-sn', 'snapshot-url', + '-or', input_value + ]) + spyglass.server.run_server.assert_called_once_with( + '1.2.3.4', + 1234, + ANY, + ANY, + 'streaming-url', + 'snapshot-url', + expected_output + ) + + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_using_only_tuning_filter_file(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-tf', 'test', + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + "test", + DEFAULT_TUNING_FILTER_DIR + ) + +@patch("spyglass.server.run_server") +@patch("spyglass.camera.init_camera") +def test_init_camera_using_tuning_filters(mock_spyglass_server, mock_spyglass_camera): + from spyglass import cli + import spyglass.camera + cli.main(args=[ + '-tf', 'test', + '-tfd', 'test-dir', + ]) + spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_WIDTH, + DEFAULT_HEIGHT, + DEFAULT_FPS, + DEFAULT_AUTOFOCUS_MODE, + DEFAULT_LENS_POSITION, + DEFAULT_AF_SPEED, + DEFAULT_UPSIDE_DOWN, + DEFAULT_FLIP_HORIZONTALLY, + DEFAULT_FLIP_VERTICALLY, + DEFAULT_CONTROLS, + "test", + "test-dir" + ) diff --git a/tests/test_exif.py b/tests/test_exif.py new file mode 100644 index 0000000..1650e60 --- /dev/null +++ b/tests/test_exif.py @@ -0,0 +1,17 @@ +import pytest + + +@pytest.mark.parametrize("input_value, expected_output", [ + ('h', 1), + ('mh', 2), + ('r180', 3), + ('mv', 4), + ('mhr270', 5), + ('r90', 6), + ('mhr90', 7), + ('r270', 8), +]) +def test_option_to_exif_orientation_map(input_value, expected_output): + from spyglass.exif import option_to_exif_orientation + orientation_value = option_to_exif_orientation[input_value] + assert orientation_value == expected_output diff --git a/tests/test_url_parsing.py b/tests/test_url_parsing.py new file mode 100644 index 0000000..5fb08ac --- /dev/null +++ b/tests/test_url_parsing.py @@ -0,0 +1,137 @@ +import pytest + + +@pytest.mark.parametrize("expected_url, incoming_url, expected_output", [ + ('/?a=b', '/?a=b', True), + ('/?a=b', '/?b=a', True), + ('/?a=b', '/?a=b&', True), + ('/?a=b', '/?b=c&d=e', True), + ('/?a=b', '/?a=b&c=d', True), + ('/?a=b', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?a=b', True), + ('/?a=b&c=d', '/?a=b&c=d', True), + ('/?a=b&c=d', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?d=e&a=b', True), + + ('/?a=b', '/a?a=b', False), + ('/?a=b', '/a?b=a', False), + ('/?a=b', '/a?b=c&d=e', False), + ('/?a=b&c=d', '/a?a=b', False), + ('/?a=b&c=d', '/a?a=b&c=d', False), + ('/?a=b&c=d', '/a?c=d&a=b', False), + + ('/a', '/a', True), + ('/a', '/b', False), + ('/a', '/a?b=c', True), + ('/a', '/a/?b=c', True), + ('/a', '/b/?b=c', False), + ('/a', '/?a=b', False), + + ('/a?a=b', '/a?a=b', True), + ('/a?a=b', '/a?b=a', True), + ('/a?a=b', '/a?b=c&d=e', True), + ('/a?a=b&c=d', '/a?a=b', True), + ('/a?a=b&c=d', '/a?a=b&c=d', True), + ('/a?a=b&c=d', '/a?c=d&a=b', True), + + ('/a?a=b', '/b?a=b', False), + ('/a?a=b', '/b?b=a', False), + ('/a?a=b', '/b?b=c&d=e', False), + ('/a?a=b&c=d', '/b?a=b', False), + ('/a?a=b&c=d', '/b?a=b&c=d', False), + ('/a?a=b&c=d', '/b?c=d&a=b', False) +]) +def test_check_paths_match(expected_url, incoming_url, expected_output): + from spyglass.url_parsing import check_paths_match + match_value = check_paths_match(expected_url, incoming_url) + assert match_value == expected_output + +@pytest.mark.parametrize("expected_url, incoming_url, expected_output", [ + ('/?a=b', '/?a=b', True), + ('/?a=b', '/?b=a', False), + ('/?a=b', '/?a=b&', True), + ('/?a=b', '/?b=c&d=e', False), + ('/?a=b', '/?a=b&c=d', True), + ('/?a=b', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?a=b', False), + ('/?a=b&c=d', '/?a=b&c=d', True), + ('/?a=b&c=d', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?d=e&a=b', False), + + ('/?a=b', '/a?a=b', True), + ('/?a=b', '/a?b=a', False), + ('/?a=b', '/a?b=c&d=e', False), + ('/?a=b&c=d', '/a?a=b', False), + ('/?a=b&c=d', '/a?a=b&c=d', True), + ('/?a=b&c=d', '/a?c=d&a=b', True), + + ('/a', '/a', True), + ('/a', '/b', True), + ('/a', '/a?b=c', True), + ('/a', '/a/?b=c', True), + ('/a', '/b/?b=c', True), + ('/a', '/?a=b', True), + + ('/a?a=b', '/a?a=b', True), + ('/a?a=b', '/a?b=a', False), + ('/a?a=b', '/a?b=c&d=e', False), + ('/a?a=b&c=d', '/a?a=b', False), + ('/a?a=b&c=d', '/a?a=b&c=d', True), + ('/a?a=b&c=d', '/a?c=d&a=b', True), + + ('/a?a=b', '/b?a=b', True), + ('/a?a=b', '/b?b=a', False), + ('/a?a=b', '/b?b=c&d=e', False), + ('/a?a=b&c=d', '/b?a=b', False), + ('/a?a=b&c=d', '/b?a=b&c=d', True), + ('/a?a=b&c=d', '/b?c=d&a=b', True) +]) +def test_check_params_match(expected_url, incoming_url, expected_output): + from spyglass.url_parsing import check_params_match + match_value = check_params_match(expected_url, incoming_url) + assert match_value == expected_output + +@pytest.mark.parametrize("expected_url, incoming_url, expected_output", [ + ('/?a=b', '/?a=b', True), + ('/?a=b', '/?b=a', False), + ('/?a=b', '/?a=b&', True), + ('/?a=b', '/?b=c&d=e', False), + ('/?a=b', '/?a=b&c=d', True), + ('/?a=b', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?a=b', False), + ('/?a=b&c=d', '/?a=b&c=d', True), + ('/?a=b&c=d', '/?c=d&a=b', True), + ('/?a=b&c=d', '/?d=e&a=b', False), + + ('/?a=b', '/a?a=b', False), + ('/?a=b', '/a?b=a', False), + ('/?a=b', '/a?b=c&d=e', False), + ('/?a=b&c=d', '/a?a=b', False), + ('/?a=b&c=d', '/a?a=b&c=d', False), + ('/?a=b&c=d', '/a?c=d&a=b', False), + + ('/a', '/a', True), + ('/a', '/b', False), + ('/a', '/a?b=c', True), + ('/a', '/a/?b=c', True), + ('/a', '/b/?b=c', False), + ('/a', '/?a=b', False), + + ('/a?a=b', '/a?a=b', True), + ('/a?a=b', '/a?b=a', False), + ('/a?a=b', '/a?b=c&d=e', False), + ('/a?a=b&c=d', '/a?a=b', False), + ('/a?a=b&c=d', '/a?a=b&c=d', True), + ('/a?a=b&c=d', '/a?c=d&a=b', True), + + ('/a?a=b', '/b?a=b', False), + ('/a?a=b', '/b?b=a', False), + ('/a?a=b', '/b?b=c&d=e', False), + ('/a?a=b&c=d', '/b?a=b', False), + ('/a?a=b&c=d', '/b?a=b&c=d', False), + ('/a?a=b&c=d', '/b?c=d&a=b', False) +]) +def test_check_urls_match(expected_url, incoming_url, expected_output): + from spyglass.url_parsing import check_urls_match + match_value = check_urls_match(expected_url, incoming_url) + assert match_value == expected_output From 84ff1f48a8760eef4fcc6042c671831e8309ec26 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 14:25:32 +0200 Subject: [PATCH 04/18] fix: fix multiple things Signed-off-by: Patrick Gehrsitz --- spyglass/camera/__init__.py | 4 +-- spyglass/camera/camera.py | 22 ++++++++++---- spyglass/camera/csi.py | 15 +++++++--- spyglass/camera/usb.py | 18 ++++++------ spyglass/server.py | 57 ------------------------------------- 5 files changed, 39 insertions(+), 77 deletions(-) diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index a277d81..108704d 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -29,8 +29,8 @@ def init_camera( picam2 = Picamera2(camera_num, tuning=tuning) if picam2._is_rpi_camera(): - cam = CSI(width, height, fps, autofocus, lens_position, autofocus_speed) + cam = CSI(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) else: - cam = USB(width, height, fps, autofocus, lens_position, autofocus_speed) + cam = USB(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) cam.configure(control_list, upsidedown, flip_horizontal, flip_vertical) return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 71ba375..110e903 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -1,7 +1,9 @@ from abc import ABC, abstractmethod from picamera2 import Picamera2 +import libcamera from .. import logger -from .. server import StreamingServer, StreamingHandler +from ..exif import create_exif_header +from ..server import StreamingServer, StreamingHandler class Camera(ABC): def __init__(self, @@ -22,12 +24,15 @@ def __init__(self, self.autofocus_speed = autofocus_speed def create_controls(self): - controls = {'FrameRate': self.fps} + controls = {} + + if 'FrameRate' in self.picam2.camera_controls: + controls['FrameRate'] = self.fps if 'AfMode' in self.picam2.camera_controls: controls['AfMode'] = self.autofocus controls['AfSpeed'] = self.autofocus_speed - if self.autofocus == self.libcamera.controls.AfModeEnum.Manual: + if self.autofocus == libcamera.controls.AfModeEnum.Manual: controls['LensPosition'] = self.lens_position else: print('Attached camera does not support autofocus') @@ -39,6 +44,7 @@ def _run_server(self, port, output, streaming_handler: StreamingHandler, + get_frame, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): @@ -49,18 +55,22 @@ def _run_server(self, address = (bind_address, port) streaming_handler.output = output streaming_handler.picam2 = self.picam2 + streaming_handler.get_frame = get_frame streaming_handler.stream_url = stream_url streaming_handler.snapshot_url = snapshot_url - streaming_handler.exif_header = orientation_exif + if orientation_exif > 0: + streaming_handler.exif_header = create_exif_header(orientation_exif) + else: + streaming_handler.exif_header = None current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() @abstractmethod def configure(self, control_list: list[list[str]]=[], + upsidedown=False, flip_horizontal=False, - flip_vertical=False, - upsidedown=False): + flip_vertical=False): pass @abstractmethod diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index f5b3cf0..7270ae4 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -12,9 +12,9 @@ class CSI(camera.Camera): def configure(self, control_list: list[list[str]]=[], + upsidedown=False, flip_horizontal=False, - flip_vertical=False, - upsidedown=False): + flip_vertical=False): controls = self.create_controls() c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) controls.update(c) @@ -38,7 +38,7 @@ def start_and_run_server(self, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): - + class StreamingOutput(io.BufferedIOBase): def __init__(self): self.frame = None @@ -49,12 +49,19 @@ def write(self, buf): self.frame = buf self.condition.notify_all() output = StreamingOutput() + def get_frame(self): + with output.condition: + output.condition.wait() + return output.frame + self.picam2.start_recording(MJPEGEncoder(), FileOutput(output)) + self._run_server( bind_address, port, output, - StreamingHandler(), + StreamingHandler, + get_frame, stream_url=stream_url, snapshot_url=snapshot_url, orientation_exif=orientation_exif diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index 8a44419..e3fa8f8 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -2,15 +2,15 @@ from . import camera -from spyglass.server import StreamingHandler, StreamingServer +from spyglass.server import StreamingHandler from ..camera_options import process_controls class USB(camera.Camera): def configure(self, control_list: list[list[str]]=[], + upsidedown=False, flip_horizontal=False, - flip_vertical=False, - upsidedown=False): + flip_vertical=False): controls = self.create_controls() c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) controls.update(c) @@ -34,16 +34,18 @@ def start_and_run_server(self, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): - class StreamingHandlerUSB(StreamingHandler): - def get_frame(self): - #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer - return self.output.capture_buffer() + def get_frame(self2): + #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer + return self.picam2.capture_buffer() + self.picam2.start() + self._run_server( bind_address, port, self.picam2, - StreamingHandlerUSB(), + StreamingHandler, + get_frame, stream_url=stream_url, snapshot_url=snapshot_url, orientation_exif=orientation_exif diff --git a/spyglass/server.py b/spyglass/server.py index 67ac25a..22fe55e 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -17,58 +17,6 @@ class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): daemon_threads = True class StreamingHandler(server.BaseHTTPRequestHandler): - @property - def output(self): - return self._output - - @output.setter - def output(self, value): - self._output = value - - @property - def picam2(self): - return self._picam2 - - @picam2.setter - def picam2(self, value): - self._picam2 = value - - @property - def stream_url(self): - try: - return self._stream_url - except NameError: - return '/stream' - - @stream_url.setter - def stream_url(self, value): - self._stream_url = value - - @property - def snapshot_url(self): - try: - return self._snapshot_url - except NameError: - return '/snapshot' - - @snapshot_url.setter - def snapshot_url(self, value): - self._snapshot_url = value - - @property - def exif_header(self): - try: - return self._exif_header - except NameError: - return None - - @exif_header.setter - def exif_header(self, orientation_exif): - if orientation_exif > 0: - self._exif_header = create_exif_header(orientation_exif) - else: - self._exif_header = None - def do_GET(self): if check_urls_match(self.stream_url, self.path): self.start_streaming() @@ -139,8 +87,3 @@ def send_default_headers(self): def send_jpeg_content_headers(self, frame, extra_len=0): self.send_header('Content-Type', 'image/jpeg') self.send_header('Content-Length', str(len(frame) + extra_len)) - - def get_frame(self): - with self.output.condition: - self.output.condition.wait() - return self.output.frame From 6e59d1f98da59c549754721a86d8e83eca4b3870 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 14:37:07 +0200 Subject: [PATCH 05/18] chore: adjust variables for better code highlighting Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 2 -- spyglass/camera/csi.py | 1 - spyglass/camera/usb.py | 3 +-- spyglass/server.py | 8 ++++++++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 110e903..78a81df 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -42,7 +42,6 @@ def create_controls(self): def _run_server(self, bind_address, port, - output, streaming_handler: StreamingHandler, get_frame, stream_url='/stream', @@ -53,7 +52,6 @@ def _run_server(self, logger.info('Snapshot endpoint: %s', snapshot_url) logger.info('Controls endpoint: %s', '/controls') address = (bind_address, port) - streaming_handler.output = output streaming_handler.picam2 = self.picam2 streaming_handler.get_frame = get_frame streaming_handler.stream_url = stream_url diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 7270ae4..ac9c1a4 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -59,7 +59,6 @@ def get_frame(self): self._run_server( bind_address, port, - output, StreamingHandler, get_frame, stream_url=stream_url, diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index e3fa8f8..7994670 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -34,7 +34,7 @@ def start_and_run_server(self, stream_url='/stream', snapshot_url='/snapshot', orientation_exif=0): - def get_frame(self2): + def get_frame(inner_self): #TODO: Cuts framerate in 1/n with n streams open, add some kind of buffer return self.picam2.capture_buffer() @@ -43,7 +43,6 @@ def get_frame(self2): self._run_server( bind_address, port, - self.picam2, StreamingHandler, get_frame, stream_url=stream_url, diff --git a/spyglass/server.py b/spyglass/server.py index 22fe55e..c1f46ed 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -17,6 +17,14 @@ class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): daemon_threads = True class StreamingHandler(server.BaseHTTPRequestHandler): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.picam2 = None + self.exif_header = None + self.stream_url = None + self.snapshot_url = None + self.get_frame = None + def do_GET(self): if check_urls_match(self.stream_url, self.path): self.start_streaming() From 69fc5b27eb71975b0a4ddd4fb1dbf59e8fb0d1ae Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 17:06:59 +0200 Subject: [PATCH 06/18] refactor: refactor camera code Signed-off-by: Patrick Gehrsitz --- spyglass/camera/__init__.py | 5 --- spyglass/camera/camera.py | 87 ++++++++++++++++++++----------------- spyglass/camera/csi.py | 23 ---------- spyglass/camera/usb.py | 24 +--------- spyglass/cli.py | 32 +++++++------- 5 files changed, 64 insertions(+), 107 deletions(-) diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index 108704d..c7a863d 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -12,10 +12,6 @@ def init_camera( autofocus: str, lens_position: float, autofocus_speed: str, - upsidedown=False, - flip_horizontal=False, - flip_vertical=False, - control_list: list[list[str]]=[], tuning_filter=None, tuning_filter_dir=None ) -> Camera: @@ -32,5 +28,4 @@ def init_camera( cam = CSI(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) else: cam = USB(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) - cam.configure(control_list, upsidedown, flip_horizontal, flip_vertical) return cam diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 78a81df..d090aa2 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -3,50 +3,65 @@ import libcamera from .. import logger from ..exif import create_exif_header +from ..camera_options import process_controls from ..server import StreamingServer, StreamingHandler class Camera(ABC): - def __init__(self, - picam2: Picamera2, - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str): - + def __init__(self, picam2: Picamera2): self.picam2 = picam2 - self.width = width - self.height = height - self.fps = fps - self.autofocus = autofocus - self.lens_position = lens_position - self.autofocus_speed = autofocus_speed - def create_controls(self): + def create_controls(self, fps: int, autofocus: str, lens_position: float, autofocus_speed: str): controls = {} if 'FrameRate' in self.picam2.camera_controls: - controls['FrameRate'] = self.fps + controls['FrameRate'] = fps if 'AfMode' in self.picam2.camera_controls: - controls['AfMode'] = self.autofocus - controls['AfSpeed'] = self.autofocus_speed - if self.autofocus == libcamera.controls.AfModeEnum.Manual: - controls['LensPosition'] = self.lens_position + controls['AfMode'] = autofocus + controls['AfSpeed'] = autofocus_speed + if autofocus == libcamera.controls.AfModeEnum.Manual: + controls['LensPosition'] = lens_position else: print('Attached camera does not support autofocus') return controls + def configure(self, + width: int, + height: int, + fps: int, + autofocus: str, + lens_position: float, + autofocus_speed: str, + control_list: list[list[str]]=[], + upsidedown=False, + flip_horizontal=False, + flip_vertical=False): + controls = self.create_controls(fps, autofocus, lens_position, autofocus_speed) + c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) + controls.update(c) + + transform = libcamera.Transform( + hflip=int(flip_horizontal or upsidedown), + vflip=int(flip_vertical or upsidedown) + ) + + self.picam2.configure( + self.picam2.create_video_configuration( + main={'size': (width, height)}, + controls=controls, + transform=transform + ) + ) + def _run_server(self, - bind_address, - port, - streaming_handler: StreamingHandler, - get_frame, - stream_url='/stream', - snapshot_url='/snapshot', - orientation_exif=0): + bind_address, + port, + streaming_handler: StreamingHandler, + get_frame, + stream_url='/stream', + snapshot_url='/snapshot', + orientation_exif=0): logger.info('Server listening on %s:%d', bind_address, port) logger.info('Streaming endpoint: %s', stream_url) logger.info('Snapshot endpoint: %s', snapshot_url) @@ -63,18 +78,6 @@ def _run_server(self, current_server = StreamingServer(address, streaming_handler) current_server.serve_forever() - @abstractmethod - def configure(self, - control_list: list[list[str]]=[], - upsidedown=False, - flip_horizontal=False, - flip_vertical=False): - pass - - @abstractmethod - def stop(self): - pass - @abstractmethod def start_and_run_server(self, bind_address, @@ -83,3 +86,7 @@ def start_and_run_server(self, snapshot_url='/snapshot', orientation_exif=0): pass + + @abstractmethod + def stop(self): + pass diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index ac9c1a4..375253b 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -7,31 +7,8 @@ from . import camera from ..server import StreamingHandler -from ..camera_options import process_controls class CSI(camera.Camera): - def configure(self, - control_list: list[list[str]]=[], - upsidedown=False, - flip_horizontal=False, - flip_vertical=False): - controls = self.create_controls() - c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) - controls.update(c) - - transform = libcamera.Transform( - hflip=int(flip_horizontal or upsidedown), - vflip=int(flip_vertical or upsidedown) - ) - - self.picam2.configure( - self.picam2.create_video_configuration( - main={'size': (self.width, self.height)}, - controls=controls, - transform=transform - ) - ) - def start_and_run_server(self, bind_address, port, diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index 7994670..0aa922b 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -2,32 +2,10 @@ from . import camera -from spyglass.server import StreamingHandler +from ..server import StreamingHandler from ..camera_options import process_controls class USB(camera.Camera): - def configure(self, - control_list: list[list[str]]=[], - upsidedown=False, - flip_horizontal=False, - flip_vertical=False): - controls = self.create_controls() - c = process_controls(self.picam2, [tuple(ctrl) for ctrl in control_list]) - controls.update(c) - - transform = libcamera.Transform( - hflip=int(flip_horizontal or upsidedown), - vflip=int(flip_vertical or upsidedown) - ) - - self.picam2.configure( - self.picam2.create_preview_configuration( - main={'size': (self.width, self.height), 'format': 'MJPEG'}, - controls=controls, - transform=transform - ) - ) - def start_and_run_server(self, bind_address, port, diff --git a/spyglass/cli.py b/spyglass/cli.py index 087ba45..551e59b 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -33,12 +33,7 @@ def main(args=None): parsed_args = get_args(args) - bind_address = parsed_args.bindaddress - port = parsed_args.port width, height = split_resolution(parsed_args.resolution) - stream_url = parsed_args.stream_url - snapshot_url = parsed_args.snapshot_url - orientation_exif = parsed_args.orientation_exif controls = parsed_args.controls if parsed_args.controls_string: controls += [c.split('=') for c in parsed_args.controls_string.split(',')] @@ -48,20 +43,25 @@ def main(args=None): cam = init_camera( parsed_args.camera_num, - width, - height, - parsed_args.fps, - parse_autofocus(parsed_args.autofocus), - parsed_args.lensposition, - parse_autofocus_speed(parsed_args.autofocusspeed), - parsed_args.upsidedown, - parsed_args.flip_horizontal, - parsed_args.flip_vertical, - parsed_args.controls, parsed_args.tuning_filter, parsed_args.tuning_filter_dir) + + cam.configure(width, + height, + parsed_args.fps, + parse_autofocus(parsed_args.autofocus), + parsed_args.lensposition, + parse_autofocus_speed(parsed_args.autofocusspeed), + parsed_args.controls, + parsed_args.upsidedown, + parsed_args.flip_horizontal, + parsed_args.flip_vertical,) try: - cam.start_and_run_server(bind_address, port, stream_url, snapshot_url, orientation_exif) + cam.start_and_run_server(parsed_args.bind_address, + parsed_args.port, + parsed_args.stream_url, + parsed_args.snapshot_url, + parsed_args.orientation_exif) finally: cam.stop() From aa9c5da4551aa8e06f11831e415a34f0b3f77880 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Thu, 4 Apr 2024 17:38:21 +0200 Subject: [PATCH 07/18] chore: rearrange cli arguments Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index 551e59b..36a60e3 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -175,11 +175,11 @@ def get_parser(): help='Define camera controls to start with spyglass. ' 'Input as a long string.\n' 'Format: = =') + parser.add_argument('--list-controls', action='store_true', help='List available camera controls and exits.') parser.add_argument('-tf', '--tuning_filter', type=str, default=None, nargs='?', const="", help='Set a tuning filter file name.') parser.add_argument('-tfd', '--tuning_filter_dir', type=str, default=None, nargs='?',const="", help='Set the directory to look for tuning filters.') - parser.add_argument('--list-controls', action='store_true', help='List available camera controls and exits.') parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') return parser From 6cacb33ab2c2240a396f9e98089aafe1889e637a Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 5 Apr 2024 20:08:36 +0200 Subject: [PATCH 08/18] fix: fix crash Signed-off-by: Patrick Gehrsitz --- spyglass/camera/__init__.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index c7a863d..a7dc2f7 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -6,12 +6,6 @@ def init_camera( camera_num: int, - width: int, - height: int, - fps: int, - autofocus: str, - lens_position: float, - autofocus_speed: str, tuning_filter=None, tuning_filter_dir=None ) -> Camera: @@ -25,7 +19,7 @@ def init_camera( picam2 = Picamera2(camera_num, tuning=tuning) if picam2._is_rpi_camera(): - cam = CSI(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) + cam = CSI(picam2) else: - cam = USB(picam2, width, height, fps, autofocus, lens_position, autofocus_speed) + cam = USB(picam2) return cam From b485dab63022d67c87b148965c02dedb98fa755d Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 5 Apr 2024 20:51:21 +0200 Subject: [PATCH 09/18] fix: fix typo Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index 36a60e3..533d092 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -57,7 +57,7 @@ def main(args=None): parsed_args.flip_horizontal, parsed_args.flip_vertical,) try: - cam.start_and_run_server(parsed_args.bind_address, + cam.start_and_run_server(parsed_args.bindaddress, parsed_args.port, parsed_args.stream_url, parsed_args.snapshot_url, From 1726a12747a526e183cee46258d429bbb22d0def Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Fri, 14 Jun 2024 21:29:02 +0200 Subject: [PATCH 10/18] docs: update README.md with new cli parameter Signed-off-by: Patrick Gehrsitz --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a88ed19..fcaafdb 100644 --- a/README.md +++ b/README.md @@ -57,11 +57,12 @@ On startup the following arguments are supported: | `--list-controls` | List all available libcamera controls onto the console. Those can be used with `--controls` | | | `-tf`, `--tuning_filter` | Set a tuning filter file name. | | | `-tfd`, `--tuning_filter_dir` | Set the directory to look for tuning filters. | | +| `-n`, `--camera_num` | Camera number to be used. All cameras with their number can be shown with `libcamera-hello`. | `0` | Starting the server without any argument is the same as ```shell -./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continuous -l 0.0 -s normal +./run.py -b 0.0.0.0 -p 8080 -r 640x480 -f 15 -st '/stream' -sn '/snapshot' -af continuous -l 0.0 -s normal -n 0 ``` The stream can then be accessed at `http://:8080/stream` From c4143eaa3f2f1f2c50e7cc7b77ea61d93e4c49d2 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 7 Jul 2024 18:57:14 +0200 Subject: [PATCH 11/18] test: fix tests with new methods Signed-off-by: Patrick Gehrsitz --- tests/test_cli.py | 365 ++++++++-------------------------------------- 1 file changed, 63 insertions(+), 302 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index a0a3a4f..5de2ac1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -19,6 +19,7 @@ DEFAULT_CONTROLS = [] DEFAULT_TUNING_FILTER = None DEFAULT_TUNING_FILTER_DIR = None +DEFAULT_CAMERA_NUM = 0 @pytest.fixture(autouse=True) @@ -76,49 +77,67 @@ def test_parse_tuning_filter_dir(): assert args.tuning_filter_dir == 'dir' -@patch("spyglass.server.run_server") @patch("spyglass.camera.init_camera") -def test_init_camera_with_defaults(mock_spyglass_camera, mock_spyglass_server): +def test_init_camera_with_defaults(mock_spyglass_camera,): from spyglass import cli import spyglass.camera cli.main(args=[]) spyglass.camera.init_camera.assert_called_once_with( + DEFAULT_CAMERA_NUM, + DEFAULT_TUNING_FILTER, + DEFAULT_TUNING_FILTER_DIR + ) + +@patch("spyglass.camera.camera.Camera.configure") +@patch("spyglass.camera.init_camera") +def test_configure_with_defaults(mock_init_camera, mock_configure): + from spyglass import cli + + cli.main(args=[]) + cam_instance = mock_init_camera.return_value + cam_instance.configure.assert_called_once_with( DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS, DEFAULT_AUTOFOCUS_MODE, DEFAULT_LENS_POSITION, DEFAULT_AF_SPEED, + DEFAULT_CONTROLS, DEFAULT_UPSIDE_DOWN, DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR + DEFAULT_FLIP_VERTICALLY ) - -@patch("spyglass.server.run_server") +@patch("spyglass.camera.camera.Camera.configure") @patch("spyglass.camera.init_camera") -def test_init_camera_resolution(mock_spyglass_server, mock_spyglass_camera): +def test_configure_with_parameters(mock_init_camera, mock_configure): from spyglass import cli - import spyglass.camera + cli.main(args=[ - '-r', '200x100' + '-n', '1', + '-tf', 'test', + '-tfd', 'test-dir', + '-r', '200x100', + '-f', '20', + '-af', 'manual', + '-l', '1.0', + '-s', 'normal', + '-ud', '-fh', '-fv', + '-c', 'brightness=-0.4', + '-c', 'awbenable=false', ]) - spyglass.camera.init_camera.assert_called_once_with( + cam_instance = mock_init_camera.return_value + cam_instance.configure.assert_called_once_with( 200, 100, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR + 20, + AF_MODE_ENUM_MANUAL, + 1.0, + AF_SPEED_ENUM_NORMAL, + [['brightness', '-0.4'],['awbenable', 'false']], + True, + True, + True ) @@ -138,251 +157,35 @@ def test_raise_error_when_height_greater_than_maximum(): ]) -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_fps(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-f', '20' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - 20, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_af_manual(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-af', 'manual' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - AF_MODE_ENUM_MANUAL, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") +@patch("spyglass.camera.camera.Camera.configure") @patch("spyglass.camera.init_camera") -def test_init_camera_af_continuous(mock_spyglass_server, mock_spyglass_camera): +def test_configure_camera_af_continuous_speed_fast(mock_init_camera, mock_configure): from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-af', 'continuous' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - AF_MODE_ENUM_CONTINUOUS, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_lens_position(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-l', '1.0' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - 1.0, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_af_speed_normal(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-s', 'normal' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - AF_SPEED_ENUM_NORMAL, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_af_speed_fast(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera cli.main(args=[ + '-af', 'continuous', '-s', 'fast' ]) - spyglass.camera.init_camera.assert_called_once_with( + cam_instance = mock_init_camera.return_value + cam_instance.configure.assert_called_once_with( DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, + AF_MODE_ENUM_CONTINUOUS, DEFAULT_LENS_POSITION, AF_SPEED_ENUM_FAST, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_upside_down(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-ud' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - True, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_flip_horizontal(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-fh' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - True, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_flip_vertical(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-fv' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, DEFAULT_UPSIDE_DOWN, DEFAULT_FLIP_HORIZONTALLY, - True, - DEFAULT_CONTROLS, - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR + DEFAULT_FLIP_VERTICALLY ) -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_controls(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-c', 'brightness=-0.4', - '-c', 'awbenable=false' - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - [['brightness', '-0.4'],['awbenable', 'false']], - DEFAULT_TUNING_FILTER, - DEFAULT_TUNING_FILTER_DIR - ) - -@patch("spyglass.server.run_server") +@patch("spyglass.camera.csi.CSI.start_and_run_server") @patch("spyglass.camera.init_camera") -def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock_spyglass_camera): +def test_run_server_with_configuration_from_arguments(mock_init_camera, mock_run_server): from spyglass import cli - import spyglass.server + cli.main(args=[ '-b', '1.2.3.4', '-p', '1234', @@ -390,10 +193,17 @@ def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock '-sn', 'snapshot-url', '-or', 'h' ]) - spyglass.server.run_server.assert_called_once_with('1.2.3.4', 1234, ANY, ANY, 'streaming-url', 'snapshot-url', 1) + cam_instance = mock_init_camera.return_value + cam_instance.start_and_run_server.assert_called_once_with( + '1.2.3.4', + 1234, + 'streaming-url', + 'snapshot-url', + 1 + ) -@patch("spyglass.server.run_server") +@patch("spyglass.camera.csi.CSI.start_and_run_server") @patch("spyglass.camera.init_camera") @pytest.mark.parametrize("input_value, expected_output", [ ('h', 1), @@ -405,7 +215,7 @@ def test_run_server_with_configuration_from_arguments(mock_spyglass_server, mock ('mhr90', 7), ('r270', 8), ]) -def test_run_server_with_orientation(mock_spyglass_server, mock_spyglass_camera, input_value, expected_output): +def test_run_server_with_orientation(mock_init_camera, mock_run_server, input_value, expected_output): from spyglass import cli import spyglass.server cli.main(args=[ @@ -415,60 +225,11 @@ def test_run_server_with_orientation(mock_spyglass_server, mock_spyglass_camera, '-sn', 'snapshot-url', '-or', input_value ]) - spyglass.server.run_server.assert_called_once_with( + cam_instance = mock_init_camera.return_value + cam_instance.start_and_run_server.assert_called_once_with( '1.2.3.4', 1234, - ANY, - ANY, 'streaming-url', 'snapshot-url', expected_output ) - - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_using_only_tuning_filter_file(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-tf', 'test', - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - "test", - DEFAULT_TUNING_FILTER_DIR - ) - -@patch("spyglass.server.run_server") -@patch("spyglass.camera.init_camera") -def test_init_camera_using_tuning_filters(mock_spyglass_server, mock_spyglass_camera): - from spyglass import cli - import spyglass.camera - cli.main(args=[ - '-tf', 'test', - '-tfd', 'test-dir', - ]) - spyglass.camera.init_camera.assert_called_once_with( - DEFAULT_WIDTH, - DEFAULT_HEIGHT, - DEFAULT_FPS, - DEFAULT_AUTOFOCUS_MODE, - DEFAULT_LENS_POSITION, - DEFAULT_AF_SPEED, - DEFAULT_UPSIDE_DOWN, - DEFAULT_FLIP_HORIZONTALLY, - DEFAULT_FLIP_VERTICALLY, - DEFAULT_CONTROLS, - "test", - "test-dir" - ) From 378c3433985d6150c6b39fd19bd5d6a7b4be274f Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Sun, 7 Jul 2024 18:58:17 +0200 Subject: [PATCH 12/18] chore: remove empty file Signed-off-by: Patrick Gehrsitz --- spyglass/camera.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 spyglass/camera.py diff --git a/spyglass/camera.py b/spyglass/camera.py deleted file mode 100644 index e69de29..0000000 From da012aa02ba3d9b50edb34cb09f08f568ce76bc5 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Mon, 5 Aug 2024 13:53:51 +0200 Subject: [PATCH 13/18] fix: add missing config file parameter Signed-off-by: Patrick Gehrsitz --- resources/spyglass.conf | 3 +++ scripts/spyglass | 1 + 2 files changed, 4 insertions(+) diff --git a/resources/spyglass.conf b/resources/spyglass.conf index 1072b56..8e2a7f2 100644 --- a/resources/spyglass.conf +++ b/resources/spyglass.conf @@ -9,6 +9,9 @@ #### NOTE: Values has to be surrounded by double quotes! ("value") #### NOTE: If commented out or includes typos it will use hardcoded defaults! +#### Libcamera camera to use (INTEGER)[default: 0] +CAMERA_NUM="0" + #### Running Spyglass with proxy or Standalone (BOOL)[default: true] NO_PROXY="true" diff --git a/scripts/spyglass b/scripts/spyglass index 46b6261..e41d155 100755 --- a/scripts/spyglass +++ b/scripts/spyglass @@ -94,6 +94,7 @@ run_spyglass() { fi "${PY_BIN}" "$(dirname "${BASE_SPY_PATH}")/run.py" \ + --camera_num "${CAMERA_NUM:-0}" \ --bindaddress "${bind_adress}" \ --port "${HTTP_PORT:-8080}" \ --resolution "${RESOLUTION:-640x480}" \ From 00442f691dc70b15fb451c6167cad9a072592309 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 13 Aug 2024 15:28:32 +0200 Subject: [PATCH 14/18] fix: fix logging Signed-off-by: Patrick Gehrsitz --- spyglass/camera/camera.py | 2 +- spyglass/cli.py | 7 +++---- spyglass/server.py | 7 ++++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index d090aa2..40e5b7a 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -22,7 +22,7 @@ def create_controls(self, fps: int, autofocus: str, lens_position: float, autofo if autofocus == libcamera.controls.AfModeEnum.Manual: controls['LensPosition'] = lens_position else: - print('Attached camera does not support autofocus') + logger.warning('Attached camera does not support autofocus') return controls diff --git a/spyglass/cli.py b/spyglass/cli.py index da360d4..5dc1a85 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -2,14 +2,13 @@ Parse command line arguments in, invoke server. """ + import argparse -import logging import re import sys - import libcamera -from . import camera_options +from . import camera_options, logger from .exif import option_to_exif_orientation from .__version__ import __version__ from .camera import init_camera @@ -26,7 +25,7 @@ def main(args=None): becomes sys.exit(main()). The __main__ entry point similarly wraps sys.exit(). """ - logging.info(f"Spyglass {__version__}") + logger.info(f"Spyglass {__version__}") if args is None: args = sys.argv[1:] diff --git a/spyglass/server.py b/spyglass/server.py index c1f46ed..da38930 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -1,7 +1,8 @@ #!/usr/bin/python3 -import logging import socketserver + +from . import logger from http import server import io import logging @@ -66,7 +67,7 @@ def start_streaming(self): self.wfile.write(frame[2:]) self.wfile.write(b'\r\n') except Exception as e: - logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) + logger.warning('Removed streaming client %s: %s', self.client_address, str(e)) def send_snapshot(self): try: @@ -83,7 +84,7 @@ def send_snapshot(self): self.wfile.write(self.exif_header) self.wfile.write(frame[2:]) except Exception as e: - logging.warning( + logger.warning( 'Removed client %s: %s', self.client_address, str(e)) From cbd0d06eab6cc44a68baf3a20718dfccae1d657d Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 13 Aug 2024 15:29:37 +0200 Subject: [PATCH 15/18] feat: add camera-num support for list-controls Signed-off-by: Patrick Gehrsitz --- spyglass/camera_options.py | 6 ++++-- spyglass/cli.py | 13 +++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/spyglass/camera_options.py b/spyglass/camera_options.py index d6fd3d8..a2bc95a 100644 --- a/spyglass/camera_options.py +++ b/spyglass/camera_options.py @@ -80,10 +80,12 @@ def parse_from_string(input_string: str) -> any: def get_type_str(obj) -> str: return str(type(obj)).split('\'')[1] -def get_libcamera_controls_string(camera_path: str) -> str: +def get_libcamera_controls_string(camera_num: str) -> str: ctrls_str = "" libcam_cm = libcamera.CameraManager.singleton() - cam = libcam_cm.cameras[0] + if camera_num > len(libcam_cm.cameras) - 1: + return ctrls_str + cam = libcam_cm.cameras[camera_num] def rectangle_to_tuple(rectangle): return (rectangle.x, rectangle.y, rectangle.width, rectangle.height) diff --git a/spyglass/cli.py b/spyglass/cli.py index 5dc1a85..fc186e6 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -32,13 +32,18 @@ def main(args=None): parsed_args = get_args(args) + if parsed_args.list_controls: + controls_str = camera_options.get_libcamera_controls_string(parsed_args.camera_num) + if not controls_str: + logger.warning(f"Camera {parsed_args.camera_num} not found") + else: + logger.info('Available controls:\n'+controls_str) + return + width, height = split_resolution(parsed_args.resolution) controls = parsed_args.controls if parsed_args.controls_string: controls += [c.split('=') for c in parsed_args.controls_string.split(',')] - if parsed_args.list_controls: - print('Available controls:\n'+camera_options.get_libcamera_controls_string(0)) - return cam = init_camera( parsed_args.camera_num, @@ -176,7 +181,7 @@ def get_parser(): parser.add_argument('-tfd', '--tuning_filter_dir', type=str, default=None, nargs='?',const="", help='Set the directory to look for tuning filters.') parser.add_argument('--list-controls', action='store_true', help='List available camera controls and exits.') - parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used') + parser.add_argument('-n', '--camera_num', type=int, default=0, help='Camera number to be used (Works with --list-controls)') return parser # endregion cli args From dc2d50f78091efad94d4a93b846e4e8466ed7748 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 13 Aug 2024 15:39:48 +0200 Subject: [PATCH 16/18] fix: change list-controls output to normal printing Signed-off-by: Patrick Gehrsitz --- spyglass/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spyglass/cli.py b/spyglass/cli.py index fc186e6..4a1c295 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -35,9 +35,9 @@ def main(args=None): if parsed_args.list_controls: controls_str = camera_options.get_libcamera_controls_string(parsed_args.camera_num) if not controls_str: - logger.warning(f"Camera {parsed_args.camera_num} not found") + print(f"Camera {parsed_args.camera_num} not found") else: - logger.info('Available controls:\n'+controls_str) + print('Available controls:\n'+controls_str) return width, height = split_resolution(parsed_args.resolution) From 2610fc6d72ede2e54ad1de2fbe719116bc78d3a2 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 13 Aug 2024 15:44:06 +0200 Subject: [PATCH 17/18] chore: change imports to module imports Signed-off-by: Patrick Gehrsitz --- spyglass/camera/__init__.py | 8 ++++---- spyglass/camera/camera.py | 12 +++++++----- spyglass/camera/csi.py | 7 +++---- spyglass/camera/usb.py | 8 ++------ spyglass/cli.py | 8 ++++---- spyglass/server.py | 8 ++------ 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/spyglass/camera/__init__.py b/spyglass/camera/__init__.py index a7dc2f7..82ec6cf 100644 --- a/spyglass/camera/__init__.py +++ b/spyglass/camera/__init__.py @@ -1,9 +1,9 @@ -from .camera import Camera -from .csi import CSI -from .usb import USB - from picamera2 import Picamera2 +from spyglass.camera.camera import Camera +from spyglass.camera.csi import CSI +from spyglass.camera.usb import USB + def init_camera( camera_num: int, tuning_filter=None, diff --git a/spyglass/camera/camera.py b/spyglass/camera/camera.py index 40e5b7a..77993b1 100644 --- a/spyglass/camera/camera.py +++ b/spyglass/camera/camera.py @@ -1,10 +1,12 @@ +import libcamera + from abc import ABC, abstractmethod from picamera2 import Picamera2 -import libcamera -from .. import logger -from ..exif import create_exif_header -from ..camera_options import process_controls -from ..server import StreamingServer, StreamingHandler + +from spyglass import logger +from spyglass.exif import create_exif_header +from spyglass.camera_options import process_controls +from spyglass.server import StreamingServer, StreamingHandler class Camera(ABC): def __init__(self, picam2: Picamera2): diff --git a/spyglass/camera/csi.py b/spyglass/camera/csi.py index 375253b..3bec8e8 100644 --- a/spyglass/camera/csi.py +++ b/spyglass/camera/csi.py @@ -1,12 +1,11 @@ import io -import libcamera from picamera2.encoders import MJPEGEncoder from picamera2.outputs import FileOutput from threading import Condition -from . import camera -from ..server import StreamingHandler +from spyglass import camera +from spyglass.server import StreamingHandler class CSI(camera.Camera): def start_and_run_server(self, @@ -26,7 +25,7 @@ def write(self, buf): self.frame = buf self.condition.notify_all() output = StreamingOutput() - def get_frame(self): + def get_frame(inner_self): with output.condition: output.condition.wait() return output.frame diff --git a/spyglass/camera/usb.py b/spyglass/camera/usb.py index 0aa922b..b26adee 100644 --- a/spyglass/camera/usb.py +++ b/spyglass/camera/usb.py @@ -1,9 +1,5 @@ -import libcamera - -from . import camera - -from ..server import StreamingHandler -from ..camera_options import process_controls +from spyglass import camera +from spyglass.server import StreamingHandler class USB(camera.Camera): def start_and_run_server(self, diff --git a/spyglass/cli.py b/spyglass/cli.py index 4a1c295..6370241 100644 --- a/spyglass/cli.py +++ b/spyglass/cli.py @@ -8,10 +8,10 @@ import sys import libcamera -from . import camera_options, logger -from .exif import option_to_exif_orientation -from .__version__ import __version__ -from .camera import init_camera +from spyglass import camera_options, logger +from spyglass.exif import option_to_exif_orientation +from spyglass.__version__ import __version__ +from spyglass.camera import init_camera MAX_WIDTH = 1920 diff --git a/spyglass/server.py b/spyglass/server.py index da38930..4b2cb79 100755 --- a/spyglass/server.py +++ b/spyglass/server.py @@ -2,13 +2,9 @@ import socketserver -from . import logger from http import server -import io -import logging -import socketserver -from http import server -from threading import Condition + +from spyglass import logger from spyglass.url_parsing import check_urls_match, get_url_params from spyglass.exif import create_exif_header from spyglass.camera_options import parse_dictionary_to_html_page, process_controls From f30f98c03ff07a0aeb8397eaad6276ba7d3f1185 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz Date: Tue, 13 Aug 2024 21:10:05 +0200 Subject: [PATCH 18/18] chore: update .gitignore Signed-off-by: Patrick Gehrsitz --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 5408877..faf5477 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ cover/ .idea/ venv +.venv +.vscode