目标检测02_标签文件可视化

代码实测可用

import os
import cv2 as cv
import xml.etree.ElementTree as ET


def xml_to_jpg(imgs_path, xmls_path, out_path):
    imgs_list = os.listdir(imgs_path)  # 读取图片列表
    xmls_list = os.listdir(xmls_path)  # 读取xml列表
    if len(imgs_list) <= len(xmls_list):  # 若图片个数小于或等于xml个数,从图片里面找与xml匹配的
        print("标记点1")
        for imgName in imgs_list:
            temp1 = imgName.split('.')[0]  # 图片名 例如123.jpg 分割之后 temp1 = 123
            temp1_ = imgName.split('.')[1]  # 图片后缀
            if temp1_ != 'jpg' and temp1_ != 'jpeg':
                continue
            for xmlName in xmls_list:  # 遍历xml列表
                temp2 = xmlName.split('.')[0]  # xml名
                temp2_ = xmlName.split('.')[1]
                if temp2_ != 'xml':
                    continue
                if temp2 != temp1:  # 判断图片名与xml名是否相同
                    continue
                else:  # 不同的话 开始读取xml坐标信息
                    img_path = os.path.join(imgs_path, imgName)
                    xml_path = os.path.join(xmls_path, xmlName)
                    img = cv.imread(img_path)
                    labelled = img
                    root = ET.parse(xml_path).getroot()
                    for obj in root.iter('object'):
                        bbox = obj.find('bndbox')
                        xmin = int(bbox.find('xmin').text.strip())
                        ymin = int(bbox.find('ymin').text.strip())
                        xmax = int(bbox.find('xmax').text.strip())
                        ymax = int(bbox.find('ymax').text.strip())
                        labelled = cv.rectangle(labelled, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)
                    cv.imwrite(out_path + '\\' + imgName, labelled)
                    break
    else:  # 若xml个数小于图片个数,从xml里面找与图片匹配的。下面操作与上面差不多
        print("标记点2")
        for xmlName in xmls_list:
            temp1 = xmlName.split('.')[0]
            temp1_ = xmlName.split('.')[1]
            if temp1_ != 'xml':
                continue
            for imgName in imgs_list:
                temp2 = imgName.split('.')[0]
                temp2_ = imgName.split('.')[1]  # 图片后缀
                if temp2_ != 'jpg':
                    continue
                if temp2 != temp1:
                    continue
                else:
                    img_path = os.path.join(imgs_path, imgName)
                    xml_path = os.path.join(xmls_path, xmlName)

                    img = cv.imread(img_path)
                    labelled = img
                    root = ET.parse(xml_path).getroot()

                    for obj in root.iter('object'):
                        bbox = obj.find('bndbox')
                        xmin = int(bbox.find('xmin').text.strip())
                        ymin = int(bbox.find('ymin').text.strip())
                        xmax = int(bbox.find('xmax').text.strip())
                        ymax = int(bbox.find('ymax').text.strip())
                        labelled = cv.rectangle(labelled, (xmin, ymin), (xmax, ymax), (0, 0, 255), 1)

                    print(out_path + imgName)
                    cv.imwrite(out_path + imgName, labelled)

                    break


if __name__ == '__main__':
    imgs_path = './VOC2007/JPEGImages'  # 图片路径
    xmls_path = './VOC2007/Annotations'  # xml路径
    retangele_img_path = './pic_out/'  # 保存画框的路径
    xml_to_jpg(imgs_path, xmls_path, retangele_img_path)

猜你喜欢

转载自blog.csdn.net/weixin_49321128/article/details/125339329