Spaces:
Sleeping
Sleeping
| import cv2 | |
| # Input values | |
| image_path = 'tmp/extracted_frame.png' # Replace with your PNG file | |
| output_path = 'frame_with_box_normalized.png' | |
| bbox = (480, 598, 608, 815) | |
| # Load the image | |
| image = cv2.imread(image_path) | |
| if image is None: | |
| raise IOError(f"Could not load image: {image_path}") | |
| img_height, img_width = image.shape[:2] | |
| y_min = int(bbox[0] / 1000.0 * img_height) | |
| x_min = int(bbox[1] / 1000.0 * img_width) | |
| y_max = int(bbox[2] / 1000.0 * img_height) | |
| x_max = int(bbox[3] / 1000.0 * img_width) | |
| bbox_w = x_max - x_min | |
| bbox_h = y_max - y_min | |
| # Draw the normalized bounding box | |
| top_left = (x_min, y_min) | |
| bottom_right = (x_max, y_max) | |
| cv2.rectangle(image, top_left, bottom_right, color=(0, 255, 0), thickness=2) | |
| # Save the image | |
| cv2.imwrite(output_path, image) | |
| print(f"Image with normalized bounding box saved as {output_path}") | |