-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
160 lines (128 loc) ยท 8.17 KB
/
app.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
# -*- coding: utf-8 -*-
import sys
import os
sys.path.append('./deep_text_recognition_benchmark')
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./i-can-read-379204-ce1c5c2f12f5.json"
import uvicorn
import torch
import pickle
import configparser
import requests
import torch.backends.cudnn as cudnn
import torch.utils.data
import torch.nn.functional as F
from fastapi import FastAPI, UploadFile
from transformModel import transform_data
from preprocessImage import crop_image, preprocess_image
from deep_text_recognition_benchmark import demo
from deep_text_recognition_benchmark.model import Model
from deep_text_recognition_benchmark.utils import CTCLabelConverter, AttnLabelConverter
from deep_text_recognition_benchmark.dataset import RawDataset, AlignCollate
# from deep_text_recognition_benchmark.modules import transformation
app = FastAPI(max_request_size=1024*1024*1024)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model_path = './saved_models/pretrained/best_accuracy.pth'
dir_name = "words"
if not os.path.exists(dir_name):
os.mkdir(dir_name)
class Options:
def __init__(self):
self.num_fiducial = 20
self.batch_max_length = 25
self.imgH = 32
self.imgW = 100
self.rgb = False
self.input_channel = 1
self.output_channel = 512
self.hidden_size = 256
self.character = '๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฐ๊ฑ๊ฑ๊ฑฐ๊ฑฑ๊ฑด๊ฑท๊ฑธ๊ฒ๊ฒ๊ฒ๊ฒ๊ฒ๊ฒจ๊ฒฉ๊ฒช๊ฒฌ๊ฒฐ๊ฒน๊ฒฝ๊ณ๊ณ๊ณ ๊ณก๊ณค๊ณง๊ณจ๊ณฐ๊ณฑ๊ณณ๊ณต๊ณผ๊ด๊ด๊ด๊ดด๊ต๊ต๊ตฌ๊ตญ๊ตฐ๊ตณ๊ตด๊ตต๊ตถ๊ตฝ๊ถ๊ถ๊ท๊ท๊ท๊ท ๊ทค๊ทธ๊ทน๊ทผ๊ธ๊ธ๊ธ๊ธ๊ธ๊ธ๊ธฐ๊ธด๊ธธ๊น๊น
๊น๊น๊น๊น๊น๊น๊น๊น๊นก๊นฅ๊นจ๊บผ๊บพ๊ป๊ป๊ป๊ป๊ป๊ปด๊ผฌ๊ผญ๊ผด๊ผผ๊ผฝ๊ฝ๊ฝ๊ฝ๊ฝค๊พธ๊พผ๊ฟ๊ฟ๋๋๋๋๋๋๋๋๋๋ผ๋๋๋๋๋๋ ๋ก๋จ๋ฉ๋ซ๋ญ๋ฎ๋ฏ๋ฑ๋ณ๋ด๋๋๋๋๋ฅ๋๋๋๋๋๋๋ฃ๋ค๋ฅ๋ท๋
๋
๋
๋
๋
๋
ธ๋
น๋
ผ๋๋๋๋๋๋๋๋จ๋๋๋๋๋ด๋๋๋๋๋๋๋ฅ๋ฆ๋ฌ๋๋๋๋ค๋ฅ๋ฆ๋จ๋ซ๋ฌ๋ญ๋ฎ๋ด๋ต๋ท๋น๋ฟ๋๋๋๋๋๋๋๋๋๋ค๋ฅ๋ง๋ฉ๋ฎ๋ฐ๋ธ๋๋
๋๋๋๋๋๋ผ๋๋๋๋๋๋ ๋ก๋ฅ๋ค๋ท๋๋๋ ๋ฃ๋ค๋ฌ๋ญ๋ฏ๋ฑ๋๋ฉ๋ช๋ฐ๋ฑ๋ด๋ธ๋๋
๋๋๋ ๋ก๋ค๋จ๋ป๋ผ๋๋๋๋ซ๋ฑ๋ฐ๋จ๋ฉ๋ฏ๋ฐ๋ป๋๋ผ๋ฝ๋๋๋๋๋๋๋๋จ๋ซ๋ต๋๋ฌ๋ญ๋ฐ๋ด๋ผ๋ฝ๋ฟ๋ ๋ ๋ ๋ ๋ ๋ ค๋ ฅ๋ จ๋ ฌ๋ ต๋ น๋ก๋ก๋ก๋ก ๋กฌ๋กญ๋กฏ๋ฃ๋ฃจ๋ฃฉ๋ฃน๋ฃป๋ค๋ฅ๋ฅ๋ฅ ๋ฅญ๋ฅด๋ฅธ๋ฆ๋ฆ๋ฆ๋ฆฌ๋ฆญ๋ฆฐ๋ฆผ๋ฆฝ๋ฆฟ๋ง๋ง๋ง๋ง๋ง๋ง๋ง๋ง๋ง๋ง๋ง๋ง๋งก๋งฃ๋งค๋งฅ๋งจ๋งต๋งบ๋จธ๋จน๋จผ๋ฉ๋ฉ๋ฉ๋ฉ๋ฉ๋ฉ๋ฉ๋ฉฉ๋ฉฐ๋ฉด๋ฉธ๋ช
๋ช๋ชจ๋ชฉ๋ชฌ๋ชฐ๋ชธ๋ชน๋ชป๋ชฝ๋ฌ๋ฌด๋ฌต๋ฌถ๋ฌธ๋ฌป๋ฌผ๋ญ๋ญ๋ญ๋ญ๋ญฃ๋ฏ๋ฏธ๋ฏผ๋ฏฟ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐ๋ฐค๋ฐฅ๋ฐฉ๋ฐญ๋ฐฐ๋ฐฑ๋ฑ๋ฑ๋ฑ๋ฒ๋ฒ๋ฒ๋ฒ๋ฒ๋ฒ๋ฒ ๋ฒค๋ฒจ๋ฒผ๋ฒฝ๋ณ๋ณ๋ณ๋ณ๋ณ๋ณด๋ณต๋ณถ๋ณธ๋ณผ๋ด๋ด๋ด๋ต๋ต๋ถ๋ถ๋ถ๋ถ๋ถ๋ถ๋ถ๋ถ๋ถ๋ทฐ๋ธ๋ธ๋ธ๋น๋น๋น๋น๋น๋น๋น ๋นก๋นจ๋นต๋นผ๋บ๋บจ๋ป๋ป๋ป๋ผ๋ผ๋ฝ๋ฟ๋ฟ์์จ์ฌ์ญ์ฐ์ด์ถ์ผ์ฟ์์์์์์ค์์์์ ์ค์ฌ์ญ์ฏ์ฑ์ธ์น์ผ์
์
์
์
์์์์์์์ก์ฅ์์ ์ผ์์์์์ ์จ์ซ์ญ์ฒ์ฌ์ฐ์ฝ์์ค์จ์ฌ์ด์ต์ท์น์์์ ์ฃ์ค์ซ์ฌ์ญ์ฏ์ฑ์ถ์ธ์น์ผ์์์์จ์ฉ์ฐ์น์์์์ค์ฐ์ด์ธ์์์จ์ฉ์ฌ์น์ป์์
์์์์์์์์์์์ ์ก์จ์ผ์ฝ์์์์์์์์ด์ต์ธ์น์ป์ผ์์
์์์์์์์์์์ฌ์ญ์ฐ์ด์ท์ผ์ฝ์ฟ์์์์์ค์ฅ์จ์ฌ์ฎ์ณ์ท์น์์์์์ ์ธ์ผ์์์ฉ์ฐ์ฑ์ด์ธ์์์
์์์์จ์ฌ์์์ ์ก์จ์ผ์ฝ์์์์์์ด์ต์ธ์ผ์ฝ์์์
์์์์์์์์์์ ์ก์ฃ์ฅ์ฆ์ฌ์์ค์ ์ ์ ์ ์ ์ ์ ์ ์ ์ ์ ์ ์ ฏ์ ธ์กฐ์กฑ์กด์กธ์ข์ข์ข
์ข์ข์ฃ์ฃผ์ฃฝ์ค์ค์ค์ค์ค์ฅ์ฆ์ฆ์ฆ์ฆ์ฆ์ฆ์ง์ง์ง์ง์ง์ง์ง์ง์ง์ง์ง์ง์งง์งธ์จ์ฉ์ฉ์ฉ์ฉ์ฉ์ชฝ์ซ์ญ์ญ์ฐ์ฐ์ฐข์ฐจ์ฐฉ์ฐฌ์ฐฎ์ฐฐ์ฐธ์ฐป์ฐฝ์ฐพ์ฑ์ฑ
์ฑ์ฑ์ฒ์ฒ์ฒ์ฒ ์ฒฉ์ฒซ์ฒญ์ฒด์ณ์ด์ด์ด์ด์ด์ดฌ์ต์ถ์ถ์ถ์ถ์ถค์ถฅ์ถง์ถฉ์ทจ์ธ ์ธก์ธฐ์ธต์น์น์น์น ์นจ์นซ์นญ์นด์นธ์นผ์บ์บ์บ ์ปค์ปจ์ปฌ์ปด์ปต์ปท์ผ์ผ์ผ์ฝ์ฝ์ฝ์ฝค์ฝฉ์พ์ฟ์ฟ ํดํฌํฐํดํผํคํฌํํํํํํํํํํฐํฑํดํธํ
ํ
ํ
ํ
ํ
ํ ํคํจํฑํตํดํฌํดํผํํํํธํนํผํฟํํํฐํฑํํ
ํํํํํํจํฉํฌํผํฝํํํดํธํผํํํฌํญํฐํํธํนํํํํจํํํํผํฝํํํํํํํ ํจํฉํญํดํตํธํํํํฅํํํํคํฌํํํํํํํธํนํผํํํํํํํํํฉํํํํกํจํํํํจํํดํํํํํํํกํฅํฉํฌํฐํํ?!.,()'
self.Transformation = 'TPS'
self.FeatureExtraction = 'ResNet'
self.SequenceModeling = 'BiLSTM'
self.Prediction = 'CTC'
self.saved_model = model_path
self.image_folder = "./words"
self.workers = 0
self.batch_size = 64
config = configparser.ConfigParser()
config.read('config.ini')
host = config.get('server', 'host')
port = config.getint('server', 'port')
@app.get("/")
async def root():
return {"message": f"Server running on {host}:{port}"}
@app.post('/api/v1/menu/extract')
async def extract_text(file: UploadFile):
results = []
menu = preprocess_image(file)
crop_image(menu)
opt = Options()
if 'CTC' in opt.Prediction:
converter = CTCLabelConverter(opt.character)
else:
converter = AttnLabelConverter(opt.character)
opt.num_class = len(converter.character)
if opt.rgb:
opt.input_channel = 3
model = Model(opt)
# model = torch.nn.DataParallel(model).to(device)
# model.load_state_dict(torch.load(opt.saved_model, map_location=device))
transform_data(model, './saved_models/cafe/transform.pth')
## Load the .pkl file
with open('./saved_models/new_model.pkl', 'rb') as f:
state_dict = pickle.load(f)
## When I use just .pth file
# torch.save(model.state_dict(), new_model_path)
# model.load_state_dict(torch.load(new_model_path))
#
# state_dict = torch.load(new_model_path)
# new_state_dict = {}
# for k, v in state_dict.items():
# name = k.replace('module.', '') # remove 'module.' from key name
# new_state_dict[name] = v
# model.load_state_dict(new_state_dict)
# resize the Prediction.bias tensor to match the size in the checkpoint
if 'Prediction.bias' in model.state_dict():
new_bias_size = model.state_dict()['Prediction.bias'].size()
old_bias_size = state_dict['Prediction.bias'].size()
if old_bias_size != new_bias_size:
state_dict['module.Prediction.bias'] = state_dict['module.Prediction.bias'][:new_bias_size[0]]
model.load_state_dict(state_dict, strict=False)
AlignCollate_demo = AlignCollate(imgH=opt.imgH, imgW=opt.imgW)
demo_data = RawDataset(root=opt.image_folder, opt=opt) # use RawDataset
demo_loader = torch.utils.data.DataLoader(
demo_data, batch_size=opt.batch_size,
shuffle=False,
num_workers=int(opt.workers),
collate_fn=AlignCollate_demo, pin_memory=True)
# predict
model.eval()
with torch.no_grad():
for image_tensors, image_path_list in demo_loader:
batch_size = image_tensors.size(0)
image = image_tensors.to(device)
# For max length prediction
length_for_pred = torch.IntTensor([opt.batch_max_length] * batch_size).to(device)
text_for_pred = torch.LongTensor(batch_size, opt.batch_max_length + 1).fill_(0).to(device)
if 'CTC' in opt.Prediction:
preds = model(image, text_for_pred)
preds_size = torch.IntTensor([preds.size(1)] * batch_size)
_, preds_index = preds.max(2)
preds_str = converter.decode(preds_index, preds_size)
else:
preds = model(image, text_for_pred, is_train=False)
_, preds_index = preds.max(2)
preds_str = converter.decode(preds_index, length_for_pred)
preds_prob = F.softmax(preds, dim=2)
preds_max_prob, _ = preds_prob.max(dim=2)
for img_name, pred, pred_max_prob in zip(image_path_list, preds_str, preds_max_prob):
if 'Attn' in opt.Prediction:
pred_EOS = pred.find('[s]')
pred = pred[:pred_EOS] # prune after "end of sentence" token ([s])
result = pred
# print(f'{img_name:25s}\t{pred:25s}')
results.append(result)
print(results)
return results
if __name__ == '__main__':
uvicorn.run(app, host=host, port=port)