用labelme进行矩形标注得到的json文件中的左上和右下两个点坐标,如何转成四个点的txt文件??

最终的txt文件格式: x1,y1,x2,y2,x3,y3,x4,y4,文本内容

因为要做文本识别,所以需要文本内容,如果不需要可自行删除相关内容。

路径换成自己的路径即可。

# -*- coding: UTF-8 -*-
import json
import os
import cv2

img_folder_path = 'imgs'  # 图片存放文件夹
folder_path = 'jsons'  # 标注数据的文件地址
txt_folder_path = 'txts'  # 转换后的txt标签文件存放的文件夹

if not os.path.exists('2txts'):
    os.mkdir('2txts')

# 保存为相对坐标形式 :x1,y1,x2,y2,x3,y3,x4,y4,文本内容
def generate_txt(img_name, json_d, img_path):
    src_img = cv2.imread(img_path)
    # h, w = src_img.shape[:2]
    h, w, c = src_img.shape
    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"]:
                point = item['points']
                x1 = point[0][0]
                y1 = point[0][1]
                x3 = point[1][0]
                y3 = point[1][1]
                width = point[1][0] - point[0][0]
                height = point[1][1] - point[0][1]
                x2 = x1 + width
                y2 = y1
                x4 = x1
                y4 = y3
                text = item['label']
                f.write("{},".format(round(x1, 1)))
                f.write("{},".format(round(y1, 1)))
                f.write("{},".format(round(x2, 1)))
                f.write("{},".format(round(y2, 1)))
                f.write("{},".format(round(x3, 1)))
                f.write("{},".format(round(y3, 1)))
                f.write("{},".format(round(x4, 1)))
                f.write("{},".format(round(y4, 1)))
                f.write("{}".format(text))
                f.write(" \n")
        print('finish!')


for jsonfile in os.listdir(folder_path):
    # os.listdir用来返回指定文件夹包含的文件或文件夹的名字的列表
    temp_path = os.path.join(folder_path, jsonfile)
    print("json_path:\t", temp_path)
    jsonfile_path = temp_path
    with open(jsonfile_path, "r", encoding='GB2312') as fff:
        json_d = json.load(fff, strict=False)
        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)
        txtname = img_name.replace(".jpg", ".txt")
        txtpath = os.path.join(txt_folder_path, txtname)
        if os.path.exists(txtpath):
            continue
        else:
            generate_txt(img_name, json_d, img_path)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一鹿向晗99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值