forked from Sierkinhane/CRNN_Chinese_Characters_Rec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
75 lines (55 loc) · 1.83 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
70
71
72
73
74
75
import numpy as np
import sys, os
import time
sys.path.append(os.getcwd())
# crnn packages
import torch
from torch.autograd import Variable
import utils
import dataset
from PIL import Image
import models.crnn as crnn
import alphabets
str1 = alphabets.alphabet
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--images_path', type=str, default='test_images/test1.png', help='the path to your images')
opt = parser.parse_args()
# crnn params
# 3p6m_third_ac97p8.pth
crnn_model_path = 'trained_models/mixed_second_finetune_acc97p7.pth'
alphabet = str1
nclass = len(alphabet)+1
# crnn文本信息识别
def crnn_recognition(cropped_image, model):
converter = utils.strLabelConverter(alphabet)
image = cropped_image.convert('L')
##
w = int(image.size[0] / (280 * 1.0 / 160))
transformer = dataset.resizeNormalize((w, 32))
image = transformer(image)
if torch.cuda.is_available():
image = image.cuda()
image = image.view(1, *image.size())
image = Variable(image)
model.eval()
preds = model(image)
_, preds = preds.max(2)
preds = preds.transpose(1, 0).contiguous().view(-1)
preds_size = Variable(torch.IntTensor([preds.size(0)]))
sim_pred = converter.decode(preds.data, preds_size.data, raw=False)
print('results: {0}'.format(sim_pred))
if __name__ == '__main__':
# crnn network
model = crnn.CRNN(32, 1, nclass, 256)
if torch.cuda.is_available():
model = model.cuda()
print('loading pretrained model from {0}'.format(crnn_model_path))
# 导入已经训练好的crnn模型
model.load_state_dict(torch.load(crnn_model_path))
started = time.time()
## read an image
image = Image.open(opt.images_path)
crnn_recognition(image, model)
finished = time.time()
print('elapsed time: {0}'.format(finished-started))