forked from WenmuZhou/PytorchOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec_infer.py
66 lines (54 loc) · 2.08 KB
/
rec_infer.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
# -*- coding: utf-8 -*-
# @Time : 2020/6/16 10:57
# @Author : zhoujun
import os
import sys
import pathlib
# 将 torchocr路径加到python陆经里
__dir__ = pathlib.Path(os.path.abspath(__file__))
sys.path.append(str(__dir__))
sys.path.append(str(__dir__.parent.parent))
import torch
from torch import nn
from torchocr.networks import build_model
from torchocr.datasets.RecDataSet import RecDataProcess
from torchocr.utils import CTCLabelConverter
class RecInfer:
def __init__(self, model_path):
ckpt = torch.load(model_path, map_location='cpu')
cfg = ckpt['cfg']
self.model = build_model(cfg['model'])
state_dict = {}
for k, v in ckpt['state_dict'].items():
state_dict[k.replace('module.', '')] = v
self.model.load_state_dict(state_dict)
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.model.to(self.device)
self.model.eval()
self.process = RecDataProcess(cfg['dataset']['train']['dataset'])
self.converter = CTCLabelConverter(cfg['dataset']['alphabet'])
def predict(self, img):
# 预处理根据训练来
img = self.process.resize_with_specific_height(img)
# img = self.process.width_pad_img(img, 120)
img = self.process.normalize_img(img)
tensor = torch.from_numpy(img.transpose([2, 0, 1])).float()
tensor = tensor.unsqueeze(dim=0)
tensor = tensor.to(self.device)
out = self.model(tensor)
txt = self.converter.decode(out.softmax(dim=2).detach().cpu().numpy())
return txt
def init_args():
import argparse
parser = argparse.ArgumentParser(description='PytorchOCR infer')
parser.add_argument('--model_path', required=True, type=str, help='rec model path')
parser.add_argument('--img_path', required=True, type=str, help='img path for predict')
args = parser.parse_args()
return args
if __name__ == '__main__':
import cv2
args = init_args()
img = cv2.imread(args.img_path)
model = RecInfer(args.model_path)
out = model.predict(img)
print(out)