Spaces:
Runtime error
Runtime error
File size: 6,623 Bytes
c310e19 |
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 |
import os
import numpy as np
import cv2
from shapely.geometry import box, Polygon
from shapely import affinity
import math
def _rect2quad(boxes):
x_min, y_min, x_max, y_max = boxes[:, 0].reshape((-1, 1)), boxes[:, 1].reshape((-1, 1)), boxes[:, 2].reshape((-1, 1)), boxes[:, 3].reshape((-1, 1))
return np.hstack((x_min, y_min, x_max, y_min, x_max, y_max, x_min, y_max))
def _quad2rect(boxes):
## only support rectangle
return np.hstack((boxes[:, 0].reshape((-1, 1)), boxes[:, 1].reshape((-1, 1)), boxes[:, 4].reshape((-1, 1)), boxes[:, 5].reshape((-1, 1))))
def _quad2minrect(boxes):
## trans a quad(N*4) to a rectangle(N*4) which has miniual area to cover it
return np.hstack((boxes[:, ::2].min(axis=1).reshape((-1, 1)), boxes[:, 1::2].min(axis=1).reshape((-1, 1)), boxes[:, ::2].max(axis=1).reshape((-1, 1)), boxes[:, 1::2].max(axis=1).reshape((-1, 1))))
def _quad2boxlist(boxes):
res = []
for i in range(boxes.shape[0]):
res.append([[boxes[i][0], boxes[i][1]], [boxes[i][2], boxes[i][3]], [boxes[i][4], boxes[i][5]], [boxes[i][6], boxes[i][7]]])
return res
def _boxlist2quads(boxlist):
res = np.zeros((len(boxlist), 8))
for i, box in enumerate(boxlist):
# print(box)
res[i] = np.array([box[0][0], box[0][1], box[1][0], box[1][1], box[2][0], box[2][1], box[3][0], box[3][1]])
return res
def _rotate_image(im, polygons, angle):
new_polygons = polygons
## rotate image first
height, width, _ = im.shape
## get the minimal rect to cover the rotated image
img_box = np.array([[0, 0, width, 0, width, height, 0, height]])
rotated_img_box = _quad2minrect(_rotate_polygons(img_box, -1*angle, (width/2, height/2)))
r_height = int(max(rotated_img_box[0][3], rotated_img_box[0][1]) - min(rotated_img_box[0][3], rotated_img_box[0][1]))
r_width = int(max(rotated_img_box[0][2], rotated_img_box[0][0]) - min(rotated_img_box[0][2], rotated_img_box[0][0]))
r_height_padding = max(r_height, height)
r_width_padding = max(r_width, width)
## padding im
im_padding = np.zeros((r_height_padding, r_width_padding, 3))
start_h, start_w = int((r_height_padding - height)/2.0), int((r_width_padding - width)/2.0)
# start_h = max(start_h, 0)
# start_w = max(start_w, 0)
end_h, end_w = start_h + height, start_w + width
# print(start_h, end_h, start_w, end_w, im.shape)
im_padding[start_h:end_h, start_w:end_w, :] = im
M = cv2.getRotationMatrix2D((r_width/2, r_height/2), angle, 1)
im = cv2.warpAffine(im_padding, M, (r_width, r_height))
## polygons
new_polygons = _rotate_segms(polygons, -1*angle, (r_width/2, r_height/2), start_h, start_w)
return im, new_polygons
def _rotate_polygons(polygons, angle, r_c):
## polygons: N*8
## r_x: rotate center x
## r_y: rotate center y
## angle: -15~15
poly_list = _quad2boxlist(polygons)
rotate_boxes_list = []
for poly in poly_list:
box = Polygon(poly)
rbox = affinity.rotate(box, angle, r_c)
if len(list(rbox.exterior.coords))<5:
print(poly)
print(rbox)
# assert(len(list(rbox.exterior.coords))>=5)
rotate_boxes_list.append(rbox.boundary.coords[:-1])
res = _boxlist2quads(rotate_boxes_list)
return res
def _rotate_segms(polygons, angle, r_c, start_h, start_w):
## polygons: N*8
## r_x: rotate center x
## r_y: rotate center y
## angle: -15~15
poly_list=[]
for polygon in polygons:
tmp=[]
for i in range(int(len(polygon) / 2)):
tmp.append([polygon[2*i] + start_w, polygon[2*i+1] + start_h])
poly_list.append(tmp)
rotate_boxes_list = []
for poly in poly_list:
box = Polygon(poly)
rbox = affinity.rotate(box, angle, r_c)
if len(list(rbox.exterior.coords))<5:
print(poly)
print(rbox)
rotate_boxes_list.append(rbox.boundary.coords[:-1])
res = []
for i, box in enumerate(rotate_boxes_list):
tmp = []
for point in box:
tmp.append(point[0])
tmp.append(point[1])
res.append([tmp])
return res
def _read_gt(gt_path):
polygons = []
words = []
with open(gt_path, 'r') as fid:
lines = fid.readlines()
for line in lines:
line = line.strip()
polygon = line.split(',')[:8]
word = line.split(',')[8]
polygon = [float(x) for x in polygon]
polygons.append(polygon)
words.append(word)
return polygons, words
def format_new_gt(polygons, words, new_gt_path):
with open(new_gt_path, 'wt') as fid:
for polygon, word in zip(polygons, words):
# print(polygon)
polygon = [str(int(x)) for x in polygon[0]]
# polygon = [str(int(x)) for x in polygon]
line = ','.join(polygon) + ',' + word
# print(line)
fid.write(line+'\n')
def visu_gt(img, polygons, visu_path):
for polygon in polygons:
pts = np.array(polygon, np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))
cv2.imwrite(visu_path, img)
img_dir = '../datasets/icdar2013/test_images'
gt_dir = '../datasets/icdar2013/test_gts'
angle = 45
new_img_dir = '../datasets/icdar2013/rotated_test_images'+'_'+str(angle)
new_gt_dir = '../datasets/icdar2013/rotated_test_gts'+'_'+str(angle)
if not os.path.isdir(new_img_dir):
os.mkdir(new_img_dir)
if not os.path.isdir(new_gt_dir):
os.mkdir(new_gt_dir)
visu_dir = '../output/visu/'
for i in range(233):
img_name = 'img_' + str(i+1) + '.jpg'
img_path = os.path.join(img_dir, img_name)
img = cv2.imread(img_path)
gt_path = os.path.join(gt_dir, img_name + '.txt')
new_img_path = os.path.join(new_img_dir, img_name)
visu_path = os.path.join(visu_dir, img_name)
new_gt_path = os.path.join(new_gt_dir, 'gt_' + img_name.split('.')[0] + '.txt')
polygons, words = _read_gt(gt_path)
# print(img_name)
if angle == 90:
(h, w) = img.shape[:2]
img = cv2.transpose(img)
img = cv2.flip(img,flipCode=0)
# M = cv2.getRotationMatrix2D(center, 90, 1)
# img = cv2.warpAffine(img, M, (h, w))
new_polygons = [[polygon[1], w-polygon[0], polygon[3], w-polygon[2], polygon[5], w-polygon[4], polygon[7], w-polygon[6]] for polygon in polygons]
else:
img, new_polygons = _rotate_image(img, polygons, angle)
format_new_gt(new_polygons, words, new_gt_path)
# visu_gt(img, new_polygons, visu_path)
cv2.imwrite(new_img_path, img)
|