亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Yolov5多邊形標(biāo)簽和JSON數(shù)據(jù)格式轉(zhuǎn)換

 更新時(shí)間:2023年05月04日 09:39:14   作者:Pandas_007  
通過(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簡(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)文章

最新評(píng)論