-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdemo_text_detection.py
206 lines (180 loc) · 7.24 KB
/
demo_text_detection.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
200
201
202
203
204
205
206
import json
import sys
import numpy as np
import torch
import matplotlib.pyplot as plt
import cv2
import skimage
import os
import argparse
from hi_sam.modeling.build import model_registry
from hi_sam.modeling.auto_mask_generator import AutoMaskGenerator
import glob
from tqdm import tqdm
from PIL import Image
import random
from utils import utilities
from shapely.geometry import Polygon
import pyclipper
import datetime
import warnings
warnings.filterwarnings("ignore")
def get_args_parser():
parser = argparse.ArgumentParser('Hi-SAM', add_help=False)
parser.add_argument("--input", type=str, required=True, nargs="+",
help="Path to the input image")
parser.add_argument("--output", type=str, default='./demo',
help="A file or directory to save output visualizations.")
parser.add_argument("--model-type", type=str, default="vit_l",
help="The type of model to load, in ['vit_h', 'vit_l', 'vit_b']")
parser.add_argument("--checkpoint", type=str, required=True,
help="The path to the SAM checkpoint to use for mask generation.")
parser.add_argument("--device", type=str, default="cuda",
help="The device to run generation on.")
parser.add_argument("--hier_det", default=True)
parser.add_argument("--dataset", type=str, required=True, default='totaltext',
help="'totaltext' or 'ctw1500', or 'ic15'.")
parser.add_argument("--vis", action='store_true')
parser.add_argument("--zero_shot", action='store_true')
parser.add_argument('--seed', default=42, type=int)
parser.add_argument('--input_size', default=[1024, 1024], type=list)
# self-prompting
parser.add_argument('--attn_layers', default=1, type=int,
help='The number of image to token cross attention layers in model_aligner')
parser.add_argument('--prompt_len', default=12, type=int, help='The number of prompt token')
parser.add_argument('--layout_thresh', type=float, default=0.5)
return parser.parse_args()
def unclip(p, unclip_ratio=2.0):
poly = Polygon(p)
distance = poly.area * unclip_ratio / poly.length
offset = pyclipper.PyclipperOffset()
offset.AddPath(p, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
expanded = np.array(offset.Execute(distance))
return expanded
def polygon2rbox(polygon, image_height, image_width):
rect = cv2.minAreaRect(polygon)
corners = cv2.boxPoints(rect)
corners = np.array(corners, dtype="int")
pts = get_tight_rect(corners, 0, 0, image_height, image_width, 1)
pts = np.array(pts).reshape(-1, 2)
return pts
def get_tight_rect(points, start_x, start_y, image_height, image_width, scale):
points = list(points)
ps = sorted(points, key=lambda x: x[0])
if ps[1][1] > ps[0][1]:
px1 = ps[0][0] * scale + start_x
py1 = ps[0][1] * scale + start_y
px4 = ps[1][0] * scale + start_x
py4 = ps[1][1] * scale + start_y
else:
px1 = ps[1][0] * scale + start_x
py1 = ps[1][1] * scale + start_y
px4 = ps[0][0] * scale + start_x
py4 = ps[0][1] * scale + start_y
if ps[3][1] > ps[2][1]:
px2 = ps[2][0] * scale + start_x
py2 = ps[2][1] * scale + start_y
px3 = ps[3][0] * scale + start_x
py3 = ps[3][1] * scale + start_y
else:
px2 = ps[3][0] * scale + start_x
py2 = ps[3][1] * scale + start_y
px3 = ps[2][0] * scale + start_x
py3 = ps[2][1] * scale + start_y
px1 = min(max(px1, 1), image_width - 1)
px2 = min(max(px2, 1), image_width - 1)
px3 = min(max(px3, 1), image_width - 1)
px4 = min(max(px4, 1), image_width - 1)
py1 = min(max(py1, 1), image_height - 1)
py2 = min(max(py2, 1), image_height - 1)
py3 = min(max(py3, 1), image_height - 1)
py4 = min(max(py4, 1), image_height - 1)
return [px1, py1, px2, py2, px3, py3, px4, py4]
def show_mask(mask, ax, random_color=False, color=None):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = color if color is not None else np.array([30/255, 144/255, 255/255, 0.5])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_masks(masks, filename, image):
plt.figure(figsize=(15, 15))
plt.imshow(image)
for i, mask in enumerate(masks):
mask = mask[0].astype(np.uint8)
# contours, _ = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# for cont in contours:
# epsilon = 0.002 * cv2.arcLength(cont, True)
# approx = cv2.approxPolyDP(cont, epsilon, True)
# pts = approx.reshape((-1, 2))
# if pts.shape[0] < 4:
# continue
# pts = pts.astype(np.int32)
# mask = cv2.fillPoly(np.zeros(mask.shape), [pts], 1)
show_mask(mask, plt.gca(), random_color=True)
plt.axis('off')
plt.savefig(filename, bbox_inches='tight', pad_inches=0)
plt.close()
if __name__ == '__main__':
args = get_args_parser()
seed = args.seed
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
hisam = model_registry[args.model_type](args)
hisam.eval()
hisam.to(args.device)
print("Loaded model")
amg = AutoMaskGenerator(hisam)
if args.dataset == 'totaltext':
if args.zero_shot:
fg_points_num = 50 # assemble text kernel
score_thresh = 0.3
unclip_ratio = 1.5
else:
fg_points_num = 500
score_thresh = 0.95
elif args.dataset == 'ctw1500':
if args.zero_shot:
fg_points_num = 100
score_thresh = 0.6
else:
fg_points_num = 300
score_thresh = 0.7
else:
raise ValueError
if os.path.isdir(args.input[0]):
args.input = [os.path.join(args.input[0], fname) for fname in os.listdir(args.input[0])]
elif len(args.input) == 1:
args.input = glob.glob(os.path.expanduser(args.input[0]))
assert args.input, "The input path(s) was not found"
for path in tqdm(args.input):
img_id = os.path.basename(path).split('.')[0]
if os.path.isdir(args.output):
assert os.path.isdir(args.output), args.output
img_name = os.path.basename(path).split('.')[0] + '.png'
out_filename = os.path.join(args.output, img_name)
else:
assert len(args.input) == 1
out_filename = args.output
image = cv2.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # h, w, 3
img_h, img_w = image.shape[:2]
amg.set_image(image)
masks, scores = amg.predict_text_detection(
from_low_res=False,
fg_points_num=fg_points_num,
batch_points_num=min(fg_points_num, 100),
score_thresh=score_thresh,
nms_thresh=score_thresh,
zero_shot=args.zero_shot,
dataset=args.dataset
)
if masks is not None:
print('Inference done. Start plotting masks.')
show_masks(masks, out_filename, image)
else:
print('no prediction')