从CCPD数据集中生成标注文件(xml)

CCPD(china city parking dataset)
来源:由中科大的科研人员构建
作用:用于车牌识别的大型国内的数据集
下载链接:https://github.com/detectRecog/CCPD
![https://img-blog.csdnimg.cn/20200611093437897.jpg)
结构:
``在这里插入图片描述
图片如下(分辨率720x1160):
在这里插入图片描述
我的路径如下:
在这里插入图片描述

代码如下:

import os
import re
import cv2
root_path = r'D:\CHEPAI\CCPD2019_1/'
file_name = os.listdir(root_path)
class_name = 'plate'
xml_dir =r'D:\CHEPAI\Annotations/'  # 保存xml的目录
a= 0
for i in file_name:
    if os.path.exists((xml_dir + i)):
        print('已经存在文件%s'%(xml_dir + i))
    else:os.mkdir((xml_dir + i))
    file_name1 = os.listdir(os.path.join(root_path,i))
    for ii in file_name1:
        if os.path.exists(xml_dir + i + '/'+ ii.split('.')[0]+'.xml'):
            continue
        #print(root_path+i+'/' + ii)
        img = cv2.imread((root_path+i+'/'+ii))
        print((root_path+i+'/'+ii))
        if img is None:
            print('文件%s读取失败'%ii)
            continue

        height = img.shape[0]
        width = img.shape[1]
        depth = img.shape[2]
        point = ii.split('.')[0].split('-')[3]
        #Xmin = point.split('_')[2].split('&')[0]
        num  = re.findall('\d+\d*',point)  #  正则表达式 从字符串中提取数值
        Xmin =min(num[0::2])  # list[start:stop:step]
        Ymin = min(num[1::2])
        Xmax = max(num[0::2])
        Ymax = max(num[1::2])
        #print(ii.split('&'))
        fname = ii.split('&')[0] + '&'+ii.split('&')[1]+ '&'+ii.split('&')[2]+ '&'+ii.split('&')[3]+ '&'+ii.split('&')[4]+ '&'+ii.split('&')[5]+ '&'+ii.split('&')[1]+ '&'+ii.split('&')[6]
        xml_str = "<annotation>\n\t\
                <folder>"+ i+ "</folder>\n\t\
                <filename>" + fname + "</filename>\n\t\
                " + "<path>" + root_path + i + '/'+fname+ "</path>\n\t\
                <source>\n\t\t\
                <database>Unknown</database>\n\t\
                </source>\n\t\
                <size>\n\t\t\
                <width>" + str(width) + "</width>\n\t\t\
                <height>" + str(height) + "</height>\n\t\t\
                <depth>" + str(depth) + "</depth>\n\t\
                </size>\n\t\
                <segmented>0</segmented>"
        obj_str = "\n\t\
                    <object>\n\t\t\
                    <name>" + class_name + "</name>\n\t\t\
                    <pose>Unspecified</pose>\n\t\t\
                    <truncated>0</truncated>\n\t\t\
                    <difficult>0</difficult>\n\t\t\
                    <bndbox>\n\t\t\t\
                    <xmin>" + str(Xmin) + "</xmin>\n\t\t\t\
                    <ymin>" + str(Ymin) + "</ymin>\n\t\t\t\
                    <xmax>" + str(Xmax) + "</xmax>\n\t\t\t\
                    <ymax>" + str(Ymax) + "</ymax>\n\t\t\
                    </bndbox>\n\t\
                    </object>"
        xml_str += obj_str
        xml_str +="\n</annotation>\n"
        with open(xml_dir + i + '/'+ ii.split('.')[0]+'.xml','w') as f:
            f.write(xml_str)
            a += 1
            print('成功读写文件夹%s第%d'%(i,a))
            f.close()
print('end')


  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值