3 """Utilities for dealing with webcam images."""
9 from typing import NamedTuple, Optional
11 import cv2 # type: ignore
15 import decorator_utils
18 logger = logging.getLogger(__name__)
21 class RawJpgHsv(NamedTuple):
22 """Raw image bytes, the jpeg image and the HSV (hue saturation value) image."""
25 jpg: Optional[np.ndarray]
26 hsv: Optional[np.ndarray]
29 class SanityCheckImageMetadata(NamedTuple):
30 """Is a Blue Iris image bad (big grey borders around it) or infrared?"""
33 is_infrared_image: bool
36 def sanity_check_image(hsv: np.ndarray) -> SanityCheckImageMetadata:
37 """See if a Blue Iris or Shinobi image is bad and infrared."""
39 def is_near(a, b) -> bool:
42 rows, cols, _ = hsv.shape
43 num_pixels = rows * cols
44 weird_orange_count = 0
49 if is_near(pixel[0], 16) and is_near(pixel[1], 117) and is_near(pixel[2], 196):
50 weird_orange_count += 1
51 elif is_near(pixel[0], 0) and is_near(pixel[1], 0):
53 logger.debug(f"hszero#={hs_zero_count}, weird_orange={weird_orange_count}")
54 return SanityCheckImageMetadata(
55 hs_zero_count > (num_pixels * 0.75),
56 weird_orange_count > (num_pixels * 0.75),
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
64 """Fetch the raw webcam image from the video server."""
65 camera_name = camera_name.replace(".house", "")
66 camera_name = camera_name.replace(".cabin", "")
68 f"http://10.0.0.226:8080/Umtxxf1uKMBniFblqeQ9KRbb6DDzN4/jpeg/GKlT2FfiSQ/{camera_name}/s.jpg"
70 logger.debug(f'Fetching image from {url}')
72 response = requests.get(url, stream=False, timeout=10.0)
74 raw = response.content
75 logger.debug(f'Read {len(response.content)} byte image from HTTP server')
76 tmp = np.frombuffer(raw, dtype="uint8")
78 f'Translated raw content into {tmp.shape} {type(tmp)} with element type {type(tmp[0])}.'
80 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
82 f'Decoded into {jpg.shape} jpeg {type(jpg)} with element type {type(jpg[0][0])}'
84 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
86 f'Converted JPG into HSV {hsv.shape} HSV {type(hsv)} with element type {type(hsv[0][0])}'
88 (_, is_bad_image) = sanity_check_image(hsv)
91 except Exception as e:
93 msg = f"Got a bad image or HTTP error from {url}; returning None."
95 warnings.warn(msg, stacklevel=2)
99 def camera_name_to_hostname(camera_name: str) -> str:
100 """Map a camera name to a hostname
102 >>> camera_name_to_hostname('driveway')
105 >>> camera_name_to_hostname('cabin_driveway')
110 "driveway": "driveway.house",
111 "backyard": "backyard.house",
112 "frontdoor": "frontdoor.house",
113 "cabin_driveway": "driveway.cabin",
115 camera_name = mapping.get(camera_name, camera_name)
116 if "." not in camera_name:
117 hostname = platform.node()
118 suffix = hostname.split(".")[-1]
119 camera_name += f".{suffix}"
123 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
124 def fetch_camera_image_from_rtsp_stream(camera_name: str, *, width: int = 256) -> Optional[bytes]:
125 """Fetch the raw webcam image straight from the webcam's RTSP stream."""
126 hostname = camera_name_to_hostname(camera_name)
127 stream = f"rtsp://camera:IaLaIok@{hostname}:554/live"
128 logger.debug(f'Fetching image from RTSP stream {stream}')
134 "/usr/local/bin/ffmpeg",
146 with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) as proc:
147 out, _ = proc.communicate(timeout=10)
149 except Exception as e:
151 msg = f"Failed to retrieve image via RTSP {stream}, returning None."
153 warnings.warn(msg, stacklevel=2)
157 @decorator_utils.timeout(seconds=30, use_signals=False)
158 def _fetch_camera_image(camera_name: str, *, width: int = 256, quality: int = 70) -> RawJpgHsv:
159 """Fetch a webcam image given the camera name."""
160 logger.debug("Trying to fetch camera image from video server")
161 raw = fetch_camera_image_from_video_server(camera_name, width=width, quality=quality)
163 logger.debug("Reading from video server failed; trying direct RTSP stream")
164 raw = fetch_camera_image_from_rtsp_stream(camera_name, width=width)
165 if raw is not None and len(raw) > 0:
166 tmp = np.frombuffer(raw, dtype="uint8")
167 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
168 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
174 msg = "Failed to retieve image from both video server and direct RTSP stream"
176 warnings.warn(msg, stacklevel=2)
177 return RawJpgHsv(None, None, None)
180 def fetch_camera_image(camera_name: str, *, width: int = 256, quality: int = 70) -> RawJpgHsv:
182 return _fetch_camera_image(camera_name, width=width, quality=quality)
183 except exceptions.TimeoutError:
184 logger.warning('Fetching camera image operation timed out.')
185 return RawJpgHsv(None, None, None)
188 if __name__ == '__main__':