Ran black code formatter on everything.
[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 import warnings
10
11 import cv2  # type: ignore
12 import numpy as np
13 import requests
14
15 import decorator_utils
16 import exceptions
17
18 logger = logging.getLogger(__name__)
19
20
21 class RawJpgHsv(NamedTuple):
22     """Raw image bytes, the jpeg image and the HSV (hue saturation value) image."""
23
24     raw: Optional[bytes]
25     jpg: Optional[np.ndarray]
26     hsv: Optional[np.ndarray]
27
28
29 class SanityCheckImageMetadata(NamedTuple):
30     """Is a Blue Iris image bad (big grey borders around it) or infrared?"""
31
32     is_bad_image: bool
33     is_infrared_image: bool
34
35
36 def sanity_check_image(hsv: np.ndarray) -> SanityCheckImageMetadata:
37     """See if a Blue Iris or Shinobi image is bad and infrared."""
38
39     def is_near(a, b) -> bool:
40         return abs(a - b) < 3
41
42     rows, cols, _ = hsv.shape
43     num_pixels = rows * cols
44     weird_orange_count = 0
45     hs_zero_count = 0
46     for r in range(rows):
47         for c in range(cols):
48             pixel = hsv[(r, c)]
49             if (
50                 is_near(pixel[0], 16)
51                 and is_near(pixel[1], 117)
52                 and is_near(pixel[2], 196)
53             ):
54                 weird_orange_count += 1
55             elif is_near(pixel[0], 0) and is_near(pixel[1], 0):
56                 hs_zero_count += 1
57     logger.debug(f"hszero#={hs_zero_count}, weird_orange={weird_orange_count}")
58     return SanityCheckImageMetadata(
59         hs_zero_count > (num_pixels * 0.75),
60         weird_orange_count > (num_pixels * 0.75),
61     )
62
63
64 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
65 def fetch_camera_image_from_video_server(
66     camera_name: str, *, width: int = 256, quality: int = 70
67 ) -> Optional[bytes]:
68     """Fetch the raw webcam image from the video server."""
69     camera_name = camera_name.replace(".house", "")
70     camera_name = camera_name.replace(".cabin", "")
71     url = f"http://10.0.0.226:8080/Umtxxf1uKMBniFblqeQ9KRbb6DDzN4/jpeg/GKlT2FfiSQ/{camera_name}/s.jpg"
72     logger.debug(f'Fetching image from {url}')
73     try:
74         response = requests.get(url, stream=False, timeout=10.0)
75         if response.ok:
76             raw = response.content
77             logger.debug(
78                 f'Read {len(response.content)} byte image from HTTP server'
79             )
80             tmp = np.frombuffer(raw, dtype="uint8")
81             logger.debug(
82                 f'Translated raw content into {tmp.shape} {type(tmp)} with element type {type(tmp[0])}.'
83             )
84             jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
85             logger.debug(
86                 f'Decoded into {jpg.shape} jpeg {type(jpg)} with element type {type(jpg[0][0])}'
87             )
88             hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
89             logger.debug(
90                 f'Converted JPG into HSV {hsv.shape} HSV {type(hsv)} with element type {type(hsv[0][0])}'
91             )
92             (_, is_bad_image) = sanity_check_image(hsv)
93             if not is_bad_image:
94                 return raw
95     except Exception as e:
96         logger.exception(e)
97     msg = f"Got a bad image or HTTP error from {url}; returning None."
98     logger.warning(msg)
99     warnings.warn(msg, stacklevel=2)
100     return None
101
102
103 def camera_name_to_hostname(camera_name: str) -> str:
104     """Map a camera name to a hostname
105
106     >>> camera_name_to_hostname('driveway')
107     'driveway.house'
108
109     >>> camera_name_to_hostname('cabin_driveway')
110     'driveway.cabin'
111
112     """
113     mapping = {
114         "driveway": "driveway.house",
115         "backyard": "backyard.house",
116         "frontdoor": "frontdoor.house",
117         "cabin_driveway": "driveway.cabin",
118     }
119     camera_name = mapping.get(camera_name, camera_name)
120     if "." not in camera_name:
121         hostname = platform.node()
122         suffix = hostname.split(".")[-1]
123         camera_name += f".{suffix}"
124     return camera_name
125
126
127 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
128 def fetch_camera_image_from_rtsp_stream(
129     camera_name: str, *, width: int = 256
130 ) -> Optional[bytes]:
131     """Fetch the raw webcam image straight from the webcam's RTSP stream."""
132     hostname = camera_name_to_hostname(camera_name)
133     stream = f"rtsp://camera:IaLaIok@{hostname}:554/live"
134     logger.debug(f'Fetching image from RTSP stream {stream}')
135     try:
136         cmd = [
137             "/usr/bin/timeout",
138             "-k 9s",
139             "8s",
140             "/usr/local/bin/ffmpeg",
141             "-y",
142             "-i",
143             f"{stream}",
144             "-f",
145             "singlejpeg",
146             "-vframes",
147             "1",
148             "-vf",
149             f"scale={width}:-1",
150             "-",
151         ]
152         with subprocess.Popen(
153             cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
154         ) as proc:
155             out, _ = proc.communicate(timeout=10)
156             return out
157     except Exception as e:
158         logger.exception(e)
159     msg = f"Failed to retrieve image via RTSP {stream}, returning None."
160     logger.warning(msg)
161     warnings.warn(msg, stacklevel=2)
162     return None
163
164
165 @decorator_utils.timeout(seconds=30, use_signals=False)
166 def _fetch_camera_image(
167     camera_name: str, *, width: int = 256, quality: int = 70
168 ) -> RawJpgHsv:
169     """Fetch a webcam image given the camera name."""
170     logger.debug("Trying to fetch camera image from video server")
171     raw = fetch_camera_image_from_video_server(
172         camera_name, width=width, quality=quality
173     )
174     if raw is None:
175         logger.debug(
176             "Reading from video server failed; trying direct RTSP stream"
177         )
178         raw = fetch_camera_image_from_rtsp_stream(camera_name, width=width)
179     if raw is not None and len(raw) > 0:
180         tmp = np.frombuffer(raw, dtype="uint8")
181         jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
182         hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
183         return RawJpgHsv(
184             raw=raw,
185             jpg=jpg,
186             hsv=hsv,
187         )
188     msg = (
189         "Failed to retieve image from both video server and direct RTSP stream"
190     )
191     logger.warning(msg)
192     warnings.warn(msg, stacklevel=2)
193     return RawJpgHsv(None, None, None)
194
195
196 def fetch_camera_image(
197     camera_name: str, *, width: int = 256, quality: int = 70
198 ) -> RawJpgHsv:
199     try:
200         return _fetch_camera_image(camera_name, width=width, quality=quality)
201     except exceptions.TimeoutError:
202         logger.warning('Fetching camera image operation timed out.')
203         return RawJpgHsv(None, None, None)
204
205
206 if __name__ == '__main__':
207     import doctest
208
209     doctest.testmod()