I cropped the images in my yolo dataset by drawing roi. The picture was created only within the roi frame whose coordinates were given. When I want to perform object detection with Yolo within this ROI framework, it tags outside of the ROI. How can I perform object detection only in the selected area with ROI?
The code for drawing roi and creating new directories is as follows;
import os
import cv2
import numpy as np
import yaml
x1, y1, x2, y2 = 250, 229, 738, 511 # Örnek koordinatlar
input_base_path = 'yolo_converted'
output_base_path = 'roi'
input_images_train_path = os.path.join(input_base_path, 'images/train')
input_images_val_path = os.path.join(input_base_path, 'images/val')
input_labels_train_path = os.path.join(input_base_path, 'labels/train')
input_labels_val_path = os.path.join(input_base_path, 'labels/val')
output_images_train_path = os.path.join(output_base_path, 'images/train')
output_images_val_path = os.path.join(output_base_path, 'images/val')
output_labels_train_path = os.path.join(output_base_path, 'labels/train')
output_labels_val_path = os.path.join(output_base_path, 'labels/val')
os.makedirs(output_images_train_path, exist_ok=True)
os.makedirs(output_images_val_path, exist_ok=True)
os.makedirs(output_labels_train_path, exist_ok=True)
os.makedirs(output_labels_val_path, exist_ok=True)
def apply_roi(image_path, label_path, output_image_path, output_label_path, points):
image = cv2.imread(image_path)
if image is None:
print(f"Hata: Görüntü okunamadı {image_path}")
return
mask = np.zeros(image.shape[:2], dtype=np.uint8)
cv2.fillPoly(mask, [np.array(points)], 255)
masked_image = cv2.bitwise_and(image, image, mask=mask)
cv2.imwrite(output_image_path, masked_image)
print(f"Görüntü kaydedildi: {output_image_path}")
new_label_lines = []
if os.path.exists(label_path):
with open(label_path, 'r') as label_file:
lines = label_file.readlines()
for line in lines:
cls, x, y, w, h = map(float, line.strip().split())
abs_x, abs_y = x * image.shape[1], y * image.shape[0]
abs_w, abs_h = w * image.shape[1], h * image.shape[0]
box_x1 = abs_x - abs_w / 2
box_y1 = abs_y - abs_h / 2
box_x2 = abs_x + abs_w / 2
box_y2 = abs_y + abs_h / 2
if (x1 <= box_x1 <= x2 and y1 <= box_y1 <= y2 and
x1 <= box_x2 <= x2 and y1 <= box_y2 <= y2):
new_x = (abs_x - x1) / (x2 - x1)
new_y = (abs_y - y1) / (y2 - y1)
new_w = abs_w / (x2 - x1)
new_h = abs_h / (y2 - y1)
new_label_lines.append(f"{cls} {new_x:.6f} {new_y:.6f} {new_w:.6f} {new_h:.6f}n")
with open(output_label_path, 'w') as new_label_file:
new_label_file.writelines(new_label_lines)
print(f"Etiket kaydedildi: {output_label_path}")
else:
print(f"Uyarı: Etiket dosyası bulunamadı {label_path}")
def process_folder(input_images_path, input_labels_path, output_images_path, output_labels_path, points):
for file_name in os.listdir(input_images_path):
image_path = os.path.join(input_images_path, file_name)
label_path = os.path.join(input_labels_path, os.path.splitext(file_name)[0] + '.txt')
output_image_path = os.path.join(output_images_path, file_name)
output_label_path = os.path.join(output_labels_path, os.path.splitext(file_name)[0] + '.txt')
if os.path.isfile(image_path):
print(f"İşleniyor: {image_path}")
apply_roi(image_path, label_path, output_image_path, output_label_path, points)
else:
print(f"Uyarı: Geçersiz dosya {file_name}")
points = [(250, 253), (330, 229), (738, 444), (601, 511)]
print("Train klasörü işleniyor...")
process_folder(input_images_train_path, input_labels_train_path, output_images_train_path, output_labels_train_path, points)
print("Val klasörü işleniyor...")
process_folder(input_images_val_path, input_labels_val_path, output_images_val_path, output_labels_val_path, points)
train_yaml_path = os.path.join(input_base_path, 'train.yaml')
with open(train_yaml_path, 'r') as file:
train_yaml_data = yaml.safe_load(file)
base_path = '/home/ubuntu/yolo/folders/performance_metrics/'
train_yaml_data['train'] = os.path.join(base_path, output_images_train_path)
train_yaml_data['val'] = os.path.join(base_path, output_images_val_path)
output_yaml_path = os.path.join(output_base_path, 'train.yaml')
with open(output_yaml_path, 'w') as yaml_file:
yaml.dump(train_yaml_data, yaml_file)
The training code is as follows;
from ultralytics import YOLO
model = YOLO('yolov8n.pt') # YOLOv8 kullanıyorsanız 'yolov8n.pt' veya YOLOv5 kullanıyorsanız 'yolov5s.pt'
results = model.train(data='roi/train.yaml', epochs=20, imgsz=640, batch=4)
When I train according to these codes, it labels other than ROI.
Seher Elbasan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.