Spaces:
Runtime error
Runtime error
import os,torch | |
import gradio as gr | |
from PIL import Image | |
from torchvision import transforms as T | |
from huggingface_hub import hf_hub_download | |
from tokenizer import Tokenizer | |
TOKEN = os.getenv('hf_read_token') | |
repo_id = "Nischay103/captcha_recognition" | |
model_file = 'captcha_model.pt' | |
model_path = hf_hub_download(repo_id=repo_id, filename=model_file, token=TOKEN) | |
model = torch.jit.load(model_path) | |
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
_tokenizer = Tokenizer(charset) | |
def get_transform(img_size=(32,128)): | |
transforms = [] | |
transforms.extend([ | |
T.Resize(img_size, T.InterpolationMode.BICUBIC), | |
T.ToTensor(), | |
T.Normalize(0.5, 0.5) | |
]) | |
return T.Compose(transforms) | |
transform = get_transform() | |
image = gr.Image(type="filepath", label="Captcha Image") | |
text = gr.Textbox(label="Recognized Character Sequence") | |
def recognize_captcha(image_path): | |
input = Image.open(image_path) | |
input = transform(input.convert('RGB')).unsqueeze(0) | |
model = model | |
with torch.no_grad(): | |
probs = model(input) | |
preds,_ = _tokenizer.decode(probs,beam_width=2) | |
return preds[0] | |
iface = gr.Interface( | |
fn=recognize_captcha, | |
inputs=image, | |
outputs=text, | |
title="Character Sequence Recognition from Scene-Image (Captcha)", | |
description="Recognize captchas using different models", | |
examples=['examples/251615.png'] | |
) | |
iface.launch() |