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