将MSRA-TD500标签转换成逆时针输出标签+labeleme json格式转四个点的txt
生活随笔
收集整理的這篇文章主要介紹了
将MSRA-TD500标签转换成逆时针输出标签+labeleme json格式转四个点的txt
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一.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數(shù)據(jù)標簽轉(zhuǎn)換成按逆時針輸出 也即 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)點旋轉(zhuǎn)"""# angle = angle*pi/180#傳入的已經(jīng)是弧度了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和寬度高度,弧度,轉(zhuǎn)成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函數(shù)制作的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()轉(zhuǎn)換成逆時針:
二.labeleme json格式轉(zhuǎn)四個點(順時針)的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()總結(jié)
以上是生活随笔為你收集整理的将MSRA-TD500标签转换成逆时针输出标签+labeleme json格式转四个点的txt的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux学习之云服务器搭建嵌入式Lin
- 下一篇: 深度学习时出现的一些安装问题+ubunt