YOLO轉VOC

具體參考:https://blog.csdn.net/qq_43161211/article/details/122958255(如侵權现横,速刪)

1.數據目錄格式如下:

其中JPEGImages為圖片數據所在的目錄贞远,YOLO為yolo格式的標簽數據所在目錄,Annotations為生成的voc格式數據標簽目錄(程序運行前這是一個空目錄)

image.png
2.把對應文件、圖片放到對應目錄下
3.執(zhí)行yolo_to_voc.py:

本人使用的圖片格式為bmp苞七,jpg等格式需修改程序中的bmp->jpg

from xml.dom.minidom import Document
import os
import cv2
 
 
# def makexml(txtPath, xmlPath, picPath):  # txt所在文件夾路徑器予,xml文件保存路徑,圖片所在文件夾路徑
def makexml(picPath, txtPath, xmlPath):  # txt所在文件夾路徑浙芙,xml文件保存路徑登刺,圖片所在文件夾路徑
    """此函數用于將yolo格式txt標注文件轉換為voc格式xml標注文件
    在自己的標注圖片文件夾下建三個子文件夾,分別命名為picture茁裙、txt塘砸、xml
    """
    dic = {'0': "Person",  # 創(chuàng)建字典用來對類型進行轉換
           '1': "Car",  # 此處的字典要與自己的classes.txt文件中的類對應,且順序要一致
           }
    files = os.listdir(txtPath)
    for i, name in enumerate(files):
        xmlBuilder = Document()
        annotation = xmlBuilder.createElement("annotation")  # 創(chuàng)建annotation標簽
        xmlBuilder.appendChild(annotation)
        txtFile = open(txtPath + name)
        txtList = txtFile.readlines()
        img = cv2.imread(picPath + name[0:-4] + ".bmp")
        Pheight, Pwidth, Pdepth = img.shape
 
        folder = xmlBuilder.createElement("folder")  # folder標簽
        foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
        folder.appendChild(foldercontent)
        annotation.appendChild(folder)  # folder標簽結束
 
        filename = xmlBuilder.createElement("filename")  # filename標簽
        filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".bmp")
        filename.appendChild(filenamecontent)
        annotation.appendChild(filename)  # filename標簽結束
 
        size = xmlBuilder.createElement("size")  # size標簽
        width = xmlBuilder.createElement("width")  # size子標簽width
        widthcontent = xmlBuilder.createTextNode(str(Pwidth))
        width.appendChild(widthcontent)
        size.appendChild(width)  # size子標簽width結束
 
        height = xmlBuilder.createElement("height")  # size子標簽height
        heightcontent = xmlBuilder.createTextNode(str(Pheight))
        height.appendChild(heightcontent)
        size.appendChild(height)  # size子標簽height結束
 
        depth = xmlBuilder.createElement("depth")  # size子標簽depth
        depthcontent = xmlBuilder.createTextNode(str(Pdepth))
        depth.appendChild(depthcontent)
        size.appendChild(depth)  # size子標簽depth結束
 
        annotation.appendChild(size)  # size標簽結束
 
        for j in txtList:
            oneline = j.strip().split(" ")
            object = xmlBuilder.createElement("object")  # object 標簽
            picname = xmlBuilder.createElement("name")  # name標簽
            namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
            picname.appendChild(namecontent)
            object.appendChild(picname)  # name標簽結束
 
            pose = xmlBuilder.createElement("pose")  # pose標簽
            posecontent = xmlBuilder.createTextNode("Unspecified")
            pose.appendChild(posecontent)
            object.appendChild(pose)  # pose標簽結束
 
            truncated = xmlBuilder.createElement("truncated")  # truncated標簽
            truncatedContent = xmlBuilder.createTextNode("0")
            truncated.appendChild(truncatedContent)
            object.appendChild(truncated)  # truncated標簽結束
 
            difficult = xmlBuilder.createElement("difficult")  # difficult標簽
            difficultcontent = xmlBuilder.createTextNode("0")
            difficult.appendChild(difficultcontent)
            object.appendChild(difficult)  # difficult標簽結束
 
            bndbox = xmlBuilder.createElement("bndbox")  # bndbox標簽
            xmin = xmlBuilder.createElement("xmin")  # xmin標簽
            mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
            xminContent = xmlBuilder.createTextNode(str(mathData))
            xmin.appendChild(xminContent)
            bndbox.appendChild(xmin)  # xmin標簽結束
 
            ymin = xmlBuilder.createElement("ymin")  # ymin標簽
            mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
            yminContent = xmlBuilder.createTextNode(str(mathData))
            ymin.appendChild(yminContent)
            bndbox.appendChild(ymin)  # ymin標簽結束
 
            xmax = xmlBuilder.createElement("xmax")  # xmax標簽
            mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
            xmaxContent = xmlBuilder.createTextNode(str(mathData))
            xmax.appendChild(xmaxContent)
            bndbox.appendChild(xmax)  # xmax標簽結束
 
            ymax = xmlBuilder.createElement("ymax")  # ymax標簽
            mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
            ymaxContent = xmlBuilder.createTextNode(str(mathData))
            ymax.appendChild(ymaxContent)
            bndbox.appendChild(ymax)  # ymax標簽結束
 
            object.appendChild(bndbox)  # bndbox標簽結束
 
            annotation.appendChild(object)  # object標簽結束
 
        f = open(xmlPath + name[0:-4] + ".xml", 'w')
        xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
        f.close()
 
if __name__ == "__main__":
    picPath = "/home/slave110/code/VOCdevkit/VOC2007/JPEGImages/"  # 圖片所在文件夾路徑晤锥,后面的/一定要帶上
    txtPath = "/home/slave110/code/VOCdevkit/VOC2007/YOLO/"  # txt所在文件夾路徑掉蔬,后面的/一定要帶上
    xmlPath = "/home/slave110/code/VOCdevkit/VOC2007/Annotations/"  # xml文件保存路徑,后面的/一定要帶上
    makexml(picPath, txtPath, xmlPath)

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末矾瘾,一起剝皮案震驚了整個濱河市女轿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌壕翩,老刑警劉巖蛉迹,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異放妈,居然都是意外死亡北救,警方通過查閱死者的電腦和手機荐操,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來珍策,“玉大人托启,你說我怎么就攤上這事∪林妫” “怎么了屯耸?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蹭劈。 經常有香客問我疗绣,道長,這世上最難降的妖魔是什么铺韧? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任多矮,我火速辦了婚禮,結果婚禮上祟蚀,老公的妹妹穿的比我還像新娘工窍。我一直安慰自己,他們只是感情好前酿,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布患雏。 她就那樣靜靜地躺著,像睡著了一般罢维。 火紅的嫁衣襯著肌膚如雪淹仑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天肺孵,我揣著相機與錄音匀借,去河邊找鬼。 笑死平窘,一個胖子當著我的面吹牛吓肋,可吹牛的內容都是我干的。 我是一名探鬼主播瑰艘,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼是鬼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了紫新?” 一聲冷哼從身側響起均蜜,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎芒率,沒想到半個月后囤耳,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年充择,在試婚紗的時候發(fā)現(xiàn)自己被綠了德玫。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡聪铺,死狀恐怖化焕,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情铃剔,我是刑警寧澤,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布查刻,位于F島的核電站键兜,受9級特大地震影響,放射性物質發(fā)生泄漏穗泵。R本人自食惡果不足惜普气,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望佃延。 院中可真熱鬧现诀,春花似錦、人聲如沸履肃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽尺棋。三九已至封锉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間膘螟,已是汗流浹背成福。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留荆残,地道東北人奴艾。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像内斯,于是被迫代替她去往敵國和親蕴潦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內容