一.MSRA-TD500 : http://www.iapr-tc11.org/mediawiki/index.php/MSRA_Text_Detection_500_Database_%28MSRA-TD500%29
#coding:utf-8
"""
fzh created on 2019/12/6
将MSRA-TD500数据标签转换成按逆时针输出
也即
index,difficulty label,x,y,w,h,0 -->>x1,y1,x2,y2,x3,y3,x4,y4
"""
import os
from math import *
import cv2
import numpy as npdef xy_rotate(angle,x,y,cx,cy):"""点(x,y) 绕(cx,cy)点旋转"""# angle = angle*pi/180#传入的已经是弧度了x_new = (x-cx)*cos(angle) - (y-cy)*sin(angle)+cxy_new = (x-cx)*sin(angle) + (y-cy)*cos(angle)+cyreturn x_new,y_newdef rec_rotate(x, y, width, height, theta):"""传入矩形的x,y和宽度高度,弧度,转成QUAD格式:param x::param y::param width::param height::param theta::return:"""centerx = x + width / 2centery = y + height / 2x1, y1 = xy_rotate(theta, x, y, centerx, centery)x2, y2 = xy_rotate(theta, x + width, y, centerx, centery)x3, y3 = xy_rotate(theta, x, y + height, centerx, centery)x4, y4 = xy_rotate(theta, x + width, y + height, centerx, centery)return list(map(int,[x1, y1, x3, y3, x4, y4, x2, y2]))#逆时针输出
def make_standard_txt():"""制作逆时针的polygon标签:return:"""path = './test/'gt_lists_path = [os.path.join(path, i) for i in os.listdir(path) if '.gt' in i]for i, gt_list_path in enumerate(gt_lists_path):# if i < 1:print('gt_list_path:', gt_list_path)labels = []with open(gt_list_path, 'r', encoding='utf-8') as file:for read_info in file:print('read_info:', read_info)line = list(map(float, read_info.strip().split(' ')))print('line:', line)x, y = line[2], line[3]w, h = line[4], line[5]points = [x, y, x, y + h, x + w, y + h, x + w, y]pointsrotate = rec_rotate(x, y, w, h, line[-1])str_point = ','.join(map(str, pointsrotate)) + ',###'print('str_point:', str_point)labels.append(str_point)savename = gt_list_path[0:-2] + 'txt'print('savename:', savename)with open(savename, 'w', encoding='utf-8') as wfile:[wfile.write(label + '\n') for label in labels]
def read_val():"""将make_standard_txt函数制作的polygon进行验证:return:"""txt_path = './train/IMG_0723.txt'img_path = txt_path.replace('.txt', '.JPG')img = cv2.imread(img_path)print('img.shape:', img.shape)with open(txt_path, 'r', encoding='utf-8') as rfile:for j,read_info in enumerate(rfile):# if j<1:print('read_info:', read_info)point = list(map(int, read_info.strip().split(',')[:-1]))print('point:', point)#需要变成顺时针才用cv2.polylines显示point = [point[0], point[1], point[-2],point[-1], point[4], point[5], point[2], point[3]]cv2.polylines(img, [np.array(point).reshape(-1, 1, 2)], True, (0, 255, 0), thickness=5)cv2.imwrite('img.jpg', img)
if __name__ == '__main__':make_standard_txt()# read_val()
转换成逆时针:
二.labeleme json格式转四个点(顺时针)的txt
import os
import json
import cv2
import numpy as np#校正顺时针的四个点 从左上角开始
def cal_stand_points(points):rect = np.zeros((4, 2))s = np.sum(points, axis=1)rect[0] = points[np.argmin(s)]rect[2] = points[np.argmax(s)]# the top-right point will have the smallest difference,# whereas the bottom-left will have the largest differenced = np.diff(points, axis=1)rect[1] = points[np.argmin(d)]rect[3] = points[np.argmax(d)]return rect
def lableme_json_txt():path ='./效果差的_去章'imgs_list_path = [os.path.join(path, i) for i in os.listdir(path) if '.jpg' in i]print('==len(imgs_list_path)', len(imgs_list_path))for i, img_list_path in enumerate(imgs_list_path):# if i<1:json_list_path = img_list_path.replace('.jpg', '.json')output_txt_path = img_list_path.replace('.jpg', '.txt')with open(json_list_path, 'r') as file:json_info = json.load(file)print('===json_info', json_info)shapes = json_info['shapes']output_points = []for shape in shapes:points = np.array(shape['points']).astype(np.int)points = cal_stand_points(points)print('===points', points)output_points.append(list(map(str, (points.reshape(-1).tolist()))))print('===output_points', output_points)with open(output_txt_path, 'w', encoding='utf-8') as file:[file.write(','.join(out) + ',###\n') for out in output_points]if __name__ == '__main__':lableme_json_txt()