Skip to content

camera

CameraHandler #

Camera handler.

Handle camera initialization, sample detection.

Source code in cogip/tools/beaconcam/camera.py
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
class CameraHandler:
    """
    Camera handler.

    Handle camera initialization, sample detection.
    """

    _camera_device: Path = None  # Camera device
    _camera_codec: VideoCodec = None  # Video codec
    _camera_frame_width: int = None  # Camera frame width
    _camera_frame_height: int = None  # Camera frame height
    _camera_capture: cv2.VideoCapture = None  # OpenCV video capture
    _last_frame: SharedMemory = None  # Last generated frame to stream on web server
    _frame_rate: float = 6  # Number of images processed by seconds
    _exiting: bool = False  # Exit requested if True

    def __init__(self):
        """
        Class constructor.

        Create SocketIO client and connect to server.
        """
        self.settings = Settings()
        signal.signal(signal.SIGTERM, self.exit_handler)

        self.record_filename: Path | None = None
        self.record_writer: cv2.VideoWriter | None = None

        self.sio = socketio.Client(logger=False, engineio_logger=False)
        self.register_sio_events()
        Thread(
            target=lambda: polling2.poll(
                self.sio_connect,
                step=1,
                ignore_exceptions=(socketio.exceptions.ConnectionError),
                poll_forever=True,
            )
        ).start()

    @staticmethod
    def exit_handler(signum, frame):
        """
        Function called when TERM signal is received.
        """
        CameraHandler._exiting = True
        raise ExitSignal()

    def sio_connect(self) -> bool:
        """
        Connect to SocketIO server.
        Returning True stops polling for connection to succeed.
        """
        if self._exiting:
            return True

        self.sio.connect(str(self.settings.socketio_server_url), namespaces=["/beaconcam"])
        return True

    def open_camera(self):
        """
        Initialize camera and aruco markers detection parameters.
        """
        self._camera_capture = cv2.VideoCapture(str(self.settings.camera_device), cv2.CAP_V4L2)
        if not self._camera_capture.isOpened():
            logger.error(f"Camera handler: Cannot open camera device {self.settings.camera_device}")
            self._camera_capture.release()
            self._camera_capture = None
            return

        fourcc = cv2.VideoWriter_fourcc(*self.settings.camera_codec.value)
        ret = self._camera_capture.set(cv2.CAP_PROP_FOURCC, fourcc)
        if not ret:
            logger.warning(f"Video codec {self.settings.camera_codec} not supported")
            self.settings.camera_codec = None

        ret = self._camera_capture.set(cv2.CAP_PROP_FRAME_WIDTH, self.settings.camera_width)
        if not ret:
            logger.warning(f"Frame width {self.settings.camera_width} not supported")
            self.settings.camera_width = None

        ret = self._camera_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self.settings.camera_height)
        if not ret:
            logger.warning(f"Frame height {self.settings.camera_height} not supported")
            self.settings.camera_height = None

    def close_camera(self) -> None:
        """
        Release camera device.
        """
        if self._camera_capture:
            try:
                self._camera_capture.release()
                logger.info("Camera handler: Camera closed.")
            except Exception as exc:  # noqa
                logger.info("Camera handler: Failed to release camera: {exc}")

        self._camera_capture = None

    def open_last_frame(self, size: int) -> None:
        """
        Open the shared memory used to exchange last frame with the server.

        Arguments:
            size: Size of the shared memory
        """
        if not self._last_frame:
            try:
                self._last_frame = SharedMemory(name="last_frame", create=True, size=size)
                logger.info("Camera handler: shared memory for last_frame created.")
            except FileExistsError as exc:
                logger.warning(f"Camera handler: Failed to create shared memory for last_frame: {exc}")
                self._last_frame = None

    def close_last_frame(self) -> None:
        """
        Close last frame shared memory.
        """
        if self._last_frame:
            try:
                self._last_frame.close()
                self._last_frame.unlink()
                logger.info("Camera handler: Shared memory for last frame closed.")
            except Exception as exc:
                logger.info(f"Camera handler: Failed to close shared memory for last frame: {exc}")

        self._last_frame = None

    def camera_handler(self) -> None:
        """
        Read and process frames from camera.
        """
        interval = 1.0 / self._frame_rate

        try:
            while not self._exiting:
                start = time.time()

                if not self._camera_capture:
                    self.open_camera()

                if not self._camera_capture:
                    logger.warning("Camera handler: Failed to open camera, retry in 1s.")
                    sleep(1)
                    continue

                try:
                    self.process_image()
                except ExitSignal:
                    break
                except Exception as exc:
                    logger.warning(f"Unknown exception: {exc}")
                    self.close_camera()
                    sleep(1)
                    continue

                now = time.time()
                duration = now - start
                if duration > interval:
                    logger.warning(f"Function too long: {duration} > {interval}")
                else:
                    wait = interval - duration
                    time.sleep(wait)

        except (KeyboardInterrupt, ExitSignal):
            pass

        logger.info("Camera handler: Exiting.")

        self.close_last_frame()
        self.close_camera()
        if self.sio.connected:
            self.sio.disconnect()

    def process_image(self) -> None:
        """
        Read one frame from camera, process it, send samples to cogip-server
        and generate image to stream.
        """
        image_color: np.ndarray
        ret, image_color = self._camera_capture.read()
        if not ret:
            raise Exception("Camera handler: Cannot read frame.")

        image_stream: np.ndarray = image_color

        # Encode the frame in BMP format (larger but faster than JPEG)
        encoded_image: np.ndarray
        ret, encoded_image = cv2.imencode(".bmp", image_stream)

        if not ret:
            raise Exception("Can't encode frame.")

        frame = encoded_image.tobytes()
        self.open_last_frame(len(frame))

        if self._last_frame:
            self._last_frame.buf[0 : len(frame)] = frame

        if self.record_writer:
            self.record_writer.write(image_stream)

    def start_video_record(self):
        if self.record_writer:
            self.stop_video_record()
        records_dir = Path.home() / "records"
        records_dir.mkdir(exist_ok=True)
        # Keep only 20 last records
        for old_record in sorted(records_dir.glob("*.mp4"))[:-20]:
            old_record.unlink()
        timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
        self.record_filename = records_dir / f"beacon_{timestamp}.mp4"

        logger.info(f"Start recording video in {self.record_filename}")
        self.record_writer = cv2.VideoWriter(
            str(self.record_filename),
            cv2.VideoWriter_fourcc(*"mp4v"),
            self._frame_rate,
            (self.settings.camera_width, self.settings.camera_height),
        )

    def stop_video_record(self):
        if self.record_writer:
            logger.info("Stop recording video")
            self.record_writer.release()
            self.record_filename = None
            self.record_writer = None

    def register_sio_events(self) -> None:
        @self.sio.event(namespace="/beaconcam")
        def connect():
            """
            Callback on server connection.
            """
            polling2.poll(lambda: self.sio.connected is True, step=0.2, poll_forever=True)
            logger.info("Camera handler: connected to server")
            self.sio.emit("connected", namespace="/beaconcam")

        @self.sio.event(namespace="/beaconcam")
        def connect_error(data):
            """
            Callback on server connection error.
            """
            logger.info("Camera handler: connection to server failed.")

        @self.sio.event(namespace="/beaconcam")
        def disconnect():
            """
            Callback on server disconnection.
            """
            logger.info("Camera handler: disconnected from server")

        @self.sio.on("start_video_record", namespace="/beaconcam")
        def start_video_record():
            self.start_video_record()

        @self.sio.on("stop_video_record", namespace="/beaconcam")
        def stop_video_record():
            self.stop_video_record()

__init__() #

Class constructor.

Create SocketIO client and connect to server.

Source code in cogip/tools/beaconcam/camera.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def __init__(self):
    """
    Class constructor.

    Create SocketIO client and connect to server.
    """
    self.settings = Settings()
    signal.signal(signal.SIGTERM, self.exit_handler)

    self.record_filename: Path | None = None
    self.record_writer: cv2.VideoWriter | None = None

    self.sio = socketio.Client(logger=False, engineio_logger=False)
    self.register_sio_events()
    Thread(
        target=lambda: polling2.poll(
            self.sio_connect,
            step=1,
            ignore_exceptions=(socketio.exceptions.ConnectionError),
            poll_forever=True,
        )
    ).start()

camera_handler() #

Read and process frames from camera.

Source code in cogip/tools/beaconcam/camera.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def camera_handler(self) -> None:
    """
    Read and process frames from camera.
    """
    interval = 1.0 / self._frame_rate

    try:
        while not self._exiting:
            start = time.time()

            if not self._camera_capture:
                self.open_camera()

            if not self._camera_capture:
                logger.warning("Camera handler: Failed to open camera, retry in 1s.")
                sleep(1)
                continue

            try:
                self.process_image()
            except ExitSignal:
                break
            except Exception as exc:
                logger.warning(f"Unknown exception: {exc}")
                self.close_camera()
                sleep(1)
                continue

            now = time.time()
            duration = now - start
            if duration > interval:
                logger.warning(f"Function too long: {duration} > {interval}")
            else:
                wait = interval - duration
                time.sleep(wait)

    except (KeyboardInterrupt, ExitSignal):
        pass

    logger.info("Camera handler: Exiting.")

    self.close_last_frame()
    self.close_camera()
    if self.sio.connected:
        self.sio.disconnect()

close_camera() #

Release camera device.

Source code in cogip/tools/beaconcam/camera.py
108
109
110
111
112
113
114
115
116
117
118
119
def close_camera(self) -> None:
    """
    Release camera device.
    """
    if self._camera_capture:
        try:
            self._camera_capture.release()
            logger.info("Camera handler: Camera closed.")
        except Exception as exc:  # noqa
            logger.info("Camera handler: Failed to release camera: {exc}")

    self._camera_capture = None

close_last_frame() #

Close last frame shared memory.

Source code in cogip/tools/beaconcam/camera.py
136
137
138
139
140
141
142
143
144
145
146
147
148
def close_last_frame(self) -> None:
    """
    Close last frame shared memory.
    """
    if self._last_frame:
        try:
            self._last_frame.close()
            self._last_frame.unlink()
            logger.info("Camera handler: Shared memory for last frame closed.")
        except Exception as exc:
            logger.info(f"Camera handler: Failed to close shared memory for last frame: {exc}")

    self._last_frame = None

exit_handler(signum, frame) staticmethod #

Function called when TERM signal is received.

Source code in cogip/tools/beaconcam/camera.py
62
63
64
65
66
67
68
@staticmethod
def exit_handler(signum, frame):
    """
    Function called when TERM signal is received.
    """
    CameraHandler._exiting = True
    raise ExitSignal()

open_camera() #

Initialize camera and aruco markers detection parameters.

Source code in cogip/tools/beaconcam/camera.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def open_camera(self):
    """
    Initialize camera and aruco markers detection parameters.
    """
    self._camera_capture = cv2.VideoCapture(str(self.settings.camera_device), cv2.CAP_V4L2)
    if not self._camera_capture.isOpened():
        logger.error(f"Camera handler: Cannot open camera device {self.settings.camera_device}")
        self._camera_capture.release()
        self._camera_capture = None
        return

    fourcc = cv2.VideoWriter_fourcc(*self.settings.camera_codec.value)
    ret = self._camera_capture.set(cv2.CAP_PROP_FOURCC, fourcc)
    if not ret:
        logger.warning(f"Video codec {self.settings.camera_codec} not supported")
        self.settings.camera_codec = None

    ret = self._camera_capture.set(cv2.CAP_PROP_FRAME_WIDTH, self.settings.camera_width)
    if not ret:
        logger.warning(f"Frame width {self.settings.camera_width} not supported")
        self.settings.camera_width = None

    ret = self._camera_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self.settings.camera_height)
    if not ret:
        logger.warning(f"Frame height {self.settings.camera_height} not supported")
        self.settings.camera_height = None

open_last_frame(size) #

Open the shared memory used to exchange last frame with the server.

Parameters:

Name Type Description Default
size int

Size of the shared memory

required
Source code in cogip/tools/beaconcam/camera.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def open_last_frame(self, size: int) -> None:
    """
    Open the shared memory used to exchange last frame with the server.

    Arguments:
        size: Size of the shared memory
    """
    if not self._last_frame:
        try:
            self._last_frame = SharedMemory(name="last_frame", create=True, size=size)
            logger.info("Camera handler: shared memory for last_frame created.")
        except FileExistsError as exc:
            logger.warning(f"Camera handler: Failed to create shared memory for last_frame: {exc}")
            self._last_frame = None

process_image() #

Read one frame from camera, process it, send samples to cogip-server and generate image to stream.

Source code in cogip/tools/beaconcam/camera.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def process_image(self) -> None:
    """
    Read one frame from camera, process it, send samples to cogip-server
    and generate image to stream.
    """
    image_color: np.ndarray
    ret, image_color = self._camera_capture.read()
    if not ret:
        raise Exception("Camera handler: Cannot read frame.")

    image_stream: np.ndarray = image_color

    # Encode the frame in BMP format (larger but faster than JPEG)
    encoded_image: np.ndarray
    ret, encoded_image = cv2.imencode(".bmp", image_stream)

    if not ret:
        raise Exception("Can't encode frame.")

    frame = encoded_image.tobytes()
    self.open_last_frame(len(frame))

    if self._last_frame:
        self._last_frame.buf[0 : len(frame)] = frame

    if self.record_writer:
        self.record_writer.write(image_stream)

sio_connect() #

Connect to SocketIO server. Returning True stops polling for connection to succeed.

Source code in cogip/tools/beaconcam/camera.py
70
71
72
73
74
75
76
77
78
79
def sio_connect(self) -> bool:
    """
    Connect to SocketIO server.
    Returning True stops polling for connection to succeed.
    """
    if self._exiting:
        return True

    self.sio.connect(str(self.settings.socketio_server_url), namespaces=["/beaconcam"])
    return True