python将txt坐标批量打印到原图上
时间:2023-09-01 19:07:00
# -*- coding: utf-8 -*- # liufangtao #批量处理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'\ann\atxt' #txt文件夹 origin_dir = r'\testjyz\ann\bimages' #图片文件夹 target_dir1= r'\testjyz\ann\cresult' #保存地址 class_dist = {0:盘式绝缘子, 1:'棒式绝缘子', 二、复合绝缘子, 三、柱上开关绝缘子, 四、针绝缘子, 五、玻璃绝缘子,六、悬挂棒绝缘子 def divide_img(oriname): img_file = os.path.join(origin_dir, oriname '.jpg') im = cv2.imread(img_file) h, w = im.shape[:2] # print(w) # 图像从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 = open(os.path.join(annota_dir, oriname '.txt')) # 读取每个原始图像xml文件 # print(xml_file) for bbox in xml_file.readlines(): id, x1, y1, x2, y2 = float(bbox.split(' (0), float(bbox.split(' (1), float(bbox.split(' (2), float(bbox.split(' (3), float(bbox.split(' ')[4]) xmin = ((x1*2*w)-x2*w)/2 xmax = ((x1*2*w) x2*w)/2 ymin = ((y1*2*h)-y2*h)/2 ymax = ((y1*2*h) y2*h)/2 # print(xmin, ymin, xmax, ymax) # cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) # 在边界框的两点(左上角、右下角)画矩形,不填充,边框红色,边框像素为5 draw.rectangle(((xmin, ymin), (xmax, ymax)), fill=None, outline='red', width=5) draw.text((xmin, ymin-40), class_dist[id], font=font, fill=(0, 255, 0)) # 转换回OpenCV格式 img_OpenCV = cv2.cvtColor(numpy.asarray(img_PIL),cv2.COLOR_RGB2BGR) img_name = oriname '.jpg' print(img_name) 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'))