资讯详情

将xml坐标批量打印到原图上(中文显示名字)

# -*- coding: utf-8 -*- #批量处理img和xml文件,根据xml文件中的坐标把img标记目标并保存到指定文件夹中,以便检查目标标记是否准确。 import xml.etree.ElementTree as ET import os, cv2 import numpy  from tqdm import tqdm from PIL import Image, ImageDraw, ImageFont  annota_dir = r'U:\datas\testjyz\xmls1' origin_dir = r'U:\datas\testjyz\images1' target_dir1=r'U:\datas\testjyz\c'  def divide_img(oriname):     img_file = os.path.join(origin_dir, oriname   '.jpg')     im = cv2.imread(img_file)     # 图像从OpenCV格式转换成PIL格式      img_PIL = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))     draw = ImageDraw.Draw(img_PIL)     #字体路径和大小     font = ImageFont.truetype('SimHei.ttf', 40)      xml_file = os.path.join(annota_dir, oriname   '.xml')  # 读取每个原始图像xml文件     tree = ET.parse(xml_file)     root = tree.getroot()     a=[]     for object in root.findall('object#输出坐标左上角和右下角         object_name = object.find('name').text         Xmin = int(object.find('bndbox').find('xmin').text)         Ymin = int(object.find('bndbox').find('ymin').text)         Xmax = int(object.find('bndbox').find('xmax').text)         Ymax = int(object.find('bndbox').find('ymax').text)         # 在边界框的两点(左上角、右下角)画矩形,没有填充,边框是红色的,边框像素是5         draw.rectangle(((Xmin, Ymin), (Xmax, Ymax)), fill=None, outline='yellow', width=5)         #把每个xml的object名字存储在列表中         a.append(object_name)         class_dist = {'jyz_tc_bs棒式绝缘子, 'jyz_tc_ps盘式绝缘子, 'jyz_bl_ps玻璃绝缘子, 'jyz_tc_xs悬挂绝缘子, 'jyz_zs针绝缘子, 'zskg_jyzjyz_fh     #读图片,将其打印成矩形框和中文         for i in a:             pass         draw.text((Xmin, Ymin-40), class_dist[i], font=font, fill=(255, 0, 0))      # 转换回OpenCV格式      img_OpenCV = cv2.cvtColor(numpy.asarray(img_PIL),cv2.COLOR_RGB2BGR)      img_name = oriname   '.jpg'     to_name = os.path.join(target_dir1, img_name)     cv2.imwrite(to_name, img_OpenCV)  img_list = os.listdir(origin_dir) for name in img_list:     divide_img(name.rstrip('.jpg')) 

标签: sst棒式氧化锆氧传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台