How to implement ROI support in detect.py #13438
sergey99si
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How to implement ROI support in detect.py:
Add a new argument for ROI.
In the detect.py file, you need to add an argument to specify the coordinates of the ROI. This can be done using the argparse library.
Filter the detected objects by ROI.
After getting the results from the YOLO model, add a check to see if the objects' frame coordinates fall within the specified region.
add
detect.py
parser.add_argument('--roi', type=str, default=None, help='Region of interest (ROI) in the format"x_min,y_min,x_max,y_max"')
add
detect.py
def parse_roi(roi_str):if roi_str:
coords = list(map(int, roi_str.split(',')))
if len(coords) == 4:
return coords # [x_min, y_min, x_max, y_max]
else:
raise ValueError('ROI should have 4 integers: x_min,y_min,x_max,y_max')
return None
roi = parse_roi(opt.roi)
for i, det in enumerate(pred): # Перебор результатов
if len(det):
for *xyxy, conf, cls in reversed(det):
x_min, y_min, x_max, y_max = map(int, xyxy)
if roi: # Фильтруем только объекты в ROI
if x_min < roi[0] or y_min < roi[1] or x_max > roi[2] or y_max > roi[3]:
continue # Пропускаем объекты вне ROI
Arguments for ROI in the new script:
--roi x_min,y_min,x_max,y_max: sets the region of interest where objects will be saved.
--save-crop: saves cut images of found objects.
--save-txt: saves a text file with the results.
example:python detect.py --source image.jpg --roi 100,100,500,500 --save-crop
Beta Was this translation helpful? Give feedback.
All reactions