Skip to content

Instantly share code, notes, and snippets.

@pAkalpa
Created March 21, 2023 18:08
Show Gist options
  • Save pAkalpa/18c1fb23e499348fec724c88f3e0c8ae to your computer and use it in GitHub Desktop.
Save pAkalpa/18c1fb23e499348fec724c88f3e0c8ae to your computer and use it in GitHub Desktop.
Crop Images using Yolo Model Output
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