Nischay103 commited on
Commit
3d59498
1 Parent(s): 6b7e295

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os,torch
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from torchvision import transforms as T
5
+ from huggingface_hub import hf_hub_download
6
+ from tokenizer import Tokenizer
7
+
8
+ TOKEN = os.getenv('hf_read_token')
9
+ repo_id = "Nischay103/captcha_recognition"
10
+ model_file = 'captcha_model.pt'
11
+ model_path = hf_hub_download(repo_id=repo_id, filename=model_file, token=TOKEN)
12
+ model = torch.jit.load(model_path)
13
+
14
+ charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
15
+ _tokenizer = Tokenizer(charset)
16
+
17
+ def get_transform(img_size=(32,128)):
18
+ transforms = []
19
+ transforms.extend([
20
+ T.Resize(img_size, T.InterpolationMode.BICUBIC),
21
+ T.ToTensor(),
22
+ T.Normalize(0.5, 0.5)
23
+ ])
24
+ return T.Compose(transforms)
25
+
26
+ transform = get_transform()
27
+ image = gr.Image(type="filepath", label="Captcha Image")
28
+ text = gr.Textbox(label="Recognized Character Sequence")
29
+
30
+
31
+ def recognize_captcha(image_path):
32
+ input = Image.open(image_path)
33
+ input = transform(input.convert('RGB')).unsqueeze(0)
34
+ model = model
35
+ with torch.no_grad():
36
+ probs = model(input)
37
+ preds,_ = _tokenizer.decode(probs,beam_width=2)
38
+ return preds[0]
39
+
40
+ iface = gr.Interface(
41
+ fn=recognize_captcha,
42
+ inputs=image,
43
+ outputs=text,
44
+ title="Character Sequence Recognition from Scene-Image (Captcha)",
45
+ description="Recognize captchas using different models",
46
+ examples=['examples/251615.png']
47
+ )
48
+
49
+ iface.launch()