Python 目标检测 YOLO 去除过小的目标标签

import os
import cv2

# train 训练集上,通过过滤宽高小于等于的15的目标,过滤掉的数目是[65857, 15275, 38775, 1372, 15, 0]
# val 训练集上,通过过滤宽高小于等于的15的目标,过滤掉的数目是[9664, 2755, 3705, 343, 15, 0]

img_path = "/data/***/datasets/ktxx-auth/images/train/"
label_path = "/data/***/datasets/ktxx-auth/labels/train/"
label_path_new = "/data/***/datasets/ktxx-auth/labels/train-clean/"
label_list = os.listdir(label_path)
count = [0, 0, 0, 0, 0, 0]
# labels = ['person', 'dogcart', 'car', 'tricycle', 'plate', 'face']
labels = ['person', 'car', 'plate', 'face']
for label_file in label_list:
    image = cv2.imread(os.path.join(img_path, label_file[0:-4] + ".jpg"))
    img_w, img_h = image.shape[1], image.shape[0]
    if label_file.endswith(".txt"):
        with open(os.path.join(label_path, label_file), "r", encoding="utf-8") as f1, open(os.path.join(label_path_new, label_file), "w", encoding="utf-8") as f2:
            for line in f1:
                items = line.split(" ")
                cls = int(items[0])
                xmin = (float(items[1]) - float(items[3]) / 2.) * img_w
                xmax = (float(items[1]) + float(items[3]) / 2.) * img_w
                ymin = (float(items[2]) - float(items[4]) / 2.) * img_h
                ymax = (float(items[2]) + float(items[4]) / 2.) * img_h
                if xmax - xmin <= 30 or ymax - ymin <= 15:  # 在这里修改要过滤掉的目标的大小
                    count[cls] += 1
                    print("In {} clean {}, it's location is {}.".format(label_file, labels[cls], " ".join(items[1:5])))
                    continue
                line = " ".join(items)
                f2.write(line)
print("has cleaned instance:")
print(count)

注意根据自己想过滤的目标进行修改,本人这里设置的是小于宽30or高15的目标

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值