forked from princewang1994/TextSnake.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
110 lines (84 loc) · 3.47 KB
/
demo.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
import os
import time
import cv2
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.utils.data as data
from dataset.deploy import DeployDataset
from network.textnet import TextNet
from util.detection import TextDetector
from util.augmentation import BaseTransform
from util.config import config as cfg, update_config, print_config
from util.option import BaseOptions
from util.visualize import visualize_detection
from util.misc import to_device, mkdirs, rescale_result
def write_to_file(contours, file_path):
"""
:param contours: [[x1, y1], [x2, y2]... [xn, yn]]
:param file_path: target file path
"""
# according to total-text evaluation method, output file shoud be formatted to: y0,x0, ..... yn,xn
with open(file_path, 'w') as f:
for cont in contours:
cont = np.stack([cont[:, 1], cont[:, 0]], 1)
cont = cont.flatten().astype(str).tolist()
cont = ','.join(cont)
f.write(cont + '\n')
def inference(detector, test_loader, output_dir):
total_time = 0.
for i, (image, meta) in enumerate(test_loader):
image = to_device(image)
torch.cuda.synchronize()
start = time.time()
idx = 0 # test mode can only run with batch_size == 1
# get detection result
contours, output = detector.detect(image)
torch.cuda.synchronize()
end = time.time()
total_time += end - start
fps = (i + 1) / total_time
print('detect {} / {} images: {}. ({:.2f} fps)'.format(i, len(test_loader), meta['image_id'][idx], fps))
# visualization
tr_pred, tcl_pred = output['tr'], output['tcl']
img_show = image[idx].permute(1, 2, 0).cpu().numpy()
img_show = ((img_show * cfg.stds + cfg.means) * 255).astype(np.uint8)
H, W = meta['Height'][idx].item(), meta['Width'][idx].item()
img_show, contours = rescale_result(img_show, contours, H, W)
pred_vis = visualize_detection(img_show, contours)
path = os.path.join(cfg.vis_dir, '{}_deploy'.format(cfg.exp_name), meta['image_id'][idx])
cv2.imwrite(path, pred_vis)
# write to file
mkdirs(output_dir)
write_to_file(contours, os.path.join(output_dir, meta['image_id'][idx].replace('jpg', 'txt')))
def main():
testset = DeployDataset(
image_root=cfg.img_root,
transform=BaseTransform(size=cfg.input_size, mean=cfg.means, std=cfg.stds)
)
test_loader = data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=cfg.num_workers)
# Model
model = TextNet(is_training=False, backbone=cfg.net)
model_path = os.path.join(cfg.save_dir, cfg.exp_name, \
'textsnake_{}_{}.pth'.format(model.backbone_name, cfg.checkepoch))
model.load_model(model_path)
# copy to cuda
model = model.to(cfg.device)
if cfg.cuda:
cudnn.benchmark = True
detector = TextDetector(model, tr_thresh=cfg.tr_thresh, tcl_thresh=cfg.tcl_thresh)
print('Start testing TextSnake.')
output_dir = os.path.join(cfg.output_dir, cfg.exp_name)
inference(detector, test_loader, output_dir)
if __name__ == "__main__":
# parse arguments
option = BaseOptions()
args = option.initialize()
assert args.img_root is not None, 'option --img_root must be set'
update_config(cfg, args)
print_config(cfg)
vis_dir = os.path.join(cfg.vis_dir, '{}_deploy'.format(cfg.exp_name))
if not os.path.exists(vis_dir):
mkdirs(vis_dir)
# main
main()