三、目标检测xml文件(VOC格式标注文件)的可视化

目标检测xml文件(VOC格式标注文件)的可视化

from xml.dom.minidom import parse
import matplotlib.pyplot as plt
import xml.dom.minidom
import os, shutil
import matplotlib
import numpy as np
import cv2
from PIL import Image, ImageDraw, ImageFont

##########################################################
root = '/home/jojo/VSST/zqz/Object_detection/xml_file_visualization'
# only need to change these
##########################################################

annroot = '/home/jojo/VSST/zqz/Object_detection/xml_file_visualization/groundtruth/Annotations/'
picroot = '/home/jojo/VSST/zqz/Object_detection/xml_file_visualization/groundtruth/JPEGImages/'
anns = os.listdir(annroot)
imgs = os.listdir(picroot)
line_thickness = 6
# labelmap = ["person", "car", "cat", "dog"]
labelmap = ['person', 'rider', 'car', 'truck', 'bus', 'train', 'motorcycle', 'bicycle']

colormap = ['#FF3838', '#FF9D97', '#FF701F', '#FFB21D', '#CFD231', '#48F90A', '#92CC17', '#3DDB86']
# colormap = ["#48F90A", "#FFB21D", "#FF701F", "#CFD231", "pink", "olive", "deeppink", "darkorange", "purple", "cyan", "red",
#             "green", "blue", "yellow", "pink", "olive", "deeppink", "darkorange", "purple", "cyan", "red"]


def mkdir(path):
    folder = os.path.exists(path)
    if not folder:
        os.makedirs(path)

fontPath = "/home/jojo/VSST/zqz/Object_detection/xml_file_visualization/ARIAL.TTF"  # 字体路径
font = ImageFont.truetype(fontPath, 65)
# font = ImageFont.truetype(fontPath, 36)
txt_color=(0, 0, 0)

number = 0
nn = 0
for ann in anns:
    number += 1
    print(number)
    print(ann)
    annpath = annroot + ann
    picpath = picroot + ann.replace("xml", "png")
    im = Image.open(picpath)
    img = cv2.imread(picpath)
    draw = ImageDraw.Draw(im)
    DOMTree = xml.dom.minidom.parse(annpath)
    collection = DOMTree.documentElement
    objects = collection.getElementsByTagName("object")
    labelsss = ""

    for object_ in objects:
        tl = line_thickness or round(0.002 * (im.shape[0] + im.shape[1]) / 2) + 1  # line/font thickness
        a = object_.getElementsByTagName("name")[0].childNodes[0].nodeValue
        k = a.split('.', 1)
        kk = k[0]
        b = str(kk)
        for i in range(0, len(labelmap)):
            label = labelmap[i]
            print(label)

            if b == label:
                nn += 1
                if label not in labelsss:
                    labelsss += label + "_"
                bndboxs = object_.getElementsByTagName("bndbox")
                for bndbox in bndboxs:
                    xmin = bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue
                    ymin = bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue
                    xmax = bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue
                    ymax = bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue
                    xtmp1 = xmin.split('.', 1)
                    xmin1 = xtmp1[0]
                    xtmp2 = xmax.split('.', 1)
                    xmax1 = xtmp2[0]
                    xtmp3 = ymin.split('.', 1)
                    ymin1 = xtmp3[0]
                    xtmp4 = ymax.split('.', 1)
                    ymax1 = xtmp4[0]
                xmin = int(xmin1)
                ymin = int(ymin1)
                xmax = int(xmax1)
                ymax = int(ymax1)
                if xmin < 0:
                    xmin = 0
                if ymin < 0:
                    ymin = 0
                sp = img.shape
                if xmax > sp[1]:
                    xmax = sp[1]
                if ymax > sp[0]:
                    ymax = sp[0]

                roiimg = img[ymin: ymax, xmin:xmax]
                save_op = root + 'check/' + label + "_" + "/"
                mkdir(save_op)
                saveopath = save_op + str(nn) + "_" + ann.replace("xml", "png")
                cv2.imwrite(saveopath, roiimg)
                color = (128, 128, 128)
                c1, c2 = (xmin, ymin), (xmax, ymax)
                tf = max(tl - 1, 1)  # label字体的线宽 font thickness
                t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
                c3 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
                draw.rectangle((xmin, ymin, xmax, ymax), outline=colormap[i])
                draw.rectangle((xmin - 1, ymin - 1, xmax - 1, ymax - 1), outline=colormap[i])
                draw.rectangle((xmin + 1, ymin + 1, xmax + 1, ymax + 1), outline=colormap[i])
                draw.rectangle((xmin - 2, ymin - 2, xmax - 2, ymax - 2), outline=colormap[i])
                draw.rectangle((xmin + 2, ymin + 2, xmax + 2, ymax + 2), outline=colormap[i])
                draw.rectangle((xmin - 3, ymin - 3, xmax - 3, ymax - 3), outline=colormap[i])
                draw.rectangle((xmin + 3, ymin + 3, xmax + 3, ymax + 3), outline=colormap[i])
                draw.rectangle((c1, c3), fill=colormap[i], outline=colormap[i])
                # cv2.rectangle(im, c1, c3, color, -1, cv2.LINE_AA)
                # draw.text((xmin - 1, ymin - 35), label, fill=(255, 255, 255), font=font)
                draw.text((xmin - 1, ymin - 60), label, fill=(255, 255, 255), font=font)
                break

        label_has = 0
        for label in labelmap:
            if b != label:
                label_has = 1
        if not label_has:
            print(ann + "======" + b + "============================")


    save_p = root + 'check/' + labelsss + "/"
    savepath = save_p + ann.replace("xml", "png")
    mkdir(save_p)
    im.save(savepath)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值