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