Make smart futures avoid polling.
[python_utils.git] / camera_utils.py
1 #!/usr/bin/env python3
2
3 """Utilities for dealing with webcam images."""
4
5 import logging
6 import platform
7 import subprocess
8 from typing import NamedTuple, Optional
9
10 import cv2  # type: ignore
11 import numpy as np
12 import requests
13
14 import decorator_utils
15
16 logger = logging.getLogger(__name__)
17
18
19 class RawJpgHsv(NamedTuple):
20     """Raw image bytes, the jpeg image and the HSV (hue saturation value) image."""
21     raw: Optional[bytes]
22     jpg: Optional[np.ndarray]
23     hsv: Optional[np.ndarray]
24
25
26 class BlueIrisImageMetadata(NamedTuple):
27     """Is a Blue Iris image bad (big grey borders around it) or infrared?"""
28     is_bad_image: bool
29     is_infrared_image: bool
30
31
32 def analyze_blue_iris_image(hsv: np.ndarray) -> BlueIrisImageMetadata:
33     """See if a Blue Iris image is bad and infrared."""
34     rows, cols, _ = hsv.shape
35     num_pixels = rows * cols
36     hs_zero_count = 0
37     gray_count = 0
38     for r in range(rows):
39         for c in range(cols):
40             pixel = hsv[(r, c)]
41             if pixel[0] == 0 and pixel[1] == 0:
42                 hs_zero_count += 1
43             if abs(pixel[2] - 64) <= 10:
44                 gray_count += 1
45     logger.debug(f"gray#={gray_count}, hs0#={hs_zero_count}")
46     return BlueIrisImageMetadata(
47         gray_count > (num_pixels * 0.33), hs_zero_count > (num_pixels * 0.75)
48     )
49
50
51 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
52 def fetch_camera_image_from_video_server(
53     camera_name: str, *, width: int = 256, quality: int = 70
54 ) -> Optional[bytes]:
55     """Fetch the raw webcam image from the video server."""
56     camera_name = camera_name.replace(".house", "")
57     camera_name = camera_name.replace(".cabin", "")
58     url = f"http://10.0.0.56:81/image/{camera_name}?w={width}&q={quality}"
59     try:
60         response = requests.get(url, stream=False, timeout=10.0)
61         if response.ok:
62             raw = response.content
63             tmp = np.frombuffer(raw, dtype="uint8")
64             jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
65             hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
66             (is_bad_image, _) = analyze_blue_iris_image(hsv)
67             if not is_bad_image:
68                 logger.debug(f"Got a good image from {url}")
69                 return raw
70     except Exception as e:
71         logger.exception(e)
72     logger.warning(f"Got a bad image or HTTP error from {url}")
73     return None
74
75
76 def blue_iris_camera_name_to_hostname(camera_name: str) -> str:
77     mapping = {
78         "driveway": "driveway.house",
79         "backyard": "backyard.house",
80         "frontdoor": "frontdoor.house",
81         "cabin_driveway": "driveway.cabin",
82     }
83     camera_name = mapping.get(camera_name, camera_name)
84     if "." not in camera_name:
85         hostname = platform.node()
86         suffix = hostname.split(".")[-1]
87         camera_name += f".{suffix}"
88     return camera_name
89
90
91 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
92 def fetch_camera_image_from_rtsp_stream(
93     camera_name: str, *, width: int = 256
94 ) -> Optional[bytes]:
95     """Fetch the raw webcam image straight from the webcam's RTSP stream."""
96     hostname = blue_iris_camera_name_to_hostname(camera_name)
97     try:
98         cmd = [
99             "/usr/bin/timeout",
100             "-k 9s",
101             "8s",
102             "/usr/local/bin/ffmpeg",
103             "-y",
104             "-i",
105             f"rtsp://camera:IaLaIok@{hostname}:554/live",
106             "-f",
107             "singlejpeg",
108             "-vframes",
109             "1",
110             "-vf",
111             f"scale={width}:-1",
112             "-",
113         ]
114         with subprocess.Popen(
115             cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
116         ) as proc:
117             out, _ = proc.communicate(timeout=10)
118             return out
119     except Exception as e:
120         logger.exception(e)
121     logger.warning("Failed to retrieve image from RTSP stream")
122     return None
123
124
125 @decorator_utils.timeout(seconds=30, use_signals=False)
126 def _fetch_camera_image(
127     camera_name: str, *, width: int = 256, quality: int = 70
128 ) -> RawJpgHsv:
129     """Fetch a webcam image given the camera name."""
130     logger.debug("Trying to fetch camera image from video server")
131     raw = fetch_camera_image_from_video_server(
132         camera_name, width=width, quality=quality
133     )
134     if raw is None:
135         logger.debug(
136             "Reading from video server failed; trying direct RTSP stream"
137         )
138         raw = fetch_camera_image_from_rtsp_stream(camera_name, width=width)
139     if raw is not None and len(raw) > 0:
140         tmp = np.frombuffer(raw, dtype="uint8")
141         jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
142         hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
143         return RawJpgHsv(
144             raw=raw,
145             jpg=jpg,
146             hsv=hsv,
147         )
148     logger.warning(
149         "Failed to retieve image from both video server and direct RTSP stream"
150     )
151     return RawJpgHsv(None, None, None)
152
153
154 def fetch_camera_image(
155     camera_name: str, *, width: int = 256, quality: int = 70
156 ) -> RawJpgHsv:
157     try:
158         return _fetch_camera_image(camera_name, width=width, quality=quality)
159     except decorator_utils.TimeoutError:
160         return RawJpgHsv(None, None, None)