3 """Utilities for dealing with webcam images."""
8 from typing import NamedTuple, Optional
10 import cv2 # type: ignore
14 import decorator_utils
16 logger = logging.getLogger(__name__)
19 class RawJpgHsv(NamedTuple):
20 """Raw image bytes, the jpeg image and the HSV (hue saturation value) image."""
22 jpg: Optional[np.ndarray]
23 hsv: Optional[np.ndarray]
26 class BlueIrisImageMetadata(NamedTuple):
27 """Is a Blue Iris image bad (big grey borders around it) or infrared?"""
29 is_infrared_image: bool
32 def analyze_blue_iris_image(hsv: np.ndarray) -> BlueIrisImageMetadata:
33 """See if a Blue Iris image is bad and infrared."""
34 rows, cols, _ = hsv.shape
35 num_pixels = rows * cols
41 if pixel[0] == 0 and pixel[1] == 0:
43 if abs(pixel[2] - 64) <= 10:
45 logger.debug(f"gray#={gray_count}, hs0#={hs_zero_count}")
46 return BlueIrisImageMetadata(
47 gray_count > (num_pixels * 0.33), hs_zero_count > (num_pixels * 0.75)
51 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
52 def fetch_camera_image_from_video_server(
53 camera_name: str, *, width: int = 256, quality: int = 70
55 """Fetch the raw webcam image from the video server."""
56 camera_name = camera_name.replace(".house", "")
57 camera_name = camera_name.replace(".cabin", "")
58 url = f"http://10.0.0.56:81/image/{camera_name}?w={width}&q={quality}"
60 response = requests.get(url, stream=False, timeout=10.0)
62 raw = response.content
63 tmp = np.frombuffer(raw, dtype="uint8")
64 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
65 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
66 (is_bad_image, _) = analyze_blue_iris_image(hsv)
68 logger.debug(f"Got a good image from {url}")
70 except Exception as e:
72 logger.warning(f"Got a bad image or HTTP error from {url}")
76 def blue_iris_camera_name_to_hostname(camera_name: str) -> str:
78 "driveway": "driveway.house",
79 "backyard": "backyard.house",
80 "frontdoor": "frontdoor.house",
81 "cabin_driveway": "driveway.cabin",
83 camera_name = mapping.get(camera_name, camera_name)
84 if "." not in camera_name:
85 hostname = platform.node()
86 suffix = hostname.split(".")[-1]
87 camera_name += f".{suffix}"
91 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
92 def fetch_camera_image_from_rtsp_stream(
93 camera_name: str, *, width: int = 256
95 """Fetch the raw webcam image straight from the webcam's RTSP stream."""
96 hostname = blue_iris_camera_name_to_hostname(camera_name)
102 "/usr/local/bin/ffmpeg",
105 f"rtsp://camera:IaLaIok@{hostname}:554/live",
114 with subprocess.Popen(
115 cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
117 out, _ = proc.communicate(timeout=10)
119 except Exception as e:
121 logger.warning("Failed to retrieve image from RTSP stream")
125 @decorator_utils.timeout(seconds=30, use_signals=False)
126 def _fetch_camera_image(
127 camera_name: str, *, width: int = 256, quality: int = 70
129 """Fetch a webcam image given the camera name."""
130 logger.debug("Trying to fetch camera image from video server")
131 raw = fetch_camera_image_from_video_server(
132 camera_name, width=width, quality=quality
136 "Reading from video server failed; trying direct RTSP stream"
138 raw = fetch_camera_image_from_rtsp_stream(camera_name, width=width)
139 if raw is not None and len(raw) > 0:
140 tmp = np.frombuffer(raw, dtype="uint8")
141 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
142 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
149 "Failed to retieve image from both video server and direct RTSP stream"
151 return RawJpgHsv(None, None, None)
154 def fetch_camera_image(
155 camera_name: str, *, width: int = 256, quality: int = 70
158 return _fetch_camera_image(camera_name, width=width, quality=quality)
159 except decorator_utils.TimeoutError:
160 return RawJpgHsv(None, None, None)