captchaSolve / app.py
muddokon's picture
Update app.py
0d3b96f verified
raw
history blame
1.23 kB
import gradio as gr
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
import torch
def recognize_captcha(input, mdl):
input = input.convert('RGB')
# Load model and processor
processor = TrOCRProcessor.from_pretrained(mdl)
model = VisionEncoderDecoderModel.from_pretrained(mdl)
# Prepare image
pixel_values = processor(input, return_tensors="pt").pixel_values
# Generate text
generated_ids = model.generate(pixel_values)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
return generated_text
iface = gr.Interface(
fn=recognize_captcha,
inputs=[
gr.Image(),
gr.Dropdown(
['anuashok/ocr-captcha-v3','anuashok/ocr-captcha-v2','anuashok/ocr-captcha-v1','dragonstar/image-text-captcha-v2','chanelcolgate/trocr-base-printed_captcha_ocr'], label='Model to use'
)
],
outputs=['text'],
title = "Character Sequence Recognition From Captcha Image",
description = "Using some TrOCR models found on the HF Hub to test/break tough text captchas. Will you have to train your own?",
examples = ['krcx5.jpg','hyp2a.jpg','k4kyf.jpg']
)
iface.queue(max_size=10)
iface.launch()