Spaces:
Sleeping
Sleeping
File size: 3,647 Bytes
fc19a11 a6b4b00 51f127a fc19a11 51f127a 8ba3977 51f127a 7a46d3b 51f127a 21d48ea 7a46d3b 21d48ea 22bc6f4 9900537 21d48ea 51f127a 1b673e4 bbeb2b4 72089c4 bbeb2b4 51f127a 84a5149 51f127a fc19a11 37a44ff 0ba19f5 7a46d3b 21d48ea 382c403 bbeb2b4 72089c4 e5e32df 382c403 7b59ad9 0fdbca4 |
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 |
import gradio as gr
import io
def solve_captcha(data_bytes: bytes = None, file_name: str = None):
import time
import os
import sys
import cv2
import numpy as np
import onnxruntime
real_tests = False
from config import characters, img_height, img_width, img_type, max_length, transpose_perm, OUTPUT_ONNX
sess = onnxruntime.InferenceSession(f"out.model.onnx")
name = sess.get_inputs()[0].name
def get_result(pred):
"""CTC decoder of the output tensor
https://distill.pub/2017/ctc/
https://en.wikipedia.org/wiki/Connectionist_temporal_classification
:return string, float
"""
accuracy = 1
last = None
ans = []
# pred - 3d tensor, we need 2d array - first element
for item in pred[0]:
# get index of element with max accuracy
char_ind = item.argmax()
# ignore duplicates and special characters
if char_ind != last and char_ind != 0 and char_ind != len(characters)+1:
# this element is a character - append it to answer
ans.append(characters[char_ind - 1])
# Get accuracy for current character and multiply global accuracy by it
accuracy *= item[char_ind]
last = char_ind
answ = "".join(ans)[:max_length]
return answ, accuracy
def decode_img(data_bytes: bytes):
# same actions, as for tensorflow
image = cv2.imdecode(np.asarray(bytearray(data_bytes), dtype=np.uint8), 1)
image: "np.ndarray" = image.astype(np.float32) / 255.
if image.shape != (img_height, img_width, 3):
image = cv2.resize(image, (img_width, img_height))
image = image.transpose(transpose_perm)
# Creating tensor ( adding 4d dimension )
image = np.array([image])
return image
def decode_img_array(nump_array):
# same actions, as for tensorflow
#image = cv2.imdecode(nump_array, 1)
image: "np.ndarray" = nump_array.astype(np.float32) / 255.
if image.shape != (img_height, img_width, 3):
image = cv2.resize(image, (img_width, img_height))
image = image.transpose(transpose_perm)
# Creating tensor ( adding 4d dimension )
image = np.array([image])
return image
def solve(data_bytes: bytes=None, file_name=None):
if file_name:
with open(file_name, 'rb') as F:
data_bytes = F.read()
if data_bytes is None:
print('[CAPTCHA RESOLVER NN] ПУСТОТА ВМЕСТО БАЙТОВ!')
return None
if isinstance(data_bytes,np.ndarray):
print('Img is ndarray')
img = decode_img_array(data_bytes)
else:
print('Img is bytes')
img_byte_arr = io.BytesIO()
data_bytes.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
img = decode_img(data_bytes)
#print(img)
pred_onx = sess.run(None, {name: img})[0]
ans = get_result(pred_onx)
return ans
result = solve(data_bytes,file_name)
print ('solved', result)
return {"result":result[0], "predict":result[1]}
def image_classifier(inp):
if inp is None:
return "Не загружена картинка"
print('INPUT GOT>>>', inp)
#gradio img inp - numpy ndarray
result = solve_captcha(inp)
return result
#inputs=gr.Image(type="pil")
demo = gr.Interface(fn=image_classifier, inputs=gr.Image(), outputs="textbox")
demo.launch(show_api = True) |