ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

convert_annotations.py

2021-03-17 18:02:34  阅读:272  来源: 互联网

标签:convert os image py annotation strip path data annotations


'''

import sys
import os
import argparse
import xml.etree.ElementTree as ET
from pathlib import Path


def convert_voc_annotation(data_path, anno_path, use_difficult_bbox=True):

classes = ['camera1',  'camera2']
img_inds_file = 'F:/Object detection/YunYang1994-tensorflow-yolov3-master/scripts/number'#
with open(img_inds_file, 'r') as f:
    txt = f.readlines()  # 读取所有行
    image_inds = [line.strip() for line in txt]  # strip()去除首尾的空格,放入列表中
with open(anno_path, 'a') as f:
    for image_ind in image_inds:
        image_path = os.path.join(data_path, 'JPEGImages', image_ind + '.jpg')#拼接路  径/home/yang/test/VOC/test/VOCdevkit/VOC2007/JPEGImages/000001.jpg
        annotation = image_path
        label_path = os.path.join(data_path, 'labels', image_ind + '.xml')

        root = ET.parse(label_path).getroot()
        objects = root.findall('object')
        for obj in objects:
            difficult = obj.find('difficult').text.strip()
            if (not use_difficult_bbox) and(int(difficult) == 1):
                continue
            bbox = obj.find('bndbox')
            class_ind = classes.index(obj.find('name').text.lower().strip())#找出某个值在列表中的索引位置
            xmin = bbox.find('xmin').text.strip()
            xmax = bbox.find('xmax').text.strip()
            ymin = bbox.find('ymin').text.strip()
            ymax = bbox.find('ymax').text.strip()
            annotation += ' ' + ','.join([xmin, ymin, xmax, ymax, str(class_ind)])#拼接坐标值,48,240,195,371,11 8,12,352,498,14
        print(annotation)
        f.write(annotation + "\n")
return len(image_inds)

if __name__=="__main__":
data_path='F:/Object detection/YunYang1994-tensorflow-yolov3-master/'
anno_path=os.path.join(data_path,"data/dataset/voc1_train.txt")

parser = argparse.ArgumentParser()  # 创建一个解析器
parser.add_argument("--data_path", default='F:/Object detection/YunYang1994-tensorflow-yolov3-master/')  # 添加参数
parser.add_argument("--train_annotation", default="./data/dataset/voc1_train.txt")
parser.add_argument("--test_annotation",  default="./data/dataset/voc_test1.txt")
flags = parser.parse_args()  # 解析参数
path = Path('F:/Object detection/YunYang1994-tensorflow-yolov3-master/labels')
if os.path.exists(flags.train_annotation):os.remove(flags.train_annotation)#如果路径下存在文件,删除文件
if os.path.exists(flags.test_annotation):os.remove(flags.test_annotation)#
num1=convert_voc_annotation(flags.data_path,flags.train_annotation,False)
print(num1)

标签:convert,os,image,py,annotation,strip,path,data,annotations
来源: https://www.cnblogs.com/xyuanzi/p/14550923.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有