33、将Resnet32数据集转成nanodet的数据集

基本思想:因为手中有一份Resnet32的数据集,是以文件夹命名的分类图片,想用检测的模型进行训练一下,因为每张图图片的尺寸大小固定,且每个图片的目标位置大概都是固定的,逐写一个py代码构建一个标注文件xml即可

33、将Resnet32数据集转成nanodet的数据集_第1张图片

 生成数据集代码

# encoding:utf-8

from xml.dom.minidom import Document
import os
import cv2
dataDir=r"F:\sxj\20191229_zhou_2\fist_self_training\fist_datasets\fist"

for item in os.listdir(dataDir):
    if item.endswith("jpg"):
        last_dataDir = os.path.basename(dataDir)
        all_path=os.path.join(dataDir,item)
        image=cv2.imread(all_path)
        image_width,image_height,_ = image.shape


        (filename, extension) = os.path.splitext(item)
        xml_name=filename+".xml"
        all_path_name_xml = os.path.join(dataDir, xml_name)
        doc = Document()  # 创建DOM文档对象truncated
        DOCUMENT = doc.createElement('annotation')  # 创建根元素

        floder = doc.createElement('floder')  ##建立自己的开头
        floder_text = doc.createTextNode('JPEGImages')  ##建立自己的文本信息
        floder.appendChild(floder_text)  ##自己的内容
        DOCUMENT.appendChild(floder)
        doc.appendChild(DOCUMENT)

        filename = doc.createElement('filename')
        filename_text = doc.createTextNode(item)
        filename.appendChild(filename_text)
        DOCUMENT.appendChild(filename)
        doc.appendChild(DOCUMENT)

        path = doc.createElement('path')
        path_text = doc.createTextNode(item)
        path.appendChild(path_text)
        DOCUMENT.appendChild(path)
        doc.appendChild(DOCUMENT)

        source = doc.createElement('source')
        database = doc.createElement('database')
        database_text = doc.createTextNode('Unknow')  # 元素内容写入
        database.appendChild(database_text)
        source.appendChild(database)
        DOCUMENT.appendChild(source)
        doc.appendChild(DOCUMENT)

        size = doc.createElement('size')
        width = doc.createElement('width')
        width_text = doc.createTextNode(str((image_width)))  # 元素内容写入
        width.appendChild(width_text)
        size.appendChild(width)

        height = doc.createElement('height')
        height_text = doc.createTextNode(str(image_height))
        height.appendChild(height_text)
        size.appendChild(height)

        depth = doc.createElement('depth')
        depth_text = doc.createTextNode('3')
        depth.appendChild(depth_text)
        size.appendChild(depth)

        DOCUMENT.appendChild(size)

        segmented = doc.createElement('segmented')
        segmented_text = doc.createTextNode('0')
        segmented.appendChild(segmented_text)
        DOCUMENT.appendChild(segmented)
        doc.appendChild(DOCUMENT)

        object = doc.createElement('object')
        name = doc.createElement('name')
        name_text = doc.createTextNode(last_dataDir)
        name.appendChild(name_text)
        object.appendChild(name)

        pose = doc.createElement('pose')
        pose_text = doc.createTextNode('Unspecified')
        pose.appendChild(pose_text)
        object.appendChild(pose)

        truncated = doc.createElement('truncated')
        truncated_text = doc.createTextNode('0')
        truncated.appendChild(truncated_text)
        object.appendChild(truncated)

        difficult = doc.createElement('difficult')
        difficult_text = doc.createTextNode('0')
        difficult.appendChild(difficult_text)
        object.appendChild(difficult)


        bndbox = doc.createElement('bndbox')
        xmin = doc.createElement('xmin')
        xmin_text = doc.createTextNode('35')
        xmin.appendChild(xmin_text)
        bndbox.appendChild(xmin)

        ymin = doc.createElement('ymin')
        ymin_text = doc.createTextNode('35')
        ymin.appendChild(ymin_text)
        bndbox.appendChild(ymin)

        xmax = doc.createElement('xmax')
        xmax_text = doc.createTextNode(str(image_width-35))
        xmax.appendChild(xmax_text)
        bndbox.appendChild(xmax)

        ymax = doc.createElement('ymax')
        ymax_text = doc.createTextNode(str(image_height-35))
        ymax.appendChild(ymax_text)
        bndbox.appendChild(ymax)
        object.appendChild(bndbox)

        DOCUMENT.appendChild(object)

        ########### 将DOM对象doc写入文件
        with open(all_path_name_xml, 'w') as f:
            doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')

你可能感兴趣的:(Python基础知识,git,json,opencv)