How to do object detection in roi framework

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.

New contributor

Seher Elbasan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật