08目标检测数据集半自动化标注

深度学习目标检测通常需要较大的数据规模,但是数据集的标注通常是费时费力但又意义不大的一件事,我们可以先标注三五百张图片,然后训练一个较为初步的模型,然后使用这个模型对未标注的图像进行推理,将推理结果导出为voc格式,再将图片和标注文件使用本地图像标注软件labelimg打开,手动调整边框位置后,标注文件即可作为数据集用于后续的模型训练。

推理

!python /home/aistudio/PaddleDetection/tools/infer.py \
	-c /home/aistudio/PaddleDetection/configs/cascade_rcnn/cascade_rcnn_r50_fpn_1x_coco.yml \
	--draw_threshold=0.5 \
	--infer_dir=img \
	--output_dir=toolinfer \
	--use_vdl=True \
	--save_txt=True \
	-o weights=/home/aistudio/output/cascade_rcnn_r50_fpn_1x_coco/model_final

将推理结果保存到txt文件中

推理结果修改为voc格式标注


在这里插入图片描述
在这里插入图片描述

将产生的推理结果文件移动到指定文件夹中,将对应的坐标信息进行修改对应,生成voc格式的xml标注文件。

转换代码

import os
import cv2

headstr = """\
<annotation>
    <folder>VOC</folder>
    <filename>%s</filename>
    <source>
        <database>My Database</database>
        <annotation>VOC</annotation>
        <image>flickr</image>
        <flickrid>NULL</flickrid>
    </source>
    <owner>
        <flickrid>NULL</flickrid>
        <name>company</name>
    </owner>
    <size>
        <width>%d</width>
        <height>%d</height>
        <depth>%d</depth>
    </size>
    <segmented>0</segmented>
"""
objstr = """\
    <object>
        <name>%s</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>%d</difficult>
        <bndbox>
            <xmin>%d</xmin>
            <ymin>%d</ymin>
            <xmax>%d</xmax>
            <ymax>%d</ymax>
        </bndbox>
    </object>
"""
tailstr = '''\
</annotation>
'''


def save_annotations(boxes, img, filename):
    H = img.shape[0]
    W = img.shape[1]
    C = img.shape[2]
    # H,W,C = img.shape
    img_name = filename.split('.')[0] + '.bmp'
    head = headstr % (img_name, W, H, C)  # 写入头文件
    tail = tailstr  # 写入尾文件
    # 写入boxes
    save_path = anno_path + filename.split('.')[0] + '.xml'
    f = open(save_path, 'w')
    f.write(head)
    for box in boxes:
        f.write(objstr % (str(box[0]), 0, float(box[2]), float(box[3]), float(box[2]) + float(box[4]), float(box[3]) + float(box[5])))
    f.write(tail)


if __name__ == '__main__':
    # 设置路径
    root_path = './'
    total_label_path = root_path + 'txt/'  # txt存储的路径
    total_img_path = root_path + 'img/'  # 图像存储路径
    anno_path = root_path + 'Annotations/'  # 存储生成的xml标注文件
    # 判断当前路径下是否存在Annotations这个文件夹,若不存在,自动创建一个
    if not os.path.exists(anno_path):
        os.mkdir(anno_path)
    # 逐个读取txt标注文件
    for filename in os.listdir(total_label_path):
        cur_label_path = total_label_path + filename
        cur_img_path = total_img_path + filename.split('.')[0] + '.bmp'  # 换一下文件名后缀
        cur_boxes = []
        # 读取当前txt文件中的内容
        with open(cur_label_path, 'r') as file:
            while True:
                line = file.readline().strip()  # .strip()用来去掉'\r,\n'
                if not line:
                    break
                line_list = [ele for ele in line.split(' ')]
                cur_boxes.append(line_list)
        # 读取当前图像
        cur_img = cv2.imread(cur_img_path)
        # 进行xml文档存储
        save_annotations(cur_boxes, cur_img, filename)

需要修改的地方:

  1. txt文件存放的路径total_label_path
  2. 图像存放的路径total_img_path
  3. 创建存放voc格式的文件夹anno_path
  4. 图像文件的后缀名bmp

注意事项:

  1. 一般需要三五百张初始标注完成的图像
  2. 尽量使用高精度的目标检测算法完成预训练
  3. 根据预测结果与真实目标物的情况,合理选择推理阈值。如果漏检较多,则降低置信度阈值;若误检较多,则提高置信度阈值。
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值