Skip to content

Is it possible to change the order of detection from top to bottom? #18228

Answered by glenn-jocher
Flavien123 asked this question in Q&A
Discussion options

You must be logged in to vote

@Flavien123 certainly! Here's a Python example to reorder the crops from right to left and top to bottom based on their bounding box coordinates:

from pathlib import Path
from ultralytics import YOLO

model = YOLO("path/to/your/custom_model.pt")
results = model("path/to/your/image.jpg")

# Define save directory
save_dir = Path("path/to/save/crops")
save_dir.mkdir(parents=True, exist_ok=True)

# Extract boxes and sort (right to left, top to bottom)
detections = results[0].boxes
sorted_boxes = sorted(detections, key=lambda x: (-x.xyxy[2], x.xyxy[1]))  # Sort by x2 descending, then y1 ascending

# Save crops in the specified order
for i, box in enumerate(sorted_boxes):
    save_path = save_dir 

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@Flavien123
Comment options

@glenn-jocher
Comment options

Answer selected by Flavien123
@Flavien123
Comment options

@glenn-jocher
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested detect Object Detection issues, PR's
3 participants