3 """Utilities for dealing with webcam images."""
8 from typing import NamedTuple, Optional
10 import cv2 # type: ignore
14 import decorator_utils
17 logger = logging.getLogger(__name__)
20 class RawJpgHsv(NamedTuple):
21 """Raw image bytes, the jpeg image and the HSV (hue saturation value) image."""
23 jpg: Optional[np.ndarray]
24 hsv: Optional[np.ndarray]
27 class SanityCheckImageMetadata(NamedTuple):
28 """Is a Blue Iris image bad (big grey borders around it) or infrared?"""
30 is_infrared_image: bool
33 def sanity_check_image(hsv: np.ndarray) -> SanityCheckImageMetadata:
34 """See if a Blue Iris image is bad and infrared."""
35 def is_near(a, b) -> bool:
38 rows, cols, _ = hsv.shape
39 num_pixels = rows * cols
40 weird_orange_count = 0
46 is_near(pixel[0], 16) and
47 is_near(pixel[1], 117) and
48 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), weird_orange_count > (num_pixels * 0.75)
59 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
60 def fetch_camera_image_from_video_server(
61 camera_name: str, *, width: int = 256, quality: int = 70
63 """Fetch the raw webcam image from the video server."""
64 camera_name = camera_name.replace(".house", "")
65 camera_name = camera_name.replace(".cabin", "")
66 url = f"http://10.0.0.226:8080/Umtxxf1uKMBniFblqeQ9KRbb6DDzN4/jpeg/GKlT2FfiSQ/{camera_name}/s.jpg"
68 response = requests.get(url, stream=False, timeout=10.0)
70 raw = response.content
71 logger.debug(f'Read {len(response.content)} byte image from HTTP server')
72 tmp = np.frombuffer(raw, dtype="uint8")
73 logger.debug(f'Translated raw content into {tmp.shape} {type(tmp)} with element type {type(tmp[0])}.')
74 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
75 logger.debug(f'Decoded into {jpg.shape} jpeg {type(jpg)} with element type {type(jpg[0][0])}')
76 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
77 logger.debug(f'Converted JPG into HSV {hsv.shape} HSV {type(hsv)} with element type {type(hsv[0][0])}')
78 (_, is_bad_image) = sanity_check_image(hsv)
80 logger.debug(f"Got a good image from {url}")
82 except Exception as e:
84 logger.warning(f"Got a bad image or HTTP error from {url}")
88 def blue_iris_camera_name_to_hostname(camera_name: str) -> str:
90 "driveway": "driveway.house",
91 "backyard": "backyard.house",
92 "frontdoor": "frontdoor.house",
93 "cabin_driveway": "driveway.cabin",
95 camera_name = mapping.get(camera_name, camera_name)
96 if "." not in camera_name:
97 hostname = platform.node()
98 suffix = hostname.split(".")[-1]
99 camera_name += f".{suffix}"
103 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
104 def fetch_camera_image_from_rtsp_stream(
105 camera_name: str, *, width: int = 256
106 ) -> Optional[bytes]:
107 """Fetch the raw webcam image straight from the webcam's RTSP stream."""
108 hostname = blue_iris_camera_name_to_hostname(camera_name)
114 "/usr/local/bin/ffmpeg",
117 f"rtsp://camera:IaLaIok@{hostname}:554/live",
126 with subprocess.Popen(
127 cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
129 out, _ = proc.communicate(timeout=10)
131 except Exception as e:
133 logger.warning("Failed to retrieve image from RTSP stream")
137 @decorator_utils.timeout(seconds=30, use_signals=False)
138 def _fetch_camera_image(
139 camera_name: str, *, width: int = 256, quality: int = 70
141 """Fetch a webcam image given the camera name."""
142 logger.debug("Trying to fetch camera image from video server")
143 raw = fetch_camera_image_from_video_server(
144 camera_name, width=width, quality=quality
148 "Reading from video server failed; trying direct RTSP stream"
150 raw = fetch_camera_image_from_rtsp_stream(camera_name, width=width)
151 if raw is not None and len(raw) > 0:
152 tmp = np.frombuffer(raw, dtype="uint8")
153 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
154 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
161 "Failed to retieve image from both video server and direct RTSP stream"
163 return RawJpgHsv(None, None, None)
166 def fetch_camera_image(
167 camera_name: str, *, width: int = 256, quality: int = 70
170 return _fetch_camera_image(camera_name, width=width, quality=quality)
171 except exceptions.TimeoutError:
172 return RawJpgHsv(None, None, None)