-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathCTW1500_Text_New.py
199 lines (170 loc) · 7.6 KB
/
CTW1500_Text_New.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '古溪'
import os
import cv2
import numpy as np
from dataset.data_util import pil_load_img
from dataset.dataload import TextDataset, TextInstance
from util.io import read_lines
from lxml import etree as ET
class Ctw1500Text_New(TextDataset):
def __init__(self, data_root, is_training=True, load_memory=False, transform=None, ignore_list=None):
super().__init__(transform, is_training)
self.data_root = data_root
self.is_training = is_training
self.load_memory = load_memory
self.image_root = os.path.join(data_root, 'Images', 'Train' if is_training else 'Test')
self.annotation_root = os.path.join(data_root, 'gt', 'train_labels' if is_training else 'test_labels')
self.image_list = os.listdir(self.image_root)
self.annotation_list = ['{}'.format(img_name.replace('.jpg', '')) for img_name in self.image_list]
if self.load_memory:
self.datas = list()
for item in range(len(self.image_list)):
self.datas.append(self.load_img_gt(item))
@staticmethod
def parse_carve_txt(gt_path):
"""
.mat file parser
:param gt_path: (str), mat file path
:return: (list), TextInstance
"""
lines = read_lines(gt_path + ".txt")
polygons = []
for line in lines:
line = line.split(",")
gt = list(map(int, line[:-1]))
pts = np.stack([gt[0::2], gt[1::2]]).T.astype(np.int32)
label = line[-1].split("###")[-1].replace("###", "#")
polygons.append(TextInstance(pts, 'c', label))
return polygons
@staticmethod
def parse_carve_xml(gt_path):
"""
.mat file parser
:param gt_path: (str), mat file path
:return: (list), TextInstance
"""
root = ET.parse(gt_path + ".xml").getroot()
polygons = []
for tag in root.findall('image/box'):
label = tag.find("label").text.replace("###", "#")
gt = list(map(int, tag.find("segs").text.split(",")))
pts = np.stack([gt[0::2], gt[1::2]]).T.astype(np.int32)
polygons.append(TextInstance(pts, 'c', label))
return polygons
def load_img_gt(self, item):
image_id = self.image_list[item]
image_path = os.path.join(self.image_root, image_id)
# Read image data
image = pil_load_img(image_path)
try:
h, w, c = image.shape
assert (c == 3)
except:
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = np.array(image)
# Read annotation
if self.is_training:
annotation_id = self.annotation_list[item]
annotation_path = os.path.join(self.annotation_root, annotation_id)
polygons = self.parse_carve_xml(annotation_path)
pass
else:
annotation_id = self.annotation_list[item]
annotation_path = os.path.join(self.annotation_root, "000" + annotation_id)
polygons = self.parse_carve_txt(annotation_path)
data = dict()
data["image"] = image
data["polygons"] = polygons
data["image_id"] = image_id.split("/")[-1]
data["image_path"] = image_path
return data
def __getitem__(self, item):
if self.load_memory:
data = self.datas[item]
else:
data = self.load_img_gt(item)
if self.is_training:
return self.get_training_data(data["image"], data["polygons"],
image_id=data["image_id"], image_path=data["image_path"])
else:
return self.get_test_data(data["image"], data["polygons"],
image_id=data["image_id"], image_path=data["image_path"])
def __len__(self):
return len(self.image_list)
if __name__ == '__main__':
from util.augmentation import Augmentation
from util import canvas as cav
import time
means = (0.485, 0.456, 0.406)
stds = (0.229, 0.224, 0.225)
transform = Augmentation(
size=640, mean=means, std=stds
)
trainset = Ctw1500Text_New(
data_root='../data/CTW-1500',
is_training=True,
transform=transform
)
for idx in range(0, len(trainset)):
t0 = time.time()
img, train_mask, tr_mask, distance_field, \
direction_field, weight_matrix, ctrl_points, proposal_points, ignore_tags = trainset[idx]
img, train_mask, tr_mask, distance_field, \
direction_field, weight_matrix, ctrl_points, proposal_points, ignore_tags\
= map(lambda x: x.cpu().numpy(),
(img, train_mask, tr_mask, distance_field,
direction_field, weight_matrix, ctrl_points, proposal_points, ignore_tags))
img = img.transpose(1, 2, 0)
img = ((img * stds + means) * 255).astype(np.uint8)
boundary_point = ctrl_points[np.where(ignore_tags != 0)[0]]
for i, bpts in enumerate(boundary_point):
cv2.drawContours(img, [bpts.astype(np.int32)], -1, (0, 255, 0), 1)
for j, pp in enumerate(bpts):
if j == 0:
cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (255, 0, 255), -1)
elif j == 1:
cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (0, 255, 255), -1)
else:
cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (0, 0, 255), -1)
ppts = proposal_points[i]
cv2.drawContours(img, [ppts.astype(np.int32)], -1, (0, 0, 255), 1)
for j, pp in enumerate(ppts):
if j == 0:
cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (255, 0, 255), -1)
elif j == 1:
cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (0, 255, 255), -1)
else:
cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (0, 0, 255), -1)
cv2.imshow('imgs', img)
cv2.waitKey(0)
# from util.misc import split_edge_seqence
# from cfglib.config import config as cfg
#
# ret, labels = cv2.connectedComponents(np.array(distance_field >0.35, dtype=np.uint8), connectivity=4)
# for idx in range(1, ret):
# text_mask = labels == idx
# ist_id = int(np.sum(text_mask*tr_mask)/np.sum(text_mask))-1
# contours, _ = cv2.findContours(text_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# epsilon = 0.007 * cv2.arcLength(contours[0], True)
# approx = cv2.approxPolyDP(contours[0], epsilon, True).reshape((-1, 2))
#
# pts_num = approx.shape[0]
# e_index = [(i, (i + 1) % pts_num) for i in range(pts_num)]
# control_points = split_edge_seqence(approx, e_index, cfg.num_points)
# control_points = np.array(control_points[:cfg.num_points, :]).astype(np.int32)
#
# cv2.drawContours(img, [ctrl_points[ist_id].astype(np.int32)], -1, (0, 255, 0), 1)
# cv2.drawContours(img, [control_points.astype(np.int32)], -1, (0, 0, 255), 1)
# for j, pp in enumerate(control_points):
# if j == 0:
# cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (255, 0, 255), -1)
# elif j == 1:
# cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (0, 255, 255), -1)
# else:
# cv2.circle(img, (int(pp[0]), int(pp[1])), 2, (0, 255, 0), -1)
#
# cv2.imshow('imgs', img)
# cv2.waitKey(0)