目錄
1 事先準(zhǔn)備的文件:
1.1 需要訓(xùn)練的圖片
1.2 標(biāo)注的xml文件
2 功能:
3 用法:
4 注意事項(xiàng):
1 事先準(zhǔn)備的文件:
1.1 需要訓(xùn)練的圖片
? ? ? ?要求路徑中有一個(gè)子目錄名稱是 images (或 imgs ) ,訓(xùn)練程序需要通過 /images/ (或 /imgs/ ) ?替換為 /labels/ 找同名.txt文件提取標(biāo)注數(shù)據(jù)
? ? ? ?默認(rèn)情況下,yolov5是 images, yolov4是 imgs
? ? ? ?圖片無需拷貝到y(tǒng)olo目錄
1.2 標(biāo)注的xml文件
# 支持xml格式 或 voc 格式,可用標(biāo)注精靈或labelimg。后者是python程序,但連續(xù)標(biāo)注效率高,推薦使用
2 功能:
? ? 1)將標(biāo)注數(shù)據(jù)轉(zhuǎn)為yolo格式,保存到 <labeltxtpath>/<img>.txt
# <labeltxtpath>=<imgpath> 中最后一個(gè) images (或 imgs ) 換為 labels,擴(kuò)展名改為txt
? ? 2)生成 data/obj.names,類別名稱列表
? ? 3)創(chuàng)建清單文件 data/train.txt,val.txt,test.txt
# 三者按指定比例隨機(jī)比例, 不重復(fù)。5000-1萬以上的圖片,建議val 500左右,test 100 左右
3 用法:
1) 測試、調(diào)整圖片配比
?python make_label_yolo.py --runtest yes --imgpath E:\models\test1\images --labelxmlpath E:\models\test1\xmls --train_percent 0.9 --val_percent 0.08 --test_percent 0.02
2) 正式轉(zhuǎn)換
?python make_label_yolo.py --imgpath E:\models\test1\images --labelxmlpath E:\models\test1\xmls --train_percent 0.9 --val_percent 0.08 --test_percent 0.02
4 注意事項(xiàng):
#1)忽略 images 和 labelsxml 以.開頭的文件;
#2)labelsxml中的標(biāo)簽文件,只有在 imgpath 存在同名圖片才采用。
#3)需要訓(xùn)練的圖片無需拷貝到 <yolo>/data/images
#4)根據(jù)yolo要求,txt標(biāo)注文件生成在實(shí)際圖片 images (或 imgs ) 并列目錄 labels
#5)訓(xùn)練用到的文件清單描述文件,保存在 <yolo>/data/train.txt, val.txt, test.txt
#6)跳過寬w或高h(yuǎn)小于指定大小的標(biāo)注
#7)train/訓(xùn)練,val/校準(zhǔn),test/測驗(yàn)配比默認(rèn)為 0.9 0.08 0.02,命令行參數(shù)修改
#8)視頻提取的圖片文件名稱帶 grp_ 前綴,這些文件不參與訓(xùn)練時(shí)校準(zhǔn)val、測驗(yàn)test
?
腳本文件1: make_label_yolo.py,轉(zhuǎn)換格式、隨機(jī)分配 train val test清單
?
# 事先準(zhǔn)備的文件:
# 需要訓(xùn)練的圖片
# 要求路徑中有一個(gè)子目錄名稱是 images (或 imgs ) ,訓(xùn)練程序需要通過 /images/ (或 /imgs/ ) 替換為 /labels/ 找同名.txt文件提取標(biāo)注數(shù)據(jù)
# 默認(rèn)情況下,yolov5是 images, yolov4是 imgs
# 圖片無需拷貝到y(tǒng)olo目錄
# 標(biāo)注的xml文件 # xml格式 或 voc 格式
# 功能:
# 1)將標(biāo)注數(shù)據(jù)轉(zhuǎn)為yolo格式,保存到 <labeltxtpath>/<img>.txt # <labeltxtpath>=<imgpath> 中最后一個(gè) images (或 imgs ) 換為 labels,擴(kuò)展名改為txt
# 3)生成 data/obj.names
# 4)創(chuàng)建清單文件 data/train.txt,val.txt,test.txt # 三者分配比例由命令行參數(shù)確定, 不重復(fù)
# 5)坐標(biāo)越界的標(biāo)注,會自動糾正,所以不應(yīng)擔(dān)心中間異常數(shù)據(jù)導(dǎo)致中途退出
# 用法:
# 測試、調(diào)整圖片配比
# python make_label_yolo.py --runtest yes --imgpath E:\models\test1\images --labelxmlpath E:\models\test1\xmls --train_percent 0.9 --val_percent 0.08 --test_percent 0.02
# 正式轉(zhuǎn)換
# python make_label_yolo.py --imgpath E:\models\test1\images --labelxmlpath E:\models\test1\xmls --train_percent 0.9 --val_percent 0.08 --test_percent 0.02#注意事項(xiàng):
#1)忽略 images 和 labelsxml 以.開頭的文件;
#2)labelsxml中的標(biāo)簽文件,只有在 imgpath 存在同名圖片才采用。
#3)需要訓(xùn)練的圖片無需拷貝到 <yolo>/data/images
#4)根據(jù)yolo要求,txt標(biāo)注文件生成在實(shí)際圖片 images (或 imgs ) 并列目錄 labels
#5)訓(xùn)練用到的文件清單描述文件,保存在 <yolo>/data/train.txt, val.txt, test.txt
#6)跳過寬w或高h(yuǎn)小于指定大小的標(biāo)注
#7)train/訓(xùn)練,val/校準(zhǔn),test/測驗(yàn)配比默認(rèn)為 0.9 0.08 0.02,命令行參數(shù)修改
#8)視頻提取的圖片文件名稱帶 grp_ 前綴,這些文件不參與訓(xùn)練時(shí)校準(zhǔn)val、測驗(yàn)test# 入?yún)?see def parse_opt(known=False):import time
import xml.etree.ElementTree as ET
import os
import random
# from os import listdir, getcwd
# from os.path import join
from pathlib import Path
import argparse
import sysFILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
if str(ROOT) not in sys.path:sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative# IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp' # include image suffixes
file_ext_sets = ['.jpg', '.jpeg','.png','.bmp','.tif','.tiff','.ppm','.webp']
clsfilename="data/obj.names"
g_fileprefix="grp_"label_totalnum=0
label_oldfilenum=0
label_newfilenum=0
label_skipnum=0classes = []
gPicFileList= []
gPicFilemap= {}
# 不存在xml標(biāo)注文件的圖片文件名稱,make_yololabel()
skipPicList_nolabelxml=[] # print(f"not exist xmlfile={xmlfile}, p={lastps}")
# 沒有合格標(biāo)簽而被刪除標(biāo)簽文件的圖片文件名稱,make_yololabel() --> convert_annotation()
skipPicList_annotation=[] # print(f"skip {image_filename} when convert_annotation, no suitable labels ")
# 不存在標(biāo)簽文件的圖片文件名稱, make_trainvaltest_list()
skipPicList_nolabeltxt=[] # print(f"skip {fn} when make_trainvaltest_list,not exist labelFile={labelFile1}")# 分析標(biāo)注文件列表,分配 train test val 清單,其中 val 隨機(jī)選擇
# 要求必須在imgpath存在同名的圖片文件, 如果已經(jīng)生成 yolo的txt標(biāo)注,則還需在該目錄存在同名文件
def make_trainvaltest_list():global label_totalnumglobal skipPicList_nolabeltxtglobal g_fileprefixlabelsxmlpath=opt.labelxmlpathlabelstxtpath=getlabelpath(opt.imgpath)if not os.path.exists(labelstxtpath):os.makedirs(labelstxtpath)filetype = ""global gPicFileListif len(gPicFileList)<=0:gPicFileList=getPicfilelist(opt.imgpath)total_pic=[] # not grp 文件, total_pic.clear()total_pic_grp=[] # grp文件, 不參與 val testfor file1 in gPicFileList: #遍歷所有文件st1 = os.path.splitext(file1)fn = st1[0]ext= st1[1]if file1.startswith('.'):continuexmlfile=os.path.join(labelsxmlpath,fn+".xml")if not os.path.exists(xmlfile):xmlfile=os.path.join(labelsxmlpath,fn+".XML")if not os.path.exists(xmlfile):# print(f"not exist xmlfile={xmlfile}")continueif label_totalnum>0: # 目前判斷已生成 yolo txt的策略。如果有變化,相應(yīng)做修改labelFile1=os.path.join(labelstxtpath, fn+'.txt')if not os.path.exists(labelFile1):# print(f"skip {fn} when make_trainvaltest_list,not exist labelFile={labelFile1}")skipPicList_nolabeltxt.append(fn)continueif file1.startswith(g_fileprefix):total_pic_grp.append(file1)else:total_pic.append(file1)if len(filetype)==0:filetype=check_labeltype(xmlfile)totalnum = len(total_pic)totallist = range(totalnum)# val test 數(shù)量valtestn = totalnum * (opt.val_percent + opt.test_percent)if valtestn<2:valtestn=2valtestnum = int(valtestn) # val+test的數(shù)量# val test 列表valtestList = random.sample(totallist, valtestnum)# test數(shù)量testn = int( valtestnum* (opt.test_percent/(opt.val_percent + opt.test_percent)) )if testn<1:testn=1# test 列表testList = random.sample(valtestList, testn)num_train=0num_val=0 num_test=0txtsets = ['train.txt','val.txt','test.txt']ftrain = open(os.path.join("data/", txtsets[0]), 'w')fval = open(os.path.join("data/", txtsets[1]), 'w')ftest = open(os.path.join("data/", txtsets[2]), 'w')for i in totallist:picfile1=os.path.join(opt.imgpath, total_pic[i])+'\n' # 取文件名if i in valtestList:if i in testList:if opt.runtest=="no":ftest.write(picfile1)num_test+=1else:if opt.runtest=="no":fval.write(picfile1)num_val+=1else:if opt.runtest=="no":ftrain.write(picfile1)num_train+=1for file1 in total_pic_grp:picfile1=os.path.join(opt.imgpath, file1)+'\n' # 取文件名if opt.runtest=="no":ftrain.write(picfile1)num_train+=1ftrain.close()fval.close()ftest.close()print(f"""
train/val/test pic file:ref labelsxmlpath={labelsxmlpath}filetype={filetype}total={totalnum}num_train={num_train}num_val={num_val}num_test={num_test}output=data/: {txtsets}""")def save_nolabel_pic():global skipPicList_nolabelxmlglobal skipPicList_annotationglobal skipPicList_nolabeltxtskipPicList_xml_str=fprintlist(skipPicList_nolabelxml,5)skipPicList_str=fprintlist(skipPicList_annotation,5)skipPicList_txt_str=fprintlist(skipPicList_nolabeltxt,5)skipfilelist=[]skipUse={}skipfilelist, skipUse=getFilelist(skipfilelist, skipUse, skipPicList_nolabelxml)skipfilelist, skipUse=getFilelist(skipfilelist, skipUse, skipPicList_annotation)skipfilelist, skipUse=getFilelist(skipfilelist, skipUse, skipPicList_nolabeltxt)print(f"""debug info:skip image num={len(skipPicList_nolabelxml)} when make_yololabel,not exist labelxmlFile, {skipPicList_xml_str}skip image num={len(skipPicList_annotation)} when convert_annotation, no suitable labels, {skipPicList_str}skip image num={len(skipPicList_nolabeltxt)} when make_trainvaltest_list,not exist labeltxtFile, {skipPicList_txt_str}total skip image num={len(skipfilelist)}, detail list see data/skippic.txt""")fskipfile = open("data/skippic.txt", 'w')if opt.runtest=="no":fskipfile.write('\n'.join(skipfilelist)+"\n")fskipfile.close()# x1,y1,x2,y2 --> x_center,y_center,w,h(歸一化)
def convert_normalize(size, box):w0 = size[0]h0 = size[1]xmin,xmax,ymin,ymax = box[0],box[1],box[2],box[3]if xmin<0:xmin=0if xmax>=w0:xmax=w0-1if ymin<0:ymin=0if ymax>=h0:ymax=h0-1dw = 1. / w0dh = 1. / h0x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]if h>=h0:h=h0-1if w>=w0:w=w0-1if w<opt.labelminsize or h<opt.labelminsize or w*h<300:return (0, 0, 0, 0),Falsex = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h),True# xml:xml格式; voc:voc格式
def check_labeltype(labelxmlpathfileIn):in_file = open(labelxmlpathfileIn)tree = ET.parse(in_file)root = tree.getroot()o1 = root.find('outputs')if o1 != None:o2 = o1.find('object')if o2 != None:if o2.find('item') != None:return "xml"o1 = root.find('object')if o1 != None:o2 = o1.find('bndbox')if o2 != None:return "voc"return "unknown"def convert_annotation(image_filename, xmlfile, labeltxtpathIn):global classesglobal label_totalnumglobal label_oldfilenumglobal label_newfilenumglobal label_skipnumglobal skipPicList_annotationin_file = open(xmlfile) # 'data/labelxmlpath/%s.xml' % (image_filename)labelFile1=os.path.join(labeltxtpathIn, image_filename+'.txt')if opt.forcetxt!="yes" and os.path.exists(labelFile1): # defaulttxtf1=open(labelFile1, "r")labellist=txtf1.readlines()for str1 in labellist:if len(str1.strip())>0:label_totalnum+=1label_oldfilenum+=1returnlabel_newfilenum+=1out_file = open(labelFile1, 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)label_usenum=0attrTest = 'outputs'if root.find(attrTest) != None: # if attrTest in root.tag: # if root.get(attrTest) == 'outputs':# print(f"------type=xml, image_filename={image_filename}------")itemsRoot = root.find('outputs').find("object")for obj in itemsRoot.iter('item'):cls = obj.find('name').text.strip()if cls not in classes :classes.append(cls) # continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb,Flag = convert_normalize((w, h), b)label_totalnum+=1if Flag==False:label_skipnum+=1continueif opt.runtest=="no":out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')label_usenum+=1else:# print(f"------type=voc, image_filename={image_filename}------", image_filename)for obj in root.iter('object'):difficult = obj.find('difficult').textif int(difficult) == 1:continuecls = obj.find('name').textif cls not in classes:classes.append(cls) # continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb,Flag = convert_normalize((w, h), b)label_totalnum+=1if Flag==False:label_skipnum+=1continueif opt.runtest=="no":out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')label_usenum+=1out_file.close()if label_usenum<=0 and os.path.exists(labelFile1):skipPicList_annotation.append(image_filename) # print(f"skip {image_filename} when convert_annotation, no suitable labels ")os.remove(labelFile1)# 創(chuàng)建yolo需要的格式,每圖片一個(gè)txt,每行一個(gè)對象
# class/類別編號 x_center/x中心坐標(biāo) y_center/y中心坐標(biāo) width/寬 height/高, 參數(shù)均為0-1歸一化
def make_yololabel():global classesglobal file_ext_setsglobal label_totalnumglobal label_oldfilenumglobal label_newfilenumglobal label_skipnumglobal clsfilenameglobal skipPicList_nolabelxmllabelsxmlpath=opt.labelxmlpathlabelstxtpath=getlabelpath(opt.imgpath)if not os.path.exists(labelstxtpath):os.makedirs(labelstxtpath)ticks = time.time()global gPicFileListif len(gPicFileList)<=0:gPicFileList=getPicfilelist(opt.imgpath)count1=0pnum=0lastp=0.0for file1 in gPicFileList: #遍歷所有文件# if os.path.isdir(file1): #如果是文件夾則跳過# continue# # file1=file1.lower()# st1 = os.path.splitext(file1)# fn = st1[0]# ext= st1[1]# # print("--splitext",os.path.splitext(file1))# if file1.startswith('.') or ext.lower() not in file_ext_sets:# continuefn = os.path.splitext(file1)[0]pnum+=1xmlfile=os.path.join(labelsxmlpath,fn+".xml")if not os.path.exists(xmlfile):xmlfile=os.path.join(labelsxmlpath,fn+".XML")if not os.path.exists(xmlfile):skipPicList_nolabelxml.append(fn)
# p2 = 100.0*float(pnum)/float(len(gPicFileList))
# if p2>lastp+10.0:
# lastp=p2
# lastps="%0.2f%%" % (lastp)
# print(f"not exist xmlfile={xmlfile}, p={lastps}")continuecount1 += 1convert_annotation(fn, xmlfile, labelstxtpath) # xmlfile, labeltxtpathIn# time.sleep(0.01)if time.time()-ticks>=1.5:ticks=time.time()p1 = 100.0*float(count1)/float(len(gPicFileList))print(time.strftime("%Y-%m-%d %H:%M:%S ", time.localtime()),', %5.1f%%' % p1 )# save obj.namescls_file = open(clsfilename, 'w')if opt.runtest=="no":cls_file.write("\n".join(classes) + '\n')usep="%0.2f" % (100.0*float(label_totalnum-label_skipnum)/float(label_totalnum))print(f"""label info:pic_num={pnum}labeltxt={labelstxtpath}label_totalnum={label_totalnum}label_oldfilenum={label_oldfilenum}label_newfilenum={label_newfilenum}label_usenum={label_totalnum-label_skipnum}, {usep}%label_skipnum={label_skipnum}, obj too smallclasses={clsfilename}, num={len(classes)}""")# images (或 imgs )
def getlabelpath(imgPath):i1 = imgPath.rfind('images')if i1>=0:labelp = imgPath[:i1]+"labels"+imgPath[i1+len("images"):]return labelpi1 = imgPath.rfind('imgs')if i1>=0:labelp = imgPath[:i1]+"labels"+imgPath[i1+len("imgs"):]return labelpreturn os.path.join(imgPath,"../labels")def ftest():if True:p1 = "E:\\opencv_org/windows\\Opencv4\\sources.base\\data\\haarcascades_cudA/haarcascade_eye.xml"imgPathspilit=p1.lower().replace('\\','/').split('/')print("----imgPathspilit=",imgPathspilit)returnif False:if not os.path.exists(txtsaveTempPath):os.makedirs(txtsaveTempPath)pf1 = os.path.join(txtsaveTempPath, "train.txt")print("----pf1=",pf1)if False:in_file = open('E:/opencv_org/models/smoke/test1/outputs-xml/000000.xml')tree = ET.parse(in_file)root = tree.getroot()attrTest = 'outputs'print("---find=", root.find(attrTest))if root.find(attrTest) != None: # if attrTest in root.tag: # if root.get(attrTest) == 'outputs':print("---xml")else:print("---voc")def parse_opt(known=False):parser = argparse.ArgumentParser()# 圖片路徑。需要有一個(gè)子目錄名稱為 images 或 imgs ; 標(biāo)簽txt位置,通過將該子目錄名稱替換為 labels 獲得parser.add_argument('--imgpath', type=str, default=ROOT / 'data/images', help='images path')# voc 或 xml 格式的標(biāo)注文件,可用 標(biāo)注精靈 或 lableimg 工具,后者是個(gè)python程序,推薦使用parser.add_argument('--labelxmlpath', type=str, default=ROOT / 'data/labelxmlpath', help='labels xml path')# w 或 h 小于指定值的標(biāo)簽被忽略parser.add_argument('--labelminsize', type=int, default=12, help='labels min width and height, and w*h>=300')# 圖片分配比例# val 比例, 總數(shù)控制 500 左右?# test 比例, 總數(shù)控制100左右? ( 訓(xùn)練期間不參與,訓(xùn)練完成后,用來人工檢測識別成果 ) parser.add_argument('--train_percent', type=float, default=0.90, help='train_percent, train+val+test=1.0')parser.add_argument('--val_percent', type=float, default=0.08, help='val_percent, train+val+test=1.0')parser.add_argument('--test_percent', type=float, default=0.02, help='test_percent, train+val+test=1.0')# 強(qiáng)制重建yolo格式標(biāo)簽文件,否則跳過已存在的文件parser.add_argument('--forcetxt', type=str, default='yes', help='force rebuild yolo label txt file')# 在 xml 標(biāo)簽文件目錄中,如果在 images 目錄下沒有同名圖片文件,則該 xml標(biāo)簽文件 被移到 xmlbak 目錄parser.add_argument('--movexmlbak', type=str, default='no', help='move xml label file when no same name image file')# 僅測試, 不寫文件yolo txt文件、train/val/test 文件parser.add_argument('--runtest', type=str, default='no', help='run ftest() only')opt = parser.parse_known_args()[0] if known else parser.parse_args()return optdef getPicfilelist(imgPath):global file_ext_setsglobal gPicFilemapgPicFilemap.clear()picFileListOut = []picFileList0 = os.listdir(imgPath)# print("--picFileList0",picFileList0)for file1 in picFileList0: #遍歷所有文件if os.path.isdir(file1): #如果是文件夾則跳過continuest1 = os.path.splitext(file1)fn = st1[0]ext= st1[1]# print("--splitext",os.path.splitext(file1))if (not file1.startswith('.')) and ext.lower() in file_ext_sets:picFileListOut.append(file1)gPicFilemap[fn]=file1print(f"get picFileList={len(picFileListOut)}")return picFileListOut# 不以.開頭、擴(kuò)展名為 file_ext_sets 的文件數(shù)
# images (或 imgs )
def getpicfilenum(imgPath):if not os.path.exists(imgPath):print(f"--imgpath not exist={imgPath}")return -1imgPathspilit=imgPath.lower().replace('\\','/').split('/')if "images" not in imgPathspilit and "imgs" not in imgPathspilit:print("--imgpath need contains images or imgs subdir")return -1global gPicFileListif len(gPicFileList)<=0:gPicFileList=getPicfilelist(imgPath)return len(gPicFileList)# 不以.開頭、擴(kuò)展名為xml的文件數(shù)
def getxmlfilenum(labelsxmlPath):if not os.path.exists(labelsxmlPath):print(f"--labelsxmlPath not exist={labelsxmlPath}")return -1xmlFileList = os.listdir(labelsxmlPath)count=0for file1 in xmlFileList: #遍歷所有文件if os.path.isdir(file1): #如果是文件夾則跳過continuefile1=file1.lower()if (not file1.startswith('.')) and file1.endswith('.xml'):count += 1return countdef fprintlist(listIn, num):str1=""if len(listIn)>num:str1="%s"%listIn[:num]+"..."else:str1="%s"%listInreturn str1def getFilelist(skipfilelist, skipUse, listIn):for fn in listIn:if skipUse.get(fn)!=None:continueskipUse[fn]=1f1=os.path.join(opt.imgpath,gPicFilemap[fn])if os.path.exists(f1):skipfilelist.append(f1)return skipfilelist,skipUsedef movexmlfile(labelsxmlPath):global gPicFileListglobal gPicFilemapif not os.path.exists(labelsxmlPath):print(f"--labelsxmlPath not exist={labelsxmlPath}")return -1print(f"--enter move not use xml file")if len(gPicFileList)<=0:gPicFileList=getPicfilelist(imgPath)labelsxmlPathBak=labelsxmlPath+"_bak"if not os.path.exists(labelsxmlPathBak):os.makedirs(labelsxmlPathBak)xmlFileList = os.listdir(labelsxmlPath)for file1 in xmlFileList: #遍歷所有文件if os.path.isdir(file1): #如果是文件夾則跳過continuefile2=file1.lower()if (file2.startswith('.')) or not file2.endswith('.xml'):continuefn = os.path.splitext(file1)[0]if gPicFilemap.get(fn)==None: # xml文件在 images 沒有同名文件os.replace(os.path.join(labelsxmlPath, file1), os.path.join(labelsxmlPathBak, file1))print(f"--end to move xml file")# 執(zhí)行重建yolo txt文件時(shí),將全部已有文件移到 labels_bak
def movetxtfile(labelstxtPath):if not os.path.exists(labelstxtPath):print(f"--labelstxtPath not exist={labelstxtPath}")return -1print(f"--enter move exist txt file")labelstxtPathBak=labelstxtPath+"_bak"if not os.path.exists(labelstxtPathBak):os.makedirs(labelstxtPathBak)FileList = os.listdir(labelstxtPath)for file1 in FileList: #遍歷所有文件if os.path.isdir(file1): #如果是文件夾則跳過continueos.replace(os.path.join(labelstxtPath, file1), os.path.join(labelstxtPathBak, file1))print(f"--end to move txt file")def finitLoad():global classesclasses.clear()# obj.namesif os.path.exists(clsfilename):cls_file = open(clsfilename, 'r')clsList1=cls_file.readlines()for cls in clsList1:cls=cls.strip()if len(cls)>0:classes.append(cls)print("classes load from file=",classes)returndef fmain(opt):global skipPicList_nolabelxmlglobal skipPicList_annotationglobal skipPicList_nolabeltxtglobal gPicFilemapfinitLoad()if opt.labelminsize<8:opt.labelminsize=12pn=getpicfilenum(opt.imgpath)if pn<=0:if pn==0:print("no pic file in --imgpath ")exit(0)xn=getxmlfilenum(opt.labelxmlpath)if xn<=0:if xn==0:print("no xml file in --labelxmlpath ")exit(0)# if True:if opt.movexmlbak!="no":movexmlfile(opt.labelxmlpath) # 將 xml 文件中,在 images 沒有對應(yīng)圖片的xml文件移到 xmlbak 目錄if opt.forcetxt=="yes":movetxtfile(getlabelpath(opt.imgpath))# print("cur dir=", getcwd())make_yololabel()make_trainvaltest_list()save_nolabel_pic()if __name__ == "__main__":opt = parse_opt()print("\nargparse:")print(vars(opt))print("\n")# if True:if False:# movetxtfile("E:\opencv_org\models\dataset\smoke,dataset\labels_test")exit(0)if False:ftest()exit(0)fmain(opt)
腳本文件2:打開視頻文件,按指定的間隔提取圖片,文件名稱 grp_<videoname>_<nnnnnn>.jpg
# e.g.
# cls && python make_video_pic.py --source G:\archive\fire_sample\t1.mp4 --imageoutput G:\archive\v1 --interval 0.2
# cls && python make_video_pic.py --source G:\archive\fire_sample\smoke9.avi --imageoutput G:\archive\v1 --interval 2.0
# 提取的圖片文件名稱帶 grp_ 前綴,這些文件不參與訓(xùn)練時(shí)校準(zhǔn)val、測驗(yàn)test# Python 2/3 compatibility
from __future__ import print_function
import argparse
import numpy as np
import cv2 as cv
import os
import sys
from pathlib import Path
# import utils
import timeFILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
if str(ROOT) not in sys.path:sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relativeg_fileprefix="grp_"WHlimited=640 # maxn(w,h)<WHlimited時(shí),將其放大為 WHlimiteddef parse_opt(known=False):parser = argparse.ArgumentParser()# 視頻文件。parser.add_argument('--source', type=str, default=ROOT / 'data/videofile', help='videofile')parser.add_argument('--imageoutput', type=str, default=ROOT / 'data/imageoutput', help='imageoutput path')# interval 每張間隔秒數(shù)parser.add_argument('--interval', type=float, default=0.5, help='interval')# 正常播放速度parser.add_argument('--play', type=str, default='no', help='play')# image 文件前綴parser.add_argument('--prefix', type=str, default='', help='image file prefix')# 僅測試, 不寫image文件parser.add_argument('--runtest', type=str, default='no', help='run ftest() only')opt = parser.parse_known_args()[0] if known else parser.parse_args()return optdef main():def decode_fourcc(v):v = int(v)return "".join([chr((v >> 8 * i) & 0xFF) for i in range(4)])
# 檢測源文件是否存在if not os.path.exists(opt.source):print(f"not exist: {opt.source}")exit(0)
# 目標(biāo)目錄不存在則創(chuàng)建if not os.path.exists(opt.imageoutput):os.makedirs(opt.imageoutput)
# 每秒取多少圖片if opt.interval<=0.0:opt.interval=0.5fpsgrab = float( 1.0/float(opt.interval) ) # 每秒取多少圖片
# 打開視頻cap = cv.VideoCapture(opt.source) # VideoCapture(0)if not cap.isOpened():print(f"open fail, video={opt.source}")# cap.set(cv.CAP_PROP_AUTOFOCUS, 0) # Known bug: https://github.com/opencv/opencv/pull/5474if opt.play=="yes":cv.namedWindow("Video")
# convert_rgb = True
# 視頻的幀率,每幀延時(shí)fps = float(cap.get(cv.CAP_PROP_FPS))if fps<0.0001:fps=25.0delay1 = float(1.0)/fps# focus = int(min(cap.get(cv.CAP_PROP_FOCUS) * 100, 2**31-1)) # ceil focus to C_LONG as Python3 int can go to +inf# cv.createTrackbar("FPS", "Video", int(fps), 30, lambda v: cap.set(cv.CAP_PROP_FPS, v))# cv.createTrackbar("Focus", "Video", focus, 100, lambda v: cap.set(cv.CAP_PROP_FOCUS, v / 100))frameWidth = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))frameHeight = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
# 視頻寬 高縮放比例f1 = float(frameWidth)if frameWidth < frameHeight:f1 = float(frameHeight)if f1<1.0:print(f"get w h fail, video={opt.source},fps={fps},w={frameWidth},h={frameHeight}")returnresize_ratio = -1.0if float(WHlimited)> f1:resize_ratio = float(WHlimited) / f1ticksPrn = time.time()count=0findex=0 #從零開始countSeg=int(fps/fpsgrab) # 每隔多少幀抓取一張indexseg=0print(f"video={opt.source},fps={fps},delay={delay1},w={frameWidth},h={frameHeight},countSeg={countSeg}")global g_fileprefixwhile True:ticks1 = time.time()hasFrame, img = cap.read()if not hasFrame or img is None:if count==0:print('No frames grabbed!')breakfourcc = decode_fourcc(cap.get(cv.CAP_PROP_FOURCC))count+=1indexseg+=1if opt.play=="yes":ticks2 = time.time()-ticks1d1 = delay1-ticks2if d1>0.0001:time.sleep(d1)ticks1 = time.time()-ticks1
# if not bool(cap.get(cv.CAP_PROP_CONVERT_RGB)):
# if fourcc == "MJPG":
# img = cv.imdecode(img, cv.IMREAD_GRAYSCALE)
# elif fourcc == "YUYV":
# img = cv.cvtColor(img, cv.COLOR_YUV2GRAY_YUYV)
# else:
# print("unsupported format")
# breakif time.time()-ticksPrn>=1.5:ticksPrn=time.time()p1 = 1000.0*float(ticks1)print(time.strftime("%Y-%m-%d %H:%M:%S ", time.localtime()),', tick=%5.1f ms' % p1 )if resize_ratio>0.01:img = cv.resize(img, (0, 0), fx=resize_ratio, fy=resize_ratio)if indexseg>=countSeg:indexseg=0findex+=1NewFile = os.path.join(opt.imageoutput, g_fileprefix+opt.prefix+str(findex).zfill(6)+".jpg")if opt.runtest!="yes":cv.imwrite(NewFile, img)print(f"save to={NewFile}")if opt.play=="yes":cv.imshow("Video", img)k = cv.waitKey(1)if k == 27:break
# elif k == ord('g'):
# convert_rgb = not convert_rgb
# cap.set(cv.CAP_PROP_CONVERT_RGB, 1 if convert_rgb else 0)cap.release() # 釋放print('Done')def ftest():returnif __name__ == '__main__':opt = parse_opt()# 獲取視頻文件的名稱if len(opt.prefix)==0:f1 = os.path.basename(opt.source)opt.prefix = os.path.splitext(f1)[0]+"_"print("\nargparse:")print(vars(opt))print("\n")if False:ftest()exit(0)main()cv.destroyAllWindows()
總結(jié)
以上是生活随笔為你收集整理的yolo标注文件转换工具,python做的的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。