chromapi 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chromapi/__init__.py +2 -0
- chromapi/__main__.py +6 -0
- chromapi/assets/audio/alert.wav +0 -0
- chromapi/assets/audio/chromapi_scream.wav +0 -0
- chromapi/assets/audio/confused.wav +0 -0
- chromapi/assets/audio/dizzy.wav +0 -0
- chromapi/assets/audio/excited.wav +0 -0
- chromapi/assets/audio/greeting.wav +0 -0
- chromapi/assets/audio/happy.wav +0 -0
- chromapi/assets/audio/no.wav +0 -0
- chromapi/assets/audio/purring.wav +0 -0
- chromapi/assets/audio/sad.wav +0 -0
- chromapi/assets/audio/scared.wav +0 -0
- chromapi/assets/audio/sleepy.wav +0 -0
- chromapi/assets/audio/squeak.wav +0 -0
- chromapi/assets/audio/surprised.wav +0 -0
- chromapi/assets/audio/thinking.wav +0 -0
- chromapi/assets/audio/upset.wav +0 -0
- chromapi/assets/audio/yes.wav +0 -0
- chromapi/assets/favicon.ico +0 -0
- chromapi/assets/firmware/chromapi_stm32_core_v2.0.0.hex +2222 -0
- chromapi/assets/firmware/flash.sh +88 -0
- chromapi/assets/moves/README.md +48 -0
- chromapi/assets/moves/angry.json +1 -0
- chromapi/assets/moves/confused.json +1 -0
- chromapi/assets/moves/dizzy.json +1 -0
- chromapi/assets/moves/excited.json +1 -0
- chromapi/assets/moves/greeting.json +1 -0
- chromapi/assets/moves/happy.json +1 -0
- chromapi/assets/moves/joy.json +1 -0
- chromapi/assets/moves/kick.json +1 -0
- chromapi/assets/moves/metadata.jsonl +15 -0
- chromapi/assets/moves/no.json +1 -0
- chromapi/assets/moves/purring.json +1 -0
- chromapi/assets/moves/sad.json +1 -0
- chromapi/assets/moves/scared.json +1 -0
- chromapi/assets/moves/surprised.json +1 -0
- chromapi/assets/moves/tired.json +1 -0
- chromapi/assets/moves/yes.json +1 -0
- chromapi/chromapi.py +1460 -0
- chromapi/cli.py +106 -0
- chromapi/config/config.yaml +118 -0
- chromapi/estimation/__init__.py +6 -0
- chromapi/estimation/attitude.py +107 -0
- chromapi/hardware/__init__.py +2 -0
- chromapi/hardware/battery.py +54 -0
- chromapi/hardware/fan.py +165 -0
- chromapi/hardware/motherboard_bridge.py +345 -0
- chromapi/input/__init__.py +2 -0
- chromapi/input/gamepad.py +379 -0
- chromapi/input/pairing.py +98 -0
- chromapi/kinematics/__init__.py +10 -0
- chromapi/kinematics/kromatics.py +324 -0
- chromapi/kinematics/poses.py +29 -0
- chromapi/kinematics/topology.py +26 -0
- chromapi/kinematics/types.py +21 -0
- chromapi/locomotion/__init__.py +7 -0
- chromapi/locomotion/balance.py +284 -0
- chromapi/locomotion/gait.py +595 -0
- chromapi/locomotion/recorded_move.py +290 -0
- chromapi/media/__init__.py +2 -0
- chromapi/media/audio.py +424 -0
- chromapi/media/led.py +461 -0
- chromapi/model/LICENSE +438 -0
- chromapi/model/mjcf/additional.xml +21 -0
- chromapi/model/mjcf/assets/18650_li_ion_battery.msh +0 -0
- chromapi/model/mjcf/assets/18650_li_ion_battery.part +13 -0
- chromapi/model/mjcf/assets/battery_holder.msh +0 -0
- chromapi/model/mjcf/assets/battery_holder.part +13 -0
- chromapi/model/mjcf/assets/camera_imx219_d160.msh +0 -0
- chromapi/model/mjcf/assets/camera_imx219_d160.part +13 -0
- chromapi/model/mjcf/assets/chromapi_logo.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo.part +13 -0
- chromapi/model/mjcf/assets/chromapi_logo_circle.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo_inner.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo_outer.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_motherboard.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_motherboard.part +13 -0
- chromapi/model/mjcf/assets/composant2_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant2_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/composant3_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant3_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/composant4_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant4_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1_1.msh +0 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1_1.part +13 -0
- chromapi/model/mjcf/assets/corps14.msh +0 -0
- chromapi/model/mjcf/assets/corps14.part +13 -0
- chromapi/model/mjcf/assets/fan_nf_a4x10.msh +0 -0
- chromapi/model/mjcf/assets/fan_nf_a4x10.part +13 -0
- chromapi/model/mjcf/assets/foot.msh +0 -0
- chromapi/model/mjcf/assets/foot.part +13 -0
- chromapi/model/mjcf/assets/left_coxa.msh +0 -0
- chromapi/model/mjcf/assets/left_coxa.part +13 -0
- chromapi/model/mjcf/assets/left_femur.msh +0 -0
- chromapi/model/mjcf/assets/left_femur.part +13 -0
- chromapi/model/mjcf/assets/left_tibia.msh +0 -0
- chromapi/model/mjcf/assets/left_tibia.part +13 -0
- chromapi/model/mjcf/assets/microswitch.msh +0 -0
- chromapi/model/mjcf/assets/microswitch.part +13 -0
- chromapi/model/mjcf/assets/power_switch.msh +0 -0
- chromapi/model/mjcf/assets/power_switch.part +13 -0
- chromapi/model/mjcf/assets/raspberry_pi_4_model_b.msh +0 -0
- chromapi/model/mjcf/assets/raspberry_pi_4_model_b.part +13 -0
- chromapi/model/mjcf/assets/right_coxa.msh +0 -0
- chromapi/model/mjcf/assets/right_coxa.part +13 -0
- chromapi/model/mjcf/assets/right_femur.msh +0 -0
- chromapi/model/mjcf/assets/right_femur.part +13 -0
- chromapi/model/mjcf/assets/right_tibia.msh +0 -0
- chromapi/model/mjcf/assets/right_tibia.part +13 -0
- chromapi/model/mjcf/assets/robot_case_bottom.msh +0 -0
- chromapi/model/mjcf/assets/robot_case_bottom.part +13 -0
- chromapi/model/mjcf/assets/robot_case_top.msh +0 -0
- chromapi/model/mjcf/assets/robot_case_top.part +13 -0
- chromapi/model/mjcf/assets/rpi_battery_bracket.msh +0 -0
- chromapi/model/mjcf/assets/rpi_battery_bracket.part +13 -0
- chromapi/model/mjcf/assets/speaker.msh +0 -0
- chromapi/model/mjcf/assets/speaker.part +13 -0
- chromapi/model/mjcf/assets/vis_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/vis_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/vis_1__corps1_1.msh +0 -0
- chromapi/model/mjcf/assets/vis_1__corps1_1.part +13 -0
- chromapi/model/mjcf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/ws2812b_cob_pixel_ring_27mm.msh +0 -0
- chromapi/model/mjcf/assets/ws2812b_cob_pixel_ring_27mm.part +13 -0
- chromapi/model/mjcf/config.json +14 -0
- chromapi/model/mjcf/robot.xml +639 -0
- chromapi/model/mjcf/scene.xml +24 -0
- chromapi/model/urdf/assets/18650_li_ion_battery.part +13 -0
- chromapi/model/urdf/assets/18650_li_ion_battery.stl +0 -0
- chromapi/model/urdf/assets/battery_holder.part +13 -0
- chromapi/model/urdf/assets/battery_holder.stl +0 -0
- chromapi/model/urdf/assets/camera_imx219_d160.part +13 -0
- chromapi/model/urdf/assets/camera_imx219_d160.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo.part +13 -0
- chromapi/model/urdf/assets/chromapi_logo.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_circle.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_inner.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_outer.stl +0 -0
- chromapi/model/urdf/assets/chromapi_motherboard.part +13 -0
- chromapi/model/urdf/assets/chromapi_motherboard.stl +0 -0
- chromapi/model/urdf/assets/composant2_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant2_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/composant3_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant3_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/composant4_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant4_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/connector_f_1__corps1.part +13 -0
- chromapi/model/urdf/assets/connector_f_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/connector_f_1__corps1_1.part +13 -0
- chromapi/model/urdf/assets/connector_f_1__corps1_1.stl +0 -0
- chromapi/model/urdf/assets/corps14.part +13 -0
- chromapi/model/urdf/assets/corps14.stl +0 -0
- chromapi/model/urdf/assets/fan_nf_a4x10.part +13 -0
- chromapi/model/urdf/assets/fan_nf_a4x10.stl +0 -0
- chromapi/model/urdf/assets/foot.part +13 -0
- chromapi/model/urdf/assets/foot.stl +0 -0
- chromapi/model/urdf/assets/left_coxa.part +13 -0
- chromapi/model/urdf/assets/left_coxa.stl +0 -0
- chromapi/model/urdf/assets/left_femur.part +13 -0
- chromapi/model/urdf/assets/left_femur.stl +0 -0
- chromapi/model/urdf/assets/left_tibia.part +13 -0
- chromapi/model/urdf/assets/left_tibia.stl +0 -0
- chromapi/model/urdf/assets/microswitch.part +13 -0
- chromapi/model/urdf/assets/microswitch.stl +0 -0
- chromapi/model/urdf/assets/power_switch.part +13 -0
- chromapi/model/urdf/assets/power_switch.stl +0 -0
- chromapi/model/urdf/assets/raspberry_pi_4_model_b.part +13 -0
- chromapi/model/urdf/assets/raspberry_pi_4_model_b.stl +0 -0
- chromapi/model/urdf/assets/right_coxa.part +13 -0
- chromapi/model/urdf/assets/right_coxa.stl +0 -0
- chromapi/model/urdf/assets/right_femur.part +13 -0
- chromapi/model/urdf/assets/right_femur.stl +0 -0
- chromapi/model/urdf/assets/right_tibia.part +13 -0
- chromapi/model/urdf/assets/right_tibia.stl +0 -0
- chromapi/model/urdf/assets/robot_case_bottom.part +13 -0
- chromapi/model/urdf/assets/robot_case_bottom.stl +0 -0
- chromapi/model/urdf/assets/robot_case_top.part +13 -0
- chromapi/model/urdf/assets/robot_case_top.stl +0 -0
- chromapi/model/urdf/assets/rpi_battery_bracket.part +13 -0
- chromapi/model/urdf/assets/rpi_battery_bracket.stl +0 -0
- chromapi/model/urdf/assets/speaker.part +13 -0
- chromapi/model/urdf/assets/speaker.stl +0 -0
- chromapi/model/urdf/assets/vis_1__corps1.part +13 -0
- chromapi/model/urdf/assets/vis_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/vis_1__corps1_1.part +13 -0
- chromapi/model/urdf/assets/vis_1__corps1_1.stl +0 -0
- chromapi/model/urdf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.part +13 -0
- chromapi/model/urdf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/ws2812b_cob_pixel_ring_27mm.part +13 -0
- chromapi/model/urdf/assets/ws2812b_cob_pixel_ring_27mm.stl +0 -0
- chromapi/model/urdf/config.json +8 -0
- chromapi/model/urdf/robot.urdf +2587 -0
- chromapi/utils/calibrate_imu_mount.py +80 -0
- chromapi/utils/calibrate_servo_centers.py +432 -0
- chromapi/utils/flash.py +141 -0
- chromapi/utils/health.py +210 -0
- chromapi/utils/report.py +75 -0
- chromapi/utils/setup_motors.py +189 -0
- chromapi/utils/sim_server.py +126 -0
- chromapi/web/__init__.py +2 -0
- chromapi/web/access.py +167 -0
- chromapi/web/app.py +1136 -0
- chromapi/web/camera.py +202 -0
- chromapi/web/static/fonts/varela-round.woff2 +0 -0
- chromapi/web/static/index.html +1079 -0
- chromapi/web/static/logo.png +0 -0
- chromapi/web/static/motherboard_front.png +0 -0
- chromapi/web/static/socket.io.min.js +7 -0
- chromapi-1.0.0.dist-info/METADATA +222 -0
- chromapi-1.0.0.dist-info/RECORD +219 -0
- chromapi-1.0.0.dist-info/WHEEL +5 -0
- chromapi-1.0.0.dist-info/entry_points.txt +2 -0
- chromapi-1.0.0.dist-info/licenses/LICENSE +201 -0
- chromapi-1.0.0.dist-info/licenses/src/chromapi/model/LICENSE +438 -0
- chromapi-1.0.0.dist-info/top_level.txt +1 -0
chromapi/web/camera.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Camera for the web app: fisheye-corrected MJPEG stream, and recording to a file on the robot."""
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
import threading
|
|
6
|
+
import time
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, Dict, Iterator, Optional, Tuple
|
|
9
|
+
|
|
10
|
+
import cv2
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
STREAM_JPEG_QUALITY = 60
|
|
16
|
+
RECORD_FPS = 30.0 # [Hz]
|
|
17
|
+
RECORD_JPEG_QUALITY = 95
|
|
18
|
+
IDLE_CLOSE_S = 2.0 # [s]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class CameraStreamer:
|
|
22
|
+
"""Background capture thread; :meth:`mjpeg` feeds an HTTP multipart response."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, camera_config: Dict[str, Any], device: int = 0) -> None:
|
|
25
|
+
"""``camera_config``: the ``camera:`` section of the config (fisheye calibration), may be empty."""
|
|
26
|
+
self.device = device
|
|
27
|
+
self._maps: Optional[Tuple[Any, Any]] = None
|
|
28
|
+
self._map_size: Optional[Tuple[int, int]] = None
|
|
29
|
+
if camera_config.get("model") == "fisheye":
|
|
30
|
+
K, D = np.array(camera_config["camera_matrix"]), np.array(camera_config["dist_coeffs"])
|
|
31
|
+
w, h = camera_config["resolution"]
|
|
32
|
+
self._maps = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, (w, h), cv2.CV_16SC2)
|
|
33
|
+
self._map_size = (w, h)
|
|
34
|
+
else:
|
|
35
|
+
logger.info("No fisheye calibration in the config: raw camera stream")
|
|
36
|
+
self._lock = threading.Lock()
|
|
37
|
+
self._jpeg: Optional[bytes] = None
|
|
38
|
+
self._image: Optional[np.ndarray] = None # latest corrected frame, for photos
|
|
39
|
+
self._viewers = 0
|
|
40
|
+
self._stop = threading.Event()
|
|
41
|
+
self._thread: Optional[threading.Thread] = None
|
|
42
|
+
self._rec_request: Optional[Path] = None
|
|
43
|
+
self._rec_stop = False
|
|
44
|
+
self._writer: Optional[cv2.VideoWriter] = None
|
|
45
|
+
self._rec_path: Optional[Path] = None
|
|
46
|
+
self._rec_t0 = 0.0 # [s]
|
|
47
|
+
self._rec_frames = 0
|
|
48
|
+
self.last_recording: Optional[Path] = None
|
|
49
|
+
|
|
50
|
+
def start(self) -> None:
|
|
51
|
+
"""Start the capture thread."""
|
|
52
|
+
self._thread = threading.Thread(target=self._loop, name="chromapi-camera", daemon=True)
|
|
53
|
+
self._thread.start()
|
|
54
|
+
|
|
55
|
+
def stop(self) -> None:
|
|
56
|
+
"""Stop capturing, closing any recording."""
|
|
57
|
+
self._stop.set()
|
|
58
|
+
if self._thread is not None:
|
|
59
|
+
self._thread.join(timeout=1.0)
|
|
60
|
+
|
|
61
|
+
def start_recording(self, path: Path) -> None:
|
|
62
|
+
"""Record the corrected stream to ``path`` (MJPG .avi), until :meth:`stop_recording`."""
|
|
63
|
+
with self._lock:
|
|
64
|
+
if self._writer is None:
|
|
65
|
+
self._rec_request = path
|
|
66
|
+
|
|
67
|
+
def stop_recording(self) -> None:
|
|
68
|
+
"""Close the current recording."""
|
|
69
|
+
with self._lock:
|
|
70
|
+
self._rec_stop = True
|
|
71
|
+
|
|
72
|
+
def photo(self, path: Path) -> Optional[Path]:
|
|
73
|
+
"""Save the latest corrected frame to ``path`` (JPEG); None if no frame yet (nobody watching)."""
|
|
74
|
+
with self._lock:
|
|
75
|
+
image = self._image
|
|
76
|
+
if image is None:
|
|
77
|
+
return None
|
|
78
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
79
|
+
cv2.imwrite(str(path), image, [cv2.IMWRITE_JPEG_QUALITY, RECORD_JPEG_QUALITY])
|
|
80
|
+
return path
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def viewers(self) -> int:
|
|
84
|
+
"""Streams being watched right now (the camera is on while > 0, or while recording)."""
|
|
85
|
+
with self._lock:
|
|
86
|
+
return self._viewers
|
|
87
|
+
|
|
88
|
+
def _wanted(self) -> bool:
|
|
89
|
+
with self._lock:
|
|
90
|
+
return self._viewers > 0 or self._writer is not None or self._rec_request is not None
|
|
91
|
+
|
|
92
|
+
def recording_status(self) -> Dict[str, Any]:
|
|
93
|
+
"""``on``, ``seconds`` recorded, ``last`` file name."""
|
|
94
|
+
with self._lock:
|
|
95
|
+
return {
|
|
96
|
+
"on": self._writer is not None or self._rec_request is not None,
|
|
97
|
+
"seconds": self._rec_frames / RECORD_FPS,
|
|
98
|
+
"last": self.last_recording.name if self.last_recording else None,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
def _process(self, frame: np.ndarray) -> np.ndarray:
|
|
102
|
+
frame = cv2.flip(frame, -1) # the camera is mounted upside down
|
|
103
|
+
if self._maps is not None and (frame.shape[1], frame.shape[0]) == self._map_size:
|
|
104
|
+
map1, map2 = self._maps
|
|
105
|
+
frame = cv2.remap(frame, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
|
|
106
|
+
return frame
|
|
107
|
+
|
|
108
|
+
def _record(self, frame: np.ndarray, now: float) -> None:
|
|
109
|
+
"""Write ``frame`` as often as the 30 fps clock asks, so the file plays at real speed."""
|
|
110
|
+
assert self._writer is not None
|
|
111
|
+
due = int((now - self._rec_t0) * RECORD_FPS) + 1
|
|
112
|
+
while self._rec_frames < due:
|
|
113
|
+
self._writer.write(frame)
|
|
114
|
+
self._rec_frames += 1
|
|
115
|
+
|
|
116
|
+
def _close_writer(self) -> None:
|
|
117
|
+
if self._writer is not None:
|
|
118
|
+
self._writer.release()
|
|
119
|
+
logger.info("Recording saved: %s (%.1f s)", self._rec_path, self._rec_frames / RECORD_FPS)
|
|
120
|
+
with self._lock:
|
|
121
|
+
self._writer = None
|
|
122
|
+
self.last_recording = self._rec_path
|
|
123
|
+
|
|
124
|
+
def _loop(self) -> None:
|
|
125
|
+
"""Open the camera when wanted, capture, close it once nobody watches nor records."""
|
|
126
|
+
while not self._stop.is_set():
|
|
127
|
+
if not self._wanted():
|
|
128
|
+
self._stop.wait(0.1)
|
|
129
|
+
continue
|
|
130
|
+
cap = cv2.VideoCapture(self.device)
|
|
131
|
+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
|
|
132
|
+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
|
|
133
|
+
cap.set(cv2.CAP_PROP_FPS, RECORD_FPS)
|
|
134
|
+
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
|
|
135
|
+
if not cap.isOpened():
|
|
136
|
+
logger.warning("Cannot open /dev/video%d: no camera stream", self.device)
|
|
137
|
+
return
|
|
138
|
+
logger.info("Camera on")
|
|
139
|
+
try:
|
|
140
|
+
self._capture(cap)
|
|
141
|
+
finally:
|
|
142
|
+
self._close_writer()
|
|
143
|
+
cap.release()
|
|
144
|
+
with self._lock:
|
|
145
|
+
self._jpeg, self._image = None, None
|
|
146
|
+
logger.info("Camera off")
|
|
147
|
+
|
|
148
|
+
def _capture(self, cap: Any) -> None:
|
|
149
|
+
idle_since: Optional[float] = None # [s]
|
|
150
|
+
while not self._stop.is_set():
|
|
151
|
+
now = time.monotonic()
|
|
152
|
+
if self._wanted():
|
|
153
|
+
idle_since = None
|
|
154
|
+
elif idle_since is None:
|
|
155
|
+
idle_since = now
|
|
156
|
+
elif now - idle_since > IDLE_CLOSE_S:
|
|
157
|
+
return
|
|
158
|
+
ok, frame = cap.read()
|
|
159
|
+
if not ok:
|
|
160
|
+
time.sleep(0.05)
|
|
161
|
+
continue
|
|
162
|
+
now = time.monotonic()
|
|
163
|
+
with self._lock:
|
|
164
|
+
watching = self._viewers > 0
|
|
165
|
+
request, self._rec_request = self._rec_request, None
|
|
166
|
+
stop_rec, self._rec_stop = self._rec_stop, False
|
|
167
|
+
if stop_rec:
|
|
168
|
+
self._close_writer()
|
|
169
|
+
if request is not None:
|
|
170
|
+
request.parent.mkdir(parents=True, exist_ok=True)
|
|
171
|
+
writer = cv2.VideoWriter(str(request), cv2.VideoWriter.fourcc(*"MJPG"), RECORD_FPS,
|
|
172
|
+
(frame.shape[1], frame.shape[0]))
|
|
173
|
+
writer.set(cv2.VIDEOWRITER_PROP_QUALITY, RECORD_JPEG_QUALITY)
|
|
174
|
+
with self._lock:
|
|
175
|
+
self._writer, self._rec_path, self._rec_t0, self._rec_frames = writer, request, now, 0
|
|
176
|
+
if not watching and self._writer is None:
|
|
177
|
+
continue
|
|
178
|
+
image = self._process(frame)
|
|
179
|
+
if self._writer is not None:
|
|
180
|
+
self._record(image, now)
|
|
181
|
+
if watching:
|
|
182
|
+
ok, buf = cv2.imencode(".jpg", image, [cv2.IMWRITE_JPEG_QUALITY, STREAM_JPEG_QUALITY])
|
|
183
|
+
if ok:
|
|
184
|
+
with self._lock:
|
|
185
|
+
self._jpeg, self._image = buf.tobytes(), image
|
|
186
|
+
|
|
187
|
+
def mjpeg(self) -> Iterator[bytes]:
|
|
188
|
+
"""Multipart JPEG frames, for as long as the client reads them."""
|
|
189
|
+
with self._lock:
|
|
190
|
+
self._viewers += 1
|
|
191
|
+
try:
|
|
192
|
+
last = None
|
|
193
|
+
while not self._stop.is_set():
|
|
194
|
+
with self._lock:
|
|
195
|
+
frame = self._jpeg
|
|
196
|
+
if frame is not None and frame is not last:
|
|
197
|
+
last = frame
|
|
198
|
+
yield b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + frame + b"\r\n"
|
|
199
|
+
time.sleep(0.02)
|
|
200
|
+
finally:
|
|
201
|
+
with self._lock:
|
|
202
|
+
self._viewers -= 1
|
|
Binary file
|