Yolov5多邊形標(biāo)簽和JSON數(shù)據(jù)格式轉(zhuǎn)換
Labelme簡(jiǎn)要介紹
通過(guò)labelme對(duì)圖進(jìn)行標(biāo)注后,得到的是json文件,而Yolov5對(duì)數(shù)據(jù)進(jìn)行模型構(gòu)建的時(shí)候,讀取需要的是txt格式的文件。所以需要先通過(guò)Python進(jìn)行文件格式的轉(zhuǎn)換
注:labelme是麻省理工(MIT)的計(jì)算機(jī)科學(xué)和人工智能實(shí)驗(yàn)室(CSAIL)研發(fā)的圖像標(biāo)注工具,人們可以使用該工具創(chuàng)建定制化標(biāo)注任務(wù)或執(zhí)行圖像標(biāo)注,項(xiàng)目源代碼已經(jīng)開源。
Labelme程序運(yùn)行,通過(guò)標(biāo)注后如圖所示:
圖1 Labelme標(biāo)注
此圖片可以得到以下格式的json文件:
文件中的字段如下:
‘version’——版本號(hào)
‘shapes’——里面裝的是Yolov5需要的數(shù)據(jù)
‘label’——你在labelme里面設(shè)置的類
‘points’——點(diǎn)的坐標(biāo)
我這里的label如圖1所示共有5類,等下進(jìn)行json轉(zhuǎn)化為txt的時(shí)候用
對(duì)應(yīng)這些類創(chuàng)一個(gè)字典以便json進(jìn)行轉(zhuǎn)換
例:name2id={'bike':0,'arrow':1,'crossline':2,'building':3,'car':4,'person':5}
可能某一張圖片中可能不存在上述的某個(gè)類,所以這里請(qǐng)以某個(gè)json中最多的類創(chuàng)建這個(gè)字典。
多邊形標(biāo)簽的處理方法
由于yolov5 僅支持矩形圖形的識(shí)別,所以需要通過(guò)數(shù)據(jù)處理,將多邊形變換為矩形。
處理原理:遍歷該標(biāo)簽所有的坐標(biāo),獲取最大x_max,y_max,最小x_min,y_min的x和y的坐標(biāo)。
然后再進(jìn)行數(shù)據(jù)的規(guī)范化。
轉(zhuǎn)換后的txt格式如下:
第一個(gè)是類,比如第一行中的第一個(gè)數(shù)字是4,我的name2id中car也為4,即這里指代的就是'car'這個(gè)標(biāo)簽。
第一行 第二個(gè) 和 第三個(gè)數(shù)字 為數(shù)字為圖片中心點(diǎn)(x,y)的坐標(biāo)
第四個(gè)數(shù)字和第五個(gè)數(shù)字對(duì)應(yīng)的是 這個(gè)標(biāo)簽的 寬和高。
代碼實(shí)現(xiàn)
多邊形標(biāo)簽代碼實(shí)現(xiàn)方法
x_max=0 y_max=0 x_min=100000 y_min=100000 for lk in range(len(i['points'])): x1=float(i['points'][lk][0]) y1=float(i['points'][lk][1]) # print(x1) if x_max<x1: x_max=x1 if y_max<y1: y_max=y1 if y_min>y1: y_min=y1 if x_min>x1: x_min=x1 bb = (x_min, y_max, x_max, y_min)
json轉(zhuǎn)化為txt的部分代碼如下:
def decode_json(json_floder_path,txt_outer_path, json_name): # json_floder_path='E:\\Python_package\\itesjson\\' # json_name='V1125.json' txt_name = txt_outer_path + json_name[:-5] + '.txt' with open(txt_name,'w') as f: json_path = os.path.join(json_floder_path, json_name)#os路徑融合 data = json.load(open(json_path, 'r', encoding='gb2312',errors='ignore')) img_w = data['imageWidth']#圖片的高 img_h = data['imageHeight']#圖片的寬 isshape_type=data['shapes'][0]['shape_type'] print(isshape_type) #print(isshape_type) #print('下方判斷根據(jù)這里的值可以設(shè)置為你自己的類型,我這里是polygon'多邊形) #len(data['shapes']) for i in data['shapes']: label_name = i['label']#得到j(luò)son中你標(biāo)記的類名 if (i['shape_type'] == 'polygon'):#數(shù)據(jù)類型為多邊形 需要轉(zhuǎn)化為矩形 x_max=0 y_max=0 x_min=100000 y_min=100000 for lk in range(len(i['points'])): x1=float(i['points'][lk][0]) y1=float(i['points'][lk][1]) # print(x1) if x_max<x1: x_max=x1 if y_max<y1: y_max=y1 if y_min>y1: y_min=y1 if x_min>x1: x_min=x1 bb = (x_min, y_max, x_max, y_min) if (i['shape_type'] == 'rectangle'):#為矩形不需要轉(zhuǎn)換 x1 = float(i['points'][0][0]) y1 = float(i['points'][0][1]) x2 = float(i['points'][1][0]) y2 = float(i['points'][1][1]) bb = (x1, y1, x2, y2) bbox = convert((img_w, img_h), bb) try: f.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n') except: pass
json_floder——讀取json的文件夾的絕對(duì)路徑
txt_outer_path——保存txt文本的文件夾的絕對(duì)路徑
json_name——json_name是json文本的名字
讀取該類的坐標(biāo)然后進(jìn)行數(shù)字規(guī)范化
數(shù)字規(guī)范化的代碼如下:
def convert(img_size, box): dw = 1. / (img_size[0]) dh = 1. / (img_size[1]) x = (box[0] + box[2]) / 2.0 y = (box[1] + box[3]) / 2.0 w = abs(box[2] - box[0]) h = abs(box[3] - box[1])#加入絕對(duì)值的原因是只需要它的寬和高,以免出現(xiàn)負(fù)數(shù) x = x * dw w = w * dw y = y * dh h = h * dh return (x, y, w, h)
最后附上我的完整代碼:
#處理labelme多邊形矩陣的標(biāo)注 json轉(zhuǎn)化txt import json import os name2id={'bike':0,'arrow':1,'crossline':2,'building':3,'car':4,'person':5} def convert(img_size, box): dw = 1. / (img_size[0]) dh = 1. / (img_size[1]) x = (box[0] + box[2]) / 2.0 y = (box[1] + box[3]) / 2.0 w = abs(box[2] - box[0]) h = abs(box[3] - box[1]) x = x * dw w = w * dw y = y * dh h = h * dh return (x, y, w, h) def decode_json(json_floder_path,txt_outer_path, json_name): # json_floder_path='E:\\Python_package\\itesjson\\' # json_name='V1125.json' txt_name = txt_outer_path + json_name[:-5] + '.txt' with open(txt_name,'w') as f: json_path = os.path.join(json_floder_path, json_name)#os路徑融合 data = json.load(open(json_path, 'r', encoding='gb2312',errors='ignore')) img_w = data['imageWidth']#圖片的高 img_h = data['imageHeight']#圖片的寬 isshape_type=data['shapes'][0]['shape_type'] print(isshape_type) #print(isshape_type) #print('下方判斷根據(jù)這里的值可以設(shè)置為你自己的類型,我這里是polygon'多邊形) #len(data['shapes']) for i in data['shapes']: label_name = i['label']#得到j(luò)son中你標(biāo)記的類名 if (i['shape_type'] == 'polygon'):#數(shù)據(jù)類型為多邊形 需要轉(zhuǎn)化為矩形 x_max=0 y_max=0 x_min=100000 y_min=100000 for lk in range(len(i['points'])): x1=float(i['points'][lk][0]) y1=float(i['points'][lk][1]) # print(x1) if x_max<x1: x_max=x1 if y_max<y1: y_max=y1 if y_min>y1: y_min=y1 if x_min>x1: x_min=x1 bb = (x_min, y_max, x_max, y_min) if (i['shape_type'] == 'rectangle'):#為矩形不需要轉(zhuǎn)換 x1 = float(i['points'][0][0]) y1 = float(i['points'][0][1]) x2 = float(i['points'][1][0]) y2 = float(i['points'][1][1]) bb = (x1, y1, x2, y2) bbox = convert((img_w, img_h), bb) try: f.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n') except: pass if __name__ == "__main__": json_floder_path = 'E:\Python_package\\itesjson\\'#存放json的文件夾的絕對(duì)路徑 txt_outer_path='E:\Python_package\\e1\\'#存放txt的文件夾絕對(duì)路徑 json_names = os.listdir(json_floder_path) print("共有:{}個(gè)文件待轉(zhuǎn)化".format(len(json_names))) flagcount=0 for json_name in json_names: decode_json(json_floder_path,txt_outer_path,json_name) flagcount+=1 print("還剩下{}個(gè)文件未轉(zhuǎn)化".format(len(json_names)-flagcount)) break # break print('轉(zhuǎn)化全部完畢')
os.listdir——讀取文件下的所有文件。請(qǐng)先創(chuàng)建一個(gè)只有json文件的文件夾
記得先修改!?。f __name__==“__main__”后的參數(shù)
json_floder_path——改成你自己的json文件夾路徑
txt_outer_path——改成你自己的輸出文件夾路徑
到此這篇關(guān)于Yolov5多邊形標(biāo)簽和JSON數(shù)據(jù)格式轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Yolov5的JSON數(shù)據(jù)格式轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python政策網(wǎng)字體反爬實(shí)例(附完整代碼)
大家好,本篇文章主要講的是python政策網(wǎng)字體反爬實(shí)例(附完整代碼),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01史上最全Python文件類型讀寫庫(kù)大盤點(diǎn)
這篇文章主要為大家詳細(xì)介紹了史上最全Python文件類型讀寫庫(kù)大盤點(diǎn),包含常用和不常用的大量文件格式,文本、音頻、視頻應(yīng)有盡有,廢話不多說(shuō),走起來(lái)2023-05-05Python實(shí)現(xiàn)監(jiān)控遠(yuǎn)程主機(jī)實(shí)時(shí)數(shù)據(jù)的示例詳解
這篇文章主要為大家詳細(xì)介紹了Python如何使用Socket庫(kù)和相應(yīng)的第三方庫(kù)來(lái)監(jiān)控遠(yuǎn)程主機(jī)的實(shí)時(shí)數(shù)據(jù),比如CPU使用率、內(nèi)存使用率、網(wǎng)絡(luò)帶寬等,感興趣的可以了解一下2023-04-04Python設(shè)置默認(rèn)編碼為utf8的方法
這篇文章主要介紹了Python設(shè)置默認(rèn)編碼為utf8的方法,結(jié)合實(shí)例形式分析了Python針對(duì)文件編碼的設(shè)置方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-07-07Python開發(fā)SQLite3數(shù)據(jù)庫(kù)相關(guān)操作詳解【連接,查詢,插入,更新,刪除,關(guān)閉等】
這篇文章主要介紹了Python開發(fā)SQLite3數(shù)據(jù)庫(kù)相關(guān)操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python操作SQLite3數(shù)據(jù)庫(kù)的連接,查詢,插入,更新,刪除,關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07利用Python腳本實(shí)現(xiàn)ping百度和google的方法
最近在做SEO的時(shí)候,為了讓發(fā)的外鏈能夠快速的收錄,想到了利用ping的功能,google和百度都有相關(guān)的ping介紹,有興趣的朋友可以去看看相關(guān)的知識(shí)。下面這篇文章主要介紹了利用Python腳本實(shí)現(xiàn)ping百度和google的方法,需要的朋友可以參考借鑒,一起來(lái)看看吧。2017-01-01