Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,33 @@ from PIL import Image
|
|
| 4 |
import numpy as np
|
| 5 |
from tokenizer_base import Tokenizer
|
| 6 |
import gradio as gr
|
| 7 |
-
from huggingface_hub import Repository
|
| 8 |
|
| 9 |
# Параметры модели
|
| 10 |
model_file = "captcha.onnx"
|
| 11 |
-
img_size = (32,128)
|
| 12 |
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
| 13 |
tokenizer_base = Tokenizer(charset)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def preprocess_image(img, img_size):
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
| 18 |
img = img.resize(img_size, Image.BICUBIC)
|
| 19 |
-
|
|
|
|
| 20 |
img = np.array(img).astype(np.float32) / 255.0
|
| 21 |
-
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
| 23 |
img = (img - 0.5) / 0.5
|
| 24 |
-
|
|
|
|
| 25 |
img = np.expand_dims(img, axis=0)
|
|
|
|
| 26 |
return img
|
| 27 |
|
| 28 |
# softmax на numpy
|
|
@@ -37,22 +45,22 @@ def initialize_model(model_file):
|
|
| 37 |
ort_session = rt.InferenceSession(model_file)
|
| 38 |
return ort_session
|
| 39 |
|
| 40 |
-
# Функция для распознавания текста
|
| 41 |
def get_text(img_org):
|
| 42 |
-
x = preprocess_image(img_org
|
| 43 |
|
| 44 |
# Предсказание с помощью ONNX
|
| 45 |
ort_inputs = {ort_session.get_inputs()[0].name: x}
|
| 46 |
logits = ort_session.run(None, ort_inputs)[0]
|
| 47 |
|
| 48 |
-
probs = softmax(logits, axis=-1)
|
| 49 |
-
preds, probs = tokenizer_base.decode(probs)
|
| 50 |
-
return preds[0]
|
| 51 |
|
| 52 |
# Инициализация модели
|
| 53 |
ort_session = initialize_model(model_file=model_file)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
def gradio_interface():
|
| 57 |
return gr.Interface(
|
| 58 |
fn=get_text,
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from tokenizer_base import Tokenizer
|
| 6 |
import gradio as gr
|
|
|
|
| 7 |
|
| 8 |
# Параметры модели
|
| 9 |
model_file = "captcha.onnx"
|
| 10 |
+
img_size = (32, 128)
|
| 11 |
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
| 12 |
tokenizer_base = Tokenizer(charset)
|
| 13 |
|
| 14 |
+
# Надёжный препроцессинг
|
| 15 |
def preprocess_image(img, img_size):
|
| 16 |
+
# Приводим к RGB (убираем альфу/градации серого)
|
| 17 |
+
img = img.convert("RGB")
|
| 18 |
+
|
| 19 |
+
# Resize bicubic
|
| 20 |
img = img.resize(img_size, Image.BICUBIC)
|
| 21 |
+
|
| 22 |
+
# В numpy (HWC → float32)
|
| 23 |
img = np.array(img).astype(np.float32) / 255.0
|
| 24 |
+
|
| 25 |
+
# HWC → CHW
|
| 26 |
+
img = np.transpose(img, (2, 0, 1))
|
| 27 |
+
|
| 28 |
+
# Нормализация (x-mean)/std
|
| 29 |
img = (img - 0.5) / 0.5
|
| 30 |
+
|
| 31 |
+
# Добавляем batch размерность
|
| 32 |
img = np.expand_dims(img, axis=0)
|
| 33 |
+
|
| 34 |
return img
|
| 35 |
|
| 36 |
# softmax на numpy
|
|
|
|
| 45 |
ort_session = rt.InferenceSession(model_file)
|
| 46 |
return ort_session
|
| 47 |
|
| 48 |
+
# Функция для распознавания текста
|
| 49 |
def get_text(img_org):
|
| 50 |
+
x = preprocess_image(img_org, img_size)
|
| 51 |
|
| 52 |
# Предсказание с помощью ONNX
|
| 53 |
ort_inputs = {ort_session.get_inputs()[0].name: x}
|
| 54 |
logits = ort_session.run(None, ort_inputs)[0]
|
| 55 |
|
| 56 |
+
probs = softmax(logits, axis=-1)
|
| 57 |
+
preds, probs = tokenizer_base.decode(probs)
|
| 58 |
+
return preds[0]
|
| 59 |
|
| 60 |
# Инициализация модели
|
| 61 |
ort_session = initialize_model(model_file=model_file)
|
| 62 |
|
| 63 |
+
# Gradio интерфейс
|
| 64 |
def gradio_interface():
|
| 65 |
return gr.Interface(
|
| 66 |
fn=get_text,
|