| 215 | |
| 216 | # Resize frame to requested resolution in pixels |
| 217 | def __resizeFrame(self, frame, resolution): |
| 218 | frame_h = frame.shape[0] |
| 219 | frame_w = frame.shape[1] |
| 220 | |
| 221 | # Calculate requested aspect ratio (width/height): |
| 222 | crop_aspect_ratio = resolution[0] / resolution[1] |
| 223 | |
| 224 | if crop_aspect_ratio != (frame_w / frame_h): |
| 225 | # Crop into image with resize aspect ratio |
| 226 | crop_w = int(frame_h * crop_aspect_ratio) |
| 227 | crop_h = int(frame_w / crop_aspect_ratio) |
| 228 | |
| 229 | if crop_w > frame_w: |
| 230 | # Crop top and bottom part of the image |
| 231 | top = (frame_h - crop_h) // 2 |
| 232 | bottom = top + crop_h |
| 233 | frame = frame[top : bottom, 0 : frame_w] |
| 234 | elif crop_h > frame_h: |
| 235 | # Crop left and right side of the image`` |
| 236 | left = (frame_w - crop_w) // 2 |
| 237 | right = left + crop_w |
| 238 | frame = frame[0 : frame_h, left : right] |
| 239 | else: |
| 240 | # Crop to the center of the image |
| 241 | left = (frame_w - crop_w) // 2 |
| 242 | right = left + crop_w |
| 243 | top = (frame_h - crop_h) // 2 |
| 244 | bottom = top + crop_h |
| 245 | frame = frame[top : bottom, left : right] |
| 246 | logger.debug(f"Frame cropped from ({frame_w}, {frame_h}) to ({frame.shape[1]}, {frame.shape[0]})") |
| 247 | |
| 248 | logger.debug(f"Resize frame from ({frame.shape[1]}, {frame.shape[0]}) to ({resolution[0]}, {resolution[1]})") |
| 249 | try: |
| 250 | frame = cv2.resize(frame, resolution) |
| 251 | except Exception as e: |
| 252 | logger.error(f"Error in resizeFrame(): {e}") |
| 253 | |
| 254 | return frame |
| 255 | |
| 256 | # Change color space of a frame from BGR to selected profile |
| 257 | def __changeColorSpace(self, frame, color_space): |