import cv2 def check_channels(image): channels = image.shape[2] if len(image.shape) == 3 else 1 if channels == 1: image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) elif channels > 3: image = image[:, :, :3] return image def resize_image(img, max_length=768): height, width = img.shape[:2] max_dimension = max(height, width) if max_dimension > max_length: scale_factor = max_length / max_dimension new_width = int(round(width * scale_factor)) new_height = int(round(height * scale_factor)) new_size = (new_width, new_height) img = cv2.resize(img, new_size) height, width = img.shape[:2] img = cv2.resize(img, (width-(width % 64), height-(height % 64))) return img