最终的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)