-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
69 lines (57 loc) · 1.85 KB
/
test.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
import pickle
import numpy as np
import cv2
import os
import tqdm
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--gt', type=str, default='./')
parser.add_argument('--output', type=str, default='./image_test/all_result')
parser.add_argument('--number_test', type=int, default=-1)
FLAGS = parser.parse_args()
g_truth = FLAGS.gt
def check_exists(path):
try:
if not os.path.exists(FLAGS.output):
os.makedirs(FLAGS.output)
except Exception as e:
print(e)
pass
with open('gt.pkl', 'rb') as pkl_file:
datas = pickle.load(pkl_file)
#
path_save_chars = os.path.join(FLAGS.output, "chars")
path_save_words = os.path.join(FLAGS.output, "words")
path_save_text = os.path.join(FLAGS.output, "text")
#
check_exists(path_save_chars)
check_exists(path_save_words)
check_exists(path_save_text)
path = 'data/images'
imgs = datas[:FLAGS.number_test]
for data in tqdm.tqdm(imgs):
image_name = data[0]
word_bboxs = data[1]
texts = data[2]
chars_bboxs = data[3]
# print("all texts: ", texts)
img = cv2.imread(os.path.join(path, image_name))
_n = image_name.split('.')[0]
with open("image_test/all_result/text/{}.txt".format(_n), 'w') as f:
for word_text in texts:
f.write(word_text)
f.write("\n")
word_image = img.copy()
for bb in word_bboxs:
top, _, down, _ = bb
x, y, xx, yy = int(top[0]), int(top[1]), int(down[0]), int(down[1])
word_rect = cv2.rectangle(word_image, (x, y), (xx, yy), (255, 0, 255), 2)
cv2.imwrite('{}/words/word_rect_{}.jpg'.format(FLAGS.output, _n), word_rect)
#
char_image = img.copy()
for char in chars_bboxs:
left_above, _, right_bottom, _ = char
# print(">> char: ", char)
rect = cv2.rectangle(char_image,(int(left_above[0]), int(left_above[1])),\
(int(right_bottom[0]), int(right_bottom[1])), (255,0,255), 2)
cv2.imwrite('{}/chars/char_rect_{}.jpg'.format(FLAGS.output, _n), rect)