From 2f52d92829a1fec89e14ec7f2bc8af5a515f17d9 Mon Sep 17 00:00:00 2001 From: leadnaut Date: Fri, 13 Sep 2024 10:00:34 +1000 Subject: [PATCH 1/2] rasp camera --- client/drivers/camera_overlord.py | 28 +++++++++++++++++++ .../models/pose_detection/frame_capturer.py | 18 ++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 client/drivers/camera_overlord.py diff --git a/client/drivers/camera_overlord.py b/client/drivers/camera_overlord.py new file mode 100644 index 0000000..1fe78c7 --- /dev/null +++ b/client/drivers/camera_overlord.py @@ -0,0 +1,28 @@ +# This script periodically takes photos and saves them to +# a temporary file on ram. This allows multiple programs to +# access the camera feed at once. + +from picamera2 import Picamera2 +import os +import time + +picam2 = Picamera2() +picam2.start() +picam2.options['quality'] = 80 + +try: + f = open('/tmp/snapshot.jpg','x') + f.close() +except: + print('Snapshot already exists') + +try: + while True: + picam2.capture_file("/tmp/snapshot2.jpg") + os.replace("/tmp/snapshot2.jpg", "/tmp/snapshot.jpg") + time.sleep(0.5) + +except KeyboardInterrupt: + picam2.close() + print('Closed nicely') + quit() diff --git a/client/models/pose_detection/frame_capturer.py b/client/models/pose_detection/frame_capturer.py index f58d029..f2b64d3 100644 --- a/client/models/pose_detection/frame_capturer.py +++ b/client/models/pose_detection/frame_capturer.py @@ -1,7 +1,9 @@ from abc import ABC, abstractmethod import numpy as np +import time import cv2 +import os class FrameCapturer(ABC): @@ -30,3 +32,19 @@ def get_frame(self) -> tuple[np.ndarray, int]: def release(self) -> None: """Release the camera.""" self._cam.release() + + +class RaspCapturer(FrameCapturer): + """FrameCapturer using a temp file to read from the camera. + File is created using client/drivers/camera_overlord.py""" + def get_frame(self) -> tuple[np.ndarray, int]: + tries = 0 + while True: + array = cv2.imread('/tmp/snapshot.jpg') + if array is None: + tries += 1 + if tries > 5: raise FileNotFoundError('No snapshot found') + time.sleep(0.05) + else: + finfo = os.stat('/tmp/snapshot.jpg') + return (array, finfo.st_mtime) \ No newline at end of file From 0c91f1dfcc32ff77fcbe1e13a23eb4a79bcc7110 Mon Sep 17 00:00:00 2001 From: leadnaut Date: Fri, 13 Sep 2024 10:02:51 +1000 Subject: [PATCH 2/2] fix tries counter --- client/models/pose_detection/frame_capturer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/client/models/pose_detection/frame_capturer.py b/client/models/pose_detection/frame_capturer.py index f2b64d3..f32fd35 100644 --- a/client/models/pose_detection/frame_capturer.py +++ b/client/models/pose_detection/frame_capturer.py @@ -46,5 +46,6 @@ def get_frame(self) -> tuple[np.ndarray, int]: if tries > 5: raise FileNotFoundError('No snapshot found') time.sleep(0.05) else: + tries = 0 finfo = os.stat('/tmp/snapshot.jpg') return (array, finfo.st_mtime) \ No newline at end of file