golyew commited on
Commit
faca012
·
verified ·
1 Parent(s): 31b5da1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
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
- # Преобразования изображения (замена torchvision)
16
  def preprocess_image(img, img_size):
17
- # resize с bicubic
 
 
 
18
  img = img.resize(img_size, Image.BICUBIC)
19
- # в numpy (HWC → CHW)
 
20
  img = np.array(img).astype(np.float32) / 255.0
21
- img = np.transpose(img, (2, 0, 1)) # (H,W,C) → (C,H,W)
22
- # нормализация (x-mean)/std, тут mean=0.5, std=0.5
 
 
 
23
  img = (img - 0.5) / 0.5
24
- # добавляем batch dim
 
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.convert('RGB'), img_size)
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
- # Создание Gradio интерфейса
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,