Saving crops from yolo model inference

oad the image
frame = cv2.imread(image_path)

Check if the image was successfully loaded

if frame is None:
print(f"Error: Could not load image from {image_path}")
exit(1)

Resize the frame to fit the screen resolution if it’s too large

screen_width = 1280
screen_height = 720
(h, w) = frame.shape[:2]
if w > screen_width or h > screen_height:
scale = min(screen_width / w, screen_height / h)
new_width = int(w * scale)
new_height = int(h * scale)
frame = cv2.resize(frame, (new_width, new_height))

Run YOLO model inference on the frame

results = model(frame)

Annotate the frame with the detection results

annotated_frame = results[0].plot() # Plot detections on the frame

Optionally, display the frame (press ‘q’ to exit)

cv2.imshow(‘YOLOv8 Inference’, annotated_frame)

Wait for the user to press ‘q’ to exit the display

while True:
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break

Release resources

cv2.destroyAllWindows()

this is the detected bounding boxes. Now i want to save the crop from this bouding boxes. Give me solutions

  1. Please don’t demand answers, “Give me solutions” is not a considerate way to ask for assistance.
  2. You should always check the documentation for existing capabilities or examples, as there are many already outlined (including this one). If you have trouble finding an answer in the documentation, try using the search feature :mag: to locate what you’re looking for.
  3. You’ll find what you’re looking for under inference arguments in the documentation. You can use the argument save_crop=True when executing inference with your model.