Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,71 +4,66 @@ import onnxruntime as rt
|
|
4 |
from torchvision import transforms as T
|
5 |
from PIL import Image
|
6 |
from tokenizer_base import Tokenizer
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
# Инициализация модели
|
12 |
model_file = "captcha.onnx"
|
13 |
img_size = (32,128)
|
14 |
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
15 |
tokenizer_base = Tokenizer(charset)
|
16 |
|
17 |
def get_transform(img_size):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
def to_numpy(tensor):
|
26 |
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
|
27 |
|
28 |
def initialize_model(model_file):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
return transform, ort_session
|
36 |
-
except Exception as e:
|
37 |
-
raise RuntimeError(f"Ошибка при инициализации модели: {e}")
|
38 |
-
|
39 |
-
# Инициализация модели
|
40 |
-
transform, ort_session = initialize_model(model_file)
|
41 |
-
|
42 |
-
# Создаем FastAPI приложение
|
43 |
-
app = FastAPI()
|
44 |
|
45 |
-
# Функция для получения текста
|
46 |
def get_text(img_org):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
logits = ort_session.run(None, ort_inputs)[0]
|
51 |
-
probs = torch.tensor(logits).softmax(-1)
|
52 |
-
preds, _ = tokenizer_base.decode(probs)
|
53 |
-
return preds[0]
|
54 |
-
except Exception as e:
|
55 |
-
raise RuntimeError(f"Ошибка при обработке изображения: {e}")
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
64 |
|
65 |
-
|
66 |
-
result = get_text(img)
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
#
|
74 |
-
#
|
|
|
|
|
|
|
|
4 |
from torchvision import transforms as T
|
5 |
from PIL import Image
|
6 |
from tokenizer_base import Tokenizer
|
7 |
+
import pathlib
|
8 |
+
import os
|
9 |
+
import gradio as gr
|
10 |
+
from huggingface_hub import Repository
|
11 |
+
|
12 |
+
|
13 |
|
|
|
14 |
model_file = "captcha.onnx"
|
15 |
img_size = (32,128)
|
16 |
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
17 |
tokenizer_base = Tokenizer(charset)
|
18 |
|
19 |
def get_transform(img_size):
|
20 |
+
transforms = []
|
21 |
+
transforms.extend([
|
22 |
+
T.Resize(img_size, T.InterpolationMode.BICUBIC),
|
23 |
+
T.ToTensor(),
|
24 |
+
T.Normalize(0.5, 0.5)
|
25 |
+
])
|
26 |
+
return T.Compose(transforms)
|
27 |
|
28 |
def to_numpy(tensor):
|
29 |
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
|
30 |
|
31 |
def initialize_model(model_file):
|
32 |
+
transform = get_transform(img_size)
|
33 |
+
# Onnx model loading
|
34 |
+
onnx_model = onnx.load(model_file)
|
35 |
+
onnx.checker.check_model(onnx_model)
|
36 |
+
ort_session = rt.InferenceSession(model_file)
|
37 |
+
return transform,ort_session
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
39 |
def get_text(img_org):
|
40 |
+
# img_org = Image.open(image_path)
|
41 |
+
# Preprocess. Model expects a batch of images with shape: (B, C, H, W)
|
42 |
+
x = transform(img_org.convert('RGB')).unsqueeze(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# compute ONNX Runtime output prediction
|
45 |
+
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
|
46 |
+
logits = ort_session.run(None, ort_inputs)[0]
|
47 |
+
probs = torch.tensor(logits).softmax(-1)
|
48 |
+
preds, probs = tokenizer_base.decode(probs)
|
49 |
+
preds = preds[0]
|
50 |
+
print(preds)
|
51 |
+
return preds
|
52 |
|
53 |
+
transform,ort_session = initialize_model(model_file=model_file)
|
|
|
54 |
|
55 |
+
gr.Interface(
|
56 |
+
get_text,
|
57 |
+
inputs=gr.Image(type="pil"),
|
58 |
+
outputs=gr.Textbox(),
|
59 |
+
title="Text Captcha Reader",
|
60 |
+
examples=["8000.png","11JW29.png","2a8486.jpg","2nbcx.png",
|
61 |
+
"000679.png","000HU.png","00Uga.png.jpg","00bAQwhAZU.jpg",
|
62 |
+
"00h57kYf.jpg","0EoHdtVb.png","0JS21.png","0p98z.png","10010.png"]
|
63 |
+
).launch()
|
64 |
|
65 |
+
# if __name__ == "__main__":
|
66 |
+
# image_path = "8000.png"
|
67 |
+
# preds,probs = get_text(image_path)
|
68 |
+
# print(preds[0])
|
69 |
+
|