File size: 1,591 Bytes
b50c8d8
3f7f130
 
9b5b062
b50c8d8
3f7f130
9b5b062
 
 
 
 
b50c8d8
3f7f130
 
 
 
 
 
 
 
 
 
c1669c3
3f7f130
 
 
c1669c3
 
0b75f14
3f7f130
0b75f14
3f7f130
 
c1669c3
0d3b96f
 
8665656
46b5fd7
 
 
8665656
3f7f130
 
0d3b96f
3f7f130
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
import gradio as gr
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
import torch
from PIL import Image

def recognize_captcha(input, mdl):
    # Load image
    image = Image.open(input).convert("RGB")
    # Create white background
    background = Image.new("RGBA", image.size, (255, 255, 255))
    combined = Image.alpha_composite(background, image).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','anuashok/ocr-captcha-v3'],
        ['hyp2a.jpg','dragonstar/image-text-captcha-v2'],
        ['k4kyf.jpg','chanelcolgate/trocr-base-printed_captcha_ocr']
    ]
)

iface.queue(max_size=10)
iface.launch()