|
| 1 | +# Forked and modified from @enteropositivo |
| 2 | +# https://github.com/enteropositivo/seedsigner-emulator/blob/master/seedsigner/hardware/camera.py |
| 3 | +# https://github.com/enteropositivo/seedsigner-emulator/blob/master/seedsigner/emulator/webcamvideostream.py |
| 4 | + |
| 5 | +from threading import Thread |
| 6 | +import time |
| 7 | +import cv2 |
| 8 | +import numpy as np |
| 9 | +from PIL import Image |
| 10 | + |
| 11 | +from seedsigner.models.settings import Settings, SettingsConstants |
| 12 | +from seedsigner.models.singleton import Singleton |
| 13 | + |
| 14 | + |
| 15 | +class MockCamera(Singleton): |
| 16 | + _video_stream = None |
| 17 | + _picamera = None |
| 18 | + _camera_rotation = None |
| 19 | + |
| 20 | + @classmethod |
| 21 | + def get_instance(cls): |
| 22 | + # This is the only way to access the one and only Controller |
| 23 | + print("INSTANCE") |
| 24 | + if cls._instance is None: |
| 25 | + cls._instance = cls.__new__(cls) |
| 26 | + cls._instance._camera_rotation = int( |
| 27 | + Settings.get_instance().get_value( |
| 28 | + SettingsConstants.SETTING__CAMERA_ROTATION |
| 29 | + ) |
| 30 | + ) |
| 31 | + cls._instance._camera_rotation += 90 |
| 32 | + return cls._instance |
| 33 | + |
| 34 | + def start_video_stream_mode( |
| 35 | + self, resolution=(512, 384), framerate=12, format="bgr" |
| 36 | + ): |
| 37 | + if self._video_stream is not None: |
| 38 | + self.stop_video_stream_mode() |
| 39 | + |
| 40 | + try: |
| 41 | + self._video_stream = WebcamVideoStream( |
| 42 | + resolution=resolution, framerate=framerate, format=format |
| 43 | + ) |
| 44 | + self._video_stream.start() |
| 45 | + except Exception as e: |
| 46 | + raise e |
| 47 | + |
| 48 | + def read_video_stream(self, as_image=False): |
| 49 | + if not self._video_stream: |
| 50 | + raise Exception("Must call start_video_stream first.") |
| 51 | + frame = self._video_stream.read() |
| 52 | + if not as_image: |
| 53 | + return frame |
| 54 | + else: |
| 55 | + if frame is not None: |
| 56 | + return Image.fromarray(frame.astype("uint8"), "RGB").rotate( |
| 57 | + 90 + self._camera_rotation |
| 58 | + ) |
| 59 | + return None |
| 60 | + |
| 61 | + def stop_video_stream_mode(self): |
| 62 | + if self._video_stream is not None: |
| 63 | + self._video_stream.stop() |
| 64 | + self._video_stream = None |
| 65 | + |
| 66 | + def start_single_frame_mode(self, resolution=(720, 480)): |
| 67 | + if self._video_stream is not None: |
| 68 | + self.stop_video_stream_mode() |
| 69 | + if self._picamera is not None: |
| 70 | + self._picamera.close() |
| 71 | + |
| 72 | + def capture_frame(self): |
| 73 | + print("CAPTURE") |
| 74 | + frame = WebcamVideoStream.single_frame() |
| 75 | + return Image.fromarray(frame).rotate(90 + self._camera_rotation) |
| 76 | + |
| 77 | + def stop_single_frame_mode(self): |
| 78 | + if self._picamera is not None: |
| 79 | + self._picamera.close() |
| 80 | + self._picamera = None |
| 81 | + |
| 82 | + |
| 83 | +def resize_and_center_crop_240(frame_bgr: np.ndarray) -> np.ndarray: |
| 84 | + target = 240 |
| 85 | + h, w = frame_bgr.shape[:2] |
| 86 | + |
| 87 | + # resize to 240 |
| 88 | + if w < h: |
| 89 | + scale = target / w |
| 90 | + else: |
| 91 | + scale = target / h |
| 92 | + |
| 93 | + new_w = int(w * scale) |
| 94 | + new_h = int(h * scale) |
| 95 | + |
| 96 | + resized = cv2.resize(frame_bgr, (new_w, new_h), interpolation=cv2.INTER_AREA) |
| 97 | + |
| 98 | + # center crop |
| 99 | + x1 = (new_w - target) // 2 |
| 100 | + y1 = (new_h - target) // 2 |
| 101 | + |
| 102 | + cropped = resized[y1 : y1 + target, x1 : x1 + target] |
| 103 | + |
| 104 | + return cropped |
| 105 | + |
| 106 | + |
| 107 | +class WebcamVideoStream: |
| 108 | + def __init__(self, resolution=(320, 240), framerate=32, format="bgr", **kwargs): |
| 109 | + # initialize the camera |
| 110 | + self.camera = cv2.VideoCapture(0) |
| 111 | + self.set_resolution(resolution) |
| 112 | + |
| 113 | + # initialize the frame and the variable used to indicate |
| 114 | + # if the thread should be stopped |
| 115 | + self.frame = None |
| 116 | + self.should_stop = False |
| 117 | + self.is_stopped = True |
| 118 | + |
| 119 | + def start(self): |
| 120 | + # start the thread to read frames from the video stream |
| 121 | + t = Thread(target=self.update, args=()) |
| 122 | + t.daemon = True |
| 123 | + t.start() |
| 124 | + self.is_stopped = False |
| 125 | + return self |
| 126 | + |
| 127 | + def update(self): |
| 128 | + if self.camera.isOpened(): |
| 129 | + # keep looping infinitely until the thread is stopped |
| 130 | + while not self.should_stop: |
| 131 | + # grab the frame from the stream and clear the stream in |
| 132 | + # preparation for the next frame |
| 133 | + ret, stream = self.camera.read() |
| 134 | + stream = cv2.resize(stream, (240, 240)) |
| 135 | + stream = cv2.cvtColor(stream, cv2.COLOR_BGR2RGB) |
| 136 | + time.sleep(0.05) |
| 137 | + self.frame = stream |
| 138 | + self.is_stopped = True |
| 139 | + self.should_stop = False |
| 140 | + |
| 141 | + def read(self): |
| 142 | + return self.frame |
| 143 | + |
| 144 | + def stop(self): |
| 145 | + # indicate that the thread should be stopped |
| 146 | + self.should_stop = True |
| 147 | + |
| 148 | + # Block in this thread until stopped |
| 149 | + while not self.is_stopped: |
| 150 | + pass |
| 151 | + |
| 152 | + def set_resolution(self, resolution): |
| 153 | + self.camera.set(3, resolution[0]) |
| 154 | + self.camera.set(4, resolution[1]) |
| 155 | + |
| 156 | + @staticmethod |
| 157 | + def single_frame(): |
| 158 | + cap = cv2.VideoCapture(0) |
| 159 | + |
| 160 | + # Warm-up, let auto-exposure settle in |
| 161 | + for _ in range(30): |
| 162 | + cap.read() |
| 163 | + time.sleep(0.01) |
| 164 | + |
| 165 | + ret, frame = cap.read() |
| 166 | + cap.release() |
| 167 | + |
| 168 | + if not ret: |
| 169 | + raise RuntimeError("Camera capture failed") |
| 170 | + |
| 171 | + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 172 | + frame = resize_and_center_crop_240(frame) |
| 173 | + return frame |
0 commit comments