python如何將txt坐標批量打印到原圖上
更新時間:2023年08月16日 16:50:23 作者:慕容洛凝
這篇文章主要介紹了python如何將txt坐標批量打印到原圖上的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
python將txt坐標批量打印到原圖上
# -*- coding: utf-8 -*- # liufangtao #批量處理img和xml文件,根據(jù)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:'棒式絕緣子', 2:'復合絕緣子', 3:'柱上開關(guān)絕緣子', 4:'針式絕緣子', 5:'玻璃絕緣子',6:'懸式棒絕緣子'} 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格式轉(zhuǎn)換成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)) # 轉(zhuǎn)換回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'))
python根據(jù)txt文件批量提取圖片
Python代碼:
根據(jù)txt文件批量提取圖片并保存至另一文件夾,用于深度學習-圖片數(shù)據(jù)預處理
import os import shutil # 根據(jù) 標簽列表/txt文件 批量提取圖片 name_list = open('val.txt', 'r') # print(name_list) # 圖片路徑 tu_dir = 'label' # 保存路徑 save = '/val/label' dir_name = [] # 獲取文件名 for i in name_list: dir_name.append(os.path.basename(i.replace('\n', ''))) # print(dir_name) # 批量復制 for i in dir_name: shutil.copy(tu_dir + '/' + i, save + '/' + i)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Django中的數(shù)據(jù)庫模型類-models.py(一對一的關(guān)系)
今天小編就為大家分享一篇淺談Django中的數(shù)據(jù)庫模型類-models.py(一對一的關(guān)系),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Python實現(xiàn)監(jiān)控程序執(zhí)行時間并將其寫入日志的方法
這篇文章主要介紹了Python實現(xiàn)監(jiān)控程序執(zhí)行時間并將其寫入日志的方法,實例分析了Python日志操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06keras 實現(xiàn)輕量級網(wǎng)絡(luò)ShuffleNet教程
這篇文章主要介紹了keras 實現(xiàn)輕量級網(wǎng)絡(luò)ShuffleNet教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python3安裝pip3(install pip3 for python 3.x)
這篇文章主要為大家詳細介紹了install pip3 for python 3.x,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Flask框架之數(shù)據(jù)交互的實現(xiàn)
本文主要介紹了Flask框架之數(shù)據(jù)交互的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06