Spaces:
Paused
Paused
Update app.py
#1
by
Phu92kt
- opened
app.py
CHANGED
|
@@ -4,18 +4,14 @@ import onnxruntime as rt
|
|
| 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 |
model_file = "captcha.onnx"
|
| 14 |
-
img_size = (32,128)
|
| 15 |
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
|
|
|
| 16 |
tokenizer_base = Tokenizer(charset)
|
| 17 |
|
| 18 |
-
# Преобразования для изображения
|
| 19 |
def get_transform(img_size):
|
| 20 |
transforms = [
|
| 21 |
T.Resize(img_size, T.InterpolationMode.BICUBIC),
|
|
@@ -24,11 +20,9 @@ def get_transform(img_size):
|
|
| 24 |
]
|
| 25 |
return T.Compose(transforms)
|
| 26 |
|
| 27 |
-
# Преобразование тензора в numpy
|
| 28 |
def to_numpy(tensor):
|
| 29 |
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
|
| 30 |
|
| 31 |
-
# Инициализация модели ONNX
|
| 32 |
def initialize_model(model_file):
|
| 33 |
transform = get_transform(img_size)
|
| 34 |
onnx_model = onnx.load(model_file)
|
|
@@ -36,33 +30,25 @@ def initialize_model(model_file):
|
|
| 36 |
ort_session = rt.InferenceSession(model_file)
|
| 37 |
return transform, ort_session
|
| 38 |
|
| 39 |
-
#
|
| 40 |
def get_text(img_org):
|
| 41 |
-
x = transform(img_org.convert('RGB')).unsqueeze(0)
|
| 42 |
-
|
| 43 |
-
# Предсказание с помощью ONNX
|
| 44 |
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
|
| 45 |
logits = ort_session.run(None, ort_inputs)[0]
|
| 46 |
-
probs = torch.tensor(logits).softmax(-1)
|
| 47 |
-
preds,
|
| 48 |
-
|
| 49 |
-
return preds
|
| 50 |
|
| 51 |
-
#
|
| 52 |
transform, ort_session = initialize_model(model_file=model_file)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
examples=["8000.png", "11JW29.png", "2a8486.jpg", "2nbcx.png", # Примеры изображений
|
| 63 |
-
"000679.png", "000HU.png", "00Uga.png.jpg", "00bAQwhAZU.jpg",
|
| 64 |
-
"00h57kYf.jpg", "0EoHdtVb.png", "0JS21.png", "0p98z.png", "10010.png"]
|
| 65 |
-
)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
gradio_interface().launch()
|
|
|
|
| 4 |
from torchvision import transforms as T
|
| 5 |
from PIL import Image
|
| 6 |
from tokenizer_base import Tokenizer
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
|
|
|
|
| 9 |
model_file = "captcha.onnx"
|
| 10 |
+
img_size = (32, 128)
|
| 11 |
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
| 12 |
+
|
| 13 |
tokenizer_base = Tokenizer(charset)
|
| 14 |
|
|
|
|
| 15 |
def get_transform(img_size):
|
| 16 |
transforms = [
|
| 17 |
T.Resize(img_size, T.InterpolationMode.BICUBIC),
|
|
|
|
| 20 |
]
|
| 21 |
return T.Compose(transforms)
|
| 22 |
|
|
|
|
| 23 |
def to_numpy(tensor):
|
| 24 |
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
|
| 25 |
|
|
|
|
| 26 |
def initialize_model(model_file):
|
| 27 |
transform = get_transform(img_size)
|
| 28 |
onnx_model = onnx.load(model_file)
|
|
|
|
| 30 |
ort_session = rt.InferenceSession(model_file)
|
| 31 |
return transform, ort_session
|
| 32 |
|
| 33 |
+
# Core OCR function
|
| 34 |
def get_text(img_org):
|
| 35 |
+
x = transform(img_org.convert('RGB')).unsqueeze(0)
|
|
|
|
|
|
|
| 36 |
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
|
| 37 |
logits = ort_session.run(None, ort_inputs)[0]
|
| 38 |
+
probs = torch.tensor(logits).softmax(-1)
|
| 39 |
+
preds, _ = tokenizer_base.decode(probs)
|
| 40 |
+
return preds[0]
|
|
|
|
| 41 |
|
| 42 |
+
# Load model
|
| 43 |
transform, ort_session = initialize_model(model_file=model_file)
|
| 44 |
|
| 45 |
+
# ✅ Use gr.Interface (not Blocks) to support external API call
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=get_text,
|
| 48 |
+
inputs=gr.Image(type="pil"),
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="Text Captcha Solver",
|
| 51 |
+
description="API & UI nhận diện Captcha sử dụng mô hình ONNX"
|
| 52 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
iface.launch(enable_queue=True) # <-- BẮT BUỘC để gọi được từ WinForms
|
|
|