ctd数据格式转普通坐标点
ctd_label2pt.py
import numpy as np
import cv2
import random
import os
def get_absolute_path(p):
if p.startswith('~'):
p = os.path.expanduser(p)
return os.path.abspath(p)
def read_lines(p):
"""return the text in a file in lines as a list """
p = get_absolute_path(p)
f = open(p,'r')
return f.readlines()
def replace_all(s, old, new, reg = False):
if reg:
import re
targets = re.findall(old, s)
for t in targets:
s = s.replace(t, new)
else:
s = s.replace(old, new)
return s
def remove_all(s, sub):
return replace_all(s, sub, '')
def split(s, splitter, reg = False):
if not reg:
return s.split(splitter)
import re
return re.split(splitter, s)
def get_bboxes(img, gt_path, save_path):
h, w = img.shape[0:2]
lines = read_lines(gt_path)
bboxes = []
tags = []
for line in lines:
line = line.strip()
line = remove_all(line, '\xef\xbb\xbf')
gt = split(line, ',')
x1 = np.int(gt[0])
y1 = np.int(gt[1])
bbox = [np.int(gt[i]) for i in range(4, 32)]
bbox = np.asarray(bbox) + ([x1 * 1.0, y1 * 1.0] * 14)
with open(save_path,'a') as fw:
for cnt,val in enumerate(bbox):
val = int(val)
if cnt != len(bbox)-1:
fw.write(str(val))
fw.write(",")
else:
fw.write(str(val))
fw.write('\n')
# def get_bboxes(img, gt_path, save_path):
# h, w = img.shape[0:2]
# lines = util.io.read_lines(gt_path)
# bboxes = []
# tags = []
# for line in lines:
# line = line.strip()
# line = util.str.remove_all(line, '\xef\xbb\xbf')
# gt = util.str.split(line, ',')
#
# x1 = np.int(gt[0])
# y1 = np.int(gt[1])
#
# bbox = [np.int(gt[i]) for i in range(4, 32)]
# bbox = np.asarray(bbox) + ([x1 * 1.0, y1 * 1.0] * 14)
# with open(save_path,'a') as fw:
# for cnt,val in enumerate(bbox):
# val = int(val)
# if cnt != len(bbox)-1:
# fw.write(str(val))
# fw.write(",")
# else:
# fw.write(str(val))
# fw.write('\n')
path_text_image = "/media/data_1/2019_project_test/PSENet/data/CTW1500/train/text_image/"
path_text_label_curve = "/media/data_1/2019_project_test/PSENet/data/CTW1500/train/text_label_curve/"
path_save_txt = os.path.dirname(os.path.dirname(path_text_image)) + '/ctd2general/'
if not os.path.exists(path_save_txt):
os.makedirs(path_save_txt)
list_img = os.listdir(path_text_image)
cnt = 0
for img_name in list_img:
cnt += 1
print("cnt=%d,img=%s"%(cnt,img_name))
img_path = path_text_image + img_name
img = cv2.imread(img_path)
txt_path = path_text_label_curve + img_name.replace('.jpg','.txt')
txt_save = path_save_txt + img_name.replace('.jpg','.txt')
get_bboxes(img, txt_path, txt_save)
好记性不如烂键盘---点滴、积累、进步!