Created
March 21, 2023 18:08
-
-
Save pAkalpa/18c1fb23e499348fec724c88f3e0c8ae to your computer and use it in GitHub Desktop.
Crop Images using Yolo Model Output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
# Get bounding boxes from the model | |
results = model(input_img) | |
boxes = results[0].boxes | |
# Iterate over all boxes and crop image segments | |
cropped_images = [] | |
for box in boxes: | |
# Get the coordinates of the box in the format (xmin, ymin, xmax, ymax) | |
xmin, ymin, xmax, ymax = box.xyxy | |
# Crop the image segment using the box coordinates | |
cropped_img = input_img[int(ymin):int(ymax), int(xmin):int(xmax)] | |
# Append the cropped image segment to the list of cropped images | |
cropped_images.append(cropped_img) | |
# Convert the list of cropped images to a NumPy array | |
cropped_images = np.array(cropped_images) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment