LabelMe 标注的json转txt的格式转换教程

1 labelme标注后会生成json文件

2 json解析

  • annotation里面存放labelme生成的标注文件
  • imgs里面存图片
  • txtlabel文件夹存放我们转换后的txt标签文件、

  直接建一个和上面一样的文件夹

3  从json查看标注结果

import json
import os
import cv2
img_folder_path=r'F:\imagedata\FOV\FOV_SRC_2\imgs'#原图片存放地址
folder_path=r"F:\imagedata\FOV\FOV_SRC_2\annotation"#标注数据的文件地址


# cv2.putText(img, str, (123,456)), font, 2, (0,255,0), 3)
# 各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色,字体粗细
def show_label_from_json(img_path,json_d):
    window_name = ('src') 
    cv2.namedWindow(window_name,cv2.WINDOW_FREERATIO)
    src_img=cv2.imread(img_path)   
    font = cv2.FONT_HERSHEY_SIMPLEX
    for item in json_d["shapes"]:
        #print(item['points'])
        point=item['points']
        p1=(int(point[0][0]),int(point[0][1]))
        p2=(int(point[1][0]),int(point[1][1]))
        cv2.rectangle(src_img, p1, p2, (0, 255, 0), 2)
        cv2.putText(src_img,item['label'],p1,font,2,(0,255,255),3)
        cv2.imshow(window_name,src_img)
        cv2.waitKey(0)
    cv2.destroyAllWindows()
    return 

i=0
for jsonfile in os.listdir(folder_path):
    temp_path=os.path.join(folder_path,jsonfile)
    
    i+=1
    if i>5:
        break
    #如果是一个子目录就继续
    if os.path.isdir(temp_path):
        continue
    print("json_path:\t",temp_path)
    temp_path
    with open(temp_path, "r", encoding='utf-8') as f:
        json_d = json.load(f)
        img_name=json_d['imagePath'].split("\\")[-1].split(".")[0]+".jpg"
        img_path=os.path.join(img_folder_path,img_name)    
        print("img_path:\t",img_path)
        show_label_from_json(img_path,json_d)

4 json转txt

有些用图片的绝对坐标,有的用相对坐标,下面两种方式都写了

  • 相对坐标形式 :label x_center y_center w h
  • 绝对坐标形式 :label x1 y1 x2 y2(x1,y1是标记的左上角的坐标,x2 y2右下角坐标)
import json
import os
import cv2
img_folder_path=r'F:\imagedata\FOV\FOV_SRC_2\imgs'#图片存放文件夹
folder_path=r"F:\imagedata\FOV\FOV_SRC_2\annotation"#标注数据的文件地址
txt_folder_path = r"F:\imagedata\FOV\FOV_SRC_2\txtlabel"#转换后的txt标签文件存放的文件夹

#保存为相对坐标形式 :label x_center y_center w h
def relative_coordinate_txt(img_name,json_d,img_path):
    src_img=cv2.imread(img_path)
    h,w = src_img.shape[:2]
    txt_name = img_name.split(".")[0]+".txt"
    txt_path = os.path.join(txt_folder_path,txt_name)
    print(txt_path)
    with open(txt_path,'w') as f:
        for item in json_d["shapes"]:
            #print(item['points'])
            #print(item['label'])
            point=item['points']
            x_center = (point[0][0]+point[1][0])/2
            y_center = (point[0][1]+point[1][1])/2
            width = point[1][0]-point[0][0]
            hight = point[1][1]-point[0][1]
            #print(x_center)
            f.write(" {} ".format(item['label']))
            f.write(" {} ".format(x_center/w))
            f.write(" {} ".format(y_center/h))
            f.write(" {} ".format(width/w))
            f.write(" {} ".format(hight/h))
            f.write(" \n")
            
#保存为绝对坐标形式 :label x1 y1 x2 y2
def absolute_coordinate_txt(img_name,json_d,img_path):
    src_img=cv2.imread(img_path)
    h,w = src_img.shape[:2]
    txt_name = img_name.split(".")[0]+".txt"
    txt_path = os.path.join(txt_folder_path,txt_name)
    print("txt_path:\t",txt_path)
    with open(txt_path,'w') as f:
        for item in json_d["shapes"]:
            #print(item['points'])
            #print(item['label'])
            point=item['points']
            x1 = point[0][0]
            y1 = point[0][1]
            x2 = point[1][0]
            y2 = point[1][1]
            f.write(" {} ".format(item['label']))
            f.write(" {} ".format(x1))
            f.write(" {} ".format(y1))
            f.write(" {} ".format(x2))
            f.write(" {} ".format(y2))
            f.write(" \n")

i=0
for jsonfile in os.listdir(folder_path):
    temp_path=os.path.join(folder_path,jsonfile)
    
    i+=1
    if i>5:
        break
    #如果是一个子目录就继续
    if os.path.isdir(temp_path):
        continue
    print("json_path:\t",temp_path)
    jsonfile_path=temp_path
    with open(jsonfile_path, "r", encoding='utf-8') as f:
        json_d = json.load(f)
        img_name=json_d['imagePath'].split("\\")[-1].split(".")[0]+".jpg"
        img_path=os.path.join(img_folder_path,img_name)
        print("img_path:\t",img_path)
        relative_coordinate_txt(img_name,json_d,img_path)
        absolute_coordinate_txt(img_name,json_d,img_path)

5 从txt查看标注结果

import json
import os
import cv2
img_folder_path=r'F:\imagedata\FOV\FOV_SRC_2\imgs'#图片存放文件夹
folder_path=r"F:\imagedata\FOV\FOV_SRC_2\annotation"#标注数据的文件地址
txt_folder_path = r"F:\imagedata\FOV\FOV_SRC_2\txtlabel"#转换后的txt标签文件存放的文件夹


#相对坐标格式
def show_label_from_txt(img_path,txt_path):
    window_name = ('src') 
    cv2.namedWindow(window_name,cv2.WINDOW_FREERATIO)
    src_img=cv2.imread(img_path)    
    h,w = src_img.shape[:2]
    font = cv2.FONT_HERSHEY_SIMPLEX
    with open(temp_path, "r", encoding='utf-8') as f:
        lines = f.readlines()
    for line in lines:
        data = line.split(' ')
        label = data[1]
        x1 = int((float(data[3]) - float(data[7])/2)*w)
        y1 = int((float(data[5]) - float(data[9])/2)*h)
        x2 = int((float(data[3]) + float(data[7])/2)*w)
        y2 = int((float(data[5]) + float(data[9])/2)*h)      
        p1 = (x1,y1)
        p2 = (x2,y2)
        cv2.rectangle(src_img, p1, p2, (0, 250, 0), 2)
        cv2.putText(src_img,label,p1,font,2,(0,255,255),3)
        cv2.imshow(window_name,src_img)
        cv2.waitKey(0)
    cv2.destroyAllWindows()
    return 

i=0
for txtfile in os.listdir(txt_folder_path):
    temp_path=os.path.join(txt_folder_path,txtfile)
    
    i+=1
    if i>15:
        break
    #如果是一个子目录就继续
    if os.path.isdir(temp_path):
        continue
    print("txt_path:\t",temp_path)
    img_name=txtfile.split("\\")[-1].split(".")[0]+".jpg"
    img_path=os.path.join(img_folder_path,img_name)
    show_label_from_txt(img_path,temp_path)
    

 

 

  • 6
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值