3 # © Copyright 2021-2022, Scott Gasch
5 """Utilities for dealing with webcam images."""
11 from dataclasses import dataclass
12 from typing import Optional
14 import cv2 # type: ignore
18 import decorator_utils
22 logger = logging.getLogger(__name__)
27 """Raw image bytes, the jpeg image and the HSV (hue saturation value) image."""
29 raw: Optional[bytes] = None
30 jpg: Optional[np.ndarray] = None
31 hsv: Optional[np.ndarray] = None
35 class SanityCheckImageMetadata:
36 """Is a Blue Iris image bad (big grey borders around it) or infrared?"""
38 is_infrared_image: bool = False
39 is_bad_image: bool = False
42 def sanity_check_image(hsv: np.ndarray) -> SanityCheckImageMetadata:
43 """See if a Blue Iris or Shinobi image is bad and infrared."""
45 def is_near(a, b) -> bool:
48 rows, cols, _ = hsv.shape
49 num_pixels = rows * cols
50 weird_orange_count = 0
55 if is_near(pixel[0], 16) and is_near(pixel[1], 117) and is_near(pixel[2], 196):
56 weird_orange_count += 1
57 elif is_near(pixel[0], 0) and is_near(pixel[1], 0):
59 logger.debug("hszero#=%d, weird_orange=%d", hs_zero_count, weird_orange_count)
60 return SanityCheckImageMetadata(
61 hs_zero_count > (num_pixels * 0.75),
62 weird_orange_count > (num_pixels * 0.75),
66 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
67 def fetch_camera_image_from_video_server(
68 camera_name: str, *, width: int = 256, quality: int = 70
70 """Fetch the raw webcam image from the video server."""
71 camera_name = camera_name.replace(".house", "")
72 camera_name = camera_name.replace(".cabin", "")
73 url = f"http://10.0.0.226:8080/{scott_secrets.SHINOBI_KEY1}/jpeg/{scott_secrets.SHINOBI_KEY2}/{camera_name}/s.jpg"
74 logger.debug('Fetching image from %s', url)
76 response = requests.get(url, stream=False, timeout=10.0)
78 raw = response.content
79 logger.debug('Read %d byte image from HTTP server', len(response.content))
80 tmp = np.frombuffer(raw, dtype="uint8")
82 'Translated raw content into %s %s with element type %s',
87 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
89 'Decoded into %s jpeg %s with element type %s',
94 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
96 'Converted JPG into %s HSV HSV %s with element type %s',
101 ret = sanity_check_image(hsv)
102 if not ret.is_bad_image:
104 except Exception as e:
106 msg = f"Got a bad image or HTTP error from {url}; returning None."
108 warnings.warn(msg, stacklevel=2)
112 def camera_name_to_hostname(camera_name: str) -> str:
113 """Map a camera name to a hostname
115 >>> camera_name_to_hostname('driveway')
118 >>> camera_name_to_hostname('cabin_driveway')
123 "driveway": "driveway.house",
124 "backyard": "backyard.house",
125 "frontdoor": "frontdoor.house",
126 "cabin_driveway": "driveway.cabin",
128 camera_name = mapping.get(camera_name, camera_name)
129 if "." not in camera_name:
130 hostname = platform.node()
131 suffix = hostname.split(".")[-1]
132 camera_name += f".{suffix}"
136 @decorator_utils.retry_if_none(tries=2, delay_sec=1, backoff=1.1)
137 def fetch_camera_image_from_rtsp_stream(camera_name: str, *, width: int = 256) -> Optional[bytes]:
138 """Fetch the raw webcam image straight from the webcam's RTSP stream."""
139 hostname = camera_name_to_hostname(camera_name)
140 stream = f"rtsp://camera:{scott_secrets.CAMERA_PASSWORD}@{hostname}:554/live"
141 logger.debug('Fetching image from RTSP stream %s', stream)
147 "/usr/local/bin/ffmpeg",
159 with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) as proc:
160 out, _ = proc.communicate(timeout=10)
162 except Exception as e:
164 msg = f"Failed to retrieve image via RTSP {stream}, returning None."
166 warnings.warn(msg, stacklevel=2)
170 @decorator_utils.timeout(seconds=30, use_signals=False)
171 def _fetch_camera_image(camera_name: str, *, width: int = 256, quality: int = 70) -> RawJpgHsv:
172 """Fetch a webcam image given the camera name."""
173 logger.debug("Trying to fetch camera image from video server")
174 raw = fetch_camera_image_from_video_server(camera_name, width=width, quality=quality)
176 logger.debug("Reading from video server failed; trying direct RTSP stream")
177 raw = fetch_camera_image_from_rtsp_stream(camera_name, width=width)
178 if raw is not None and len(raw) > 0:
179 tmp = np.frombuffer(raw, dtype="uint8")
180 jpg = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
181 hsv = cv2.cvtColor(jpg, cv2.COLOR_BGR2HSV)
187 msg = "Failed to retieve image from both video server and direct RTSP stream"
189 warnings.warn(msg, stacklevel=2)
190 return RawJpgHsv(None, None, None)
193 def fetch_camera_image(camera_name: str, *, width: int = 256, quality: int = 70) -> RawJpgHsv:
195 return _fetch_camera_image(camera_name, width=width, quality=quality)
196 except exceptions.TimeoutError:
197 logger.warning('Fetching camera image operation timed out.')
198 return RawJpgHsv(None, None, None)
201 if __name__ == '__main__':