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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# DB/data/processes/random_crop_data.py
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import math
|
| 6 |
+
import cv2
|
| 7 |
+
import imgaug
|
| 8 |
+
import imgaug.augmenters as iaa
|
| 9 |
+
|
| 10 |
+
# from .data_process import DataProcess
|
| 11 |
+
# from concern.config import Configurable, State
|
| 12 |
+
|
| 13 |
+
class State:
|
| 14 |
+
def __init__(self, autoload=True, default=None):
|
| 15 |
+
self.autoload = autoload
|
| 16 |
+
self.default = default
|
| 17 |
+
|
| 18 |
+
# random crop algorithm similar to https://github.com/argman/EAST
|
| 19 |
+
class RandomCropData():
|
| 20 |
+
size = (512, 512)
|
| 21 |
+
max_tries = 50
|
| 22 |
+
min_crop_side_ratio = 0.1
|
| 23 |
+
require_original_image = False
|
| 24 |
+
|
| 25 |
+
def __init__(self, **kwargs):
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
def process(self, data):
|
| 29 |
+
img = data['image']
|
| 30 |
+
ori_img = img
|
| 31 |
+
ori_lines = data['polys']
|
| 32 |
+
|
| 33 |
+
all_care_polys = [line['points']
|
| 34 |
+
for line in data['polys'] if not line['ignore']]
|
| 35 |
+
crop_x, crop_y, crop_w, crop_h = self.crop_area(img, all_care_polys)
|
| 36 |
+
scale_w = self.size[0] / crop_w
|
| 37 |
+
scale_h = self.size[1] / crop_h
|
| 38 |
+
scale = min(scale_w, scale_h)
|
| 39 |
+
h = int(crop_h * scale)
|
| 40 |
+
w = int(crop_w * scale)
|
| 41 |
+
padimg = np.zeros(
|
| 42 |
+
(self.size[1], self.size[0], img.shape[2]), img.dtype)
|
| 43 |
+
padimg[:h, :w] = cv2.resize(
|
| 44 |
+
img[crop_y:crop_y + crop_h, crop_x:crop_x + crop_w], (w, h))
|
| 45 |
+
img = padimg
|
| 46 |
+
|
| 47 |
+
lines = []
|
| 48 |
+
for line in data['polys']:
|
| 49 |
+
poly = ((np.array(line['points']) -
|
| 50 |
+
(crop_x, crop_y)) * scale).tolist()
|
| 51 |
+
if not self.is_poly_outside_rect(poly, 0, 0, w, h):
|
| 52 |
+
lines.append({**line, 'points': poly})
|
| 53 |
+
data['polys'] = lines
|
| 54 |
+
|
| 55 |
+
if self.require_original_image:
|
| 56 |
+
data['image'] = ori_img
|
| 57 |
+
else:
|
| 58 |
+
data['image'] = img
|
| 59 |
+
data['lines'] = ori_lines
|
| 60 |
+
data['scale_w'] = scale
|
| 61 |
+
data['scale_h'] = scale
|
| 62 |
+
|
| 63 |
+
return data
|
| 64 |
+
|
| 65 |
+
def is_poly_in_rect(self, poly, x, y, w, h):
|
| 66 |
+
poly = np.array(poly)
|
| 67 |
+
if poly[:, 0].min() < x or poly[:, 0].max() > x + w:
|
| 68 |
+
return False
|
| 69 |
+
if poly[:, 1].min() < y or poly[:, 1].max() > y + h:
|
| 70 |
+
return False
|
| 71 |
+
return True
|
| 72 |
+
|
| 73 |
+
def is_poly_outside_rect(self, poly, x, y, w, h):
|
| 74 |
+
poly = np.array(poly)
|
| 75 |
+
if poly[:, 0].max() < x or poly[:, 0].min() > x + w:
|
| 76 |
+
return True
|
| 77 |
+
if poly[:, 1].max() < y or poly[:, 1].min() > y + h:
|
| 78 |
+
return True
|
| 79 |
+
return False
|
| 80 |
+
|
| 81 |
+
def split_regions(self, axis):
|
| 82 |
+
regions = []
|
| 83 |
+
min_axis = 0
|
| 84 |
+
for i in range(1, axis.shape[0]):
|
| 85 |
+
if axis[i] != axis[i-1] + 1:
|
| 86 |
+
region = axis[min_axis:i]
|
| 87 |
+
min_axis = i
|
| 88 |
+
regions.append(region)
|
| 89 |
+
return regions
|
| 90 |
+
|
| 91 |
+
def random_select(self, axis, max_size):
|
| 92 |
+
xx = np.random.choice(axis, size=2)
|
| 93 |
+
xmin = np.min(xx)
|
| 94 |
+
xmax = np.max(xx)
|
| 95 |
+
xmin = np.clip(xmin, 0, max_size - 1)
|
| 96 |
+
xmax = np.clip(xmax, 0, max_size - 1)
|
| 97 |
+
return xmin, xmax
|
| 98 |
+
|
| 99 |
+
def region_wise_random_select(self, regions, max_size):
|
| 100 |
+
selected_index = list(np.random.choice(len(regions), 2))
|
| 101 |
+
selected_values = []
|
| 102 |
+
for index in selected_index:
|
| 103 |
+
axis = regions[index]
|
| 104 |
+
xx = int(np.random.choice(axis, size=1))
|
| 105 |
+
selected_values.append(xx)
|
| 106 |
+
xmin = min(selected_values)
|
| 107 |
+
xmax = max(selected_values)
|
| 108 |
+
return xmin, xmax
|
| 109 |
+
|
| 110 |
+
def crop_area(self, img, polys):
|
| 111 |
+
h, w, _ = img.shape
|
| 112 |
+
h_array = np.zeros(h, dtype=np.int32)
|
| 113 |
+
w_array = np.zeros(w, dtype=np.int32)
|
| 114 |
+
for points in polys:
|
| 115 |
+
points = np.round(points, decimals=0).astype(np.int32)
|
| 116 |
+
minx = np.min(points[:, 0])
|
| 117 |
+
maxx = np.max(points[:, 0])
|
| 118 |
+
w_array[minx:maxx] = 1
|
| 119 |
+
miny = np.min(points[:, 1])
|
| 120 |
+
maxy = np.max(points[:, 1])
|
| 121 |
+
h_array[miny:maxy] = 1
|
| 122 |
+
# ensure the cropped area not across a text
|
| 123 |
+
h_axis = np.where(h_array == 0)[0]
|
| 124 |
+
w_axis = np.where(w_array == 0)[0]
|
| 125 |
+
|
| 126 |
+
if len(h_axis) == 0 or len(w_axis) == 0:
|
| 127 |
+
return 0, 0, w, h
|
| 128 |
+
|
| 129 |
+
h_regions = self.split_regions(h_axis)
|
| 130 |
+
w_regions = self.split_regions(w_axis)
|
| 131 |
+
|
| 132 |
+
for i in range(self.max_tries):
|
| 133 |
+
if len(w_regions) > 1:
|
| 134 |
+
xmin, xmax = self.region_wise_random_select(w_regions, w)
|
| 135 |
+
else:
|
| 136 |
+
xmin, xmax = self.random_select(w_axis, w)
|
| 137 |
+
if len(h_regions) > 1:
|
| 138 |
+
ymin, ymax = self.region_wise_random_select(h_regions, h)
|
| 139 |
+
else:
|
| 140 |
+
ymin, ymax = self.random_select(h_axis, h)
|
| 141 |
+
|
| 142 |
+
if xmax - xmin < self.min_crop_side_ratio * w or ymax - ymin < self.min_crop_side_ratio * h:
|
| 143 |
+
# area too small
|
| 144 |
+
continue
|
| 145 |
+
num_poly_in_rect = 0
|
| 146 |
+
for poly in polys:
|
| 147 |
+
if not self.is_poly_outside_rect(poly, xmin, ymin, xmax - xmin, ymax - ymin):
|
| 148 |
+
num_poly_in_rect += 1
|
| 149 |
+
break
|
| 150 |
+
|
| 151 |
+
if num_poly_in_rect > 0:
|
| 152 |
+
return xmin, ymin, xmax - xmin, ymax - ymin
|
| 153 |
+
|
| 154 |
+
return 0, 0, w, h
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
if __name__ == "__main__":
|
| 158 |
+
|
| 159 |
+
im = './datasets/icdar2015/train_images/img_1.jpg'
|
| 160 |
+
gt = './datasets/icdar2015/train_gts/gt_img_1.txt'
|
| 161 |
+
|
| 162 |
+
items = []
|
| 163 |
+
reader = open(gt, 'r').readlines()
|
| 164 |
+
for line in reader:
|
| 165 |
+
item = {}
|
| 166 |
+
parts = line.strip().split(',')
|
| 167 |
+
label = parts[-1]
|
| 168 |
+
if 'TD' in gt and label == '1':
|
| 169 |
+
label = '###'
|
| 170 |
+
line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in parts]
|
| 171 |
+
if 'icdar' in gt:
|
| 172 |
+
poly = np.array(list(map(float, line[:8]))).reshape(
|
| 173 |
+
(-1, 2)).tolist()
|
| 174 |
+
else:
|
| 175 |
+
num_points = math.floor((len(line) - 1) / 2) * 2
|
| 176 |
+
poly = np.array(list(map(float, line[:num_points]))).reshape(
|
| 177 |
+
(-1, 2)).tolist()
|
| 178 |
+
item['points'] = poly # 多边形是用一个个的点表示的,起点连接第二个点,第二个连接第三个 ... 最后一点连接起点,构成一个闭合的区域
|
| 179 |
+
item['text'] = label
|
| 180 |
+
item['ignore'] = True if label == '###' else False # 此标记表示文字模糊不可辨认,文本框的标记是不可靠的
|
| 181 |
+
items.append( item )
|
| 182 |
+
|
| 183 |
+
img = cv2.imdecode(np.fromfile(im, dtype=np.uint8), -1)
|
| 184 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 185 |
+
img_shape = img.shape
|
| 186 |
+
|
| 187 |
+
data = dict(
|
| 188 |
+
image = img,
|
| 189 |
+
polys = items,
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
crop = RandomCropData()
|
| 193 |
+
crop.process(data)
|