Spaces:
Runtime error
Runtime error
Commit ·
5ba7af1
1
Parent(s): 3b2ce62
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
# from strhub.data.module import SceneTextDataModule
|
| 4 |
+
from torchvision import transforms as T
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load model and image transforms
|
| 8 |
+
parseq = torch.hub.load('baudm/parseq', 'parseq', pretrained=True).eval()
|
| 9 |
+
# img_transform = SceneTextDataModule.get_transform(parseq.hparams.img_size)
|
| 10 |
+
|
| 11 |
+
transform = T.Compose([
|
| 12 |
+
T.Resize(parseq.hparams.img_size, T.InterpolationMode.BICUBIC),
|
| 13 |
+
T.ToTensor(),
|
| 14 |
+
T.Normalize(0.5, 0.5)
|
| 15 |
+
])
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def infer(inps):
|
| 19 |
+
|
| 20 |
+
img = inps.convert('RGB')
|
| 21 |
+
# Preprocess. Model expects a batch of images with shape: (B, C, H, W)
|
| 22 |
+
img = transform(img).unsqueeze(0)
|
| 23 |
+
|
| 24 |
+
logits = parseq(img)
|
| 25 |
+
# logits.shape # torch.Size([1, 26, 95]), 94 characters + [EOS] symbol
|
| 26 |
+
|
| 27 |
+
# Greedy decoding
|
| 28 |
+
pred = logits.softmax(-1)
|
| 29 |
+
label, confidence = parseq.tokenizer.decode(pred)
|
| 30 |
+
# print('Decoded label = {}'.format(label[0]))
|
| 31 |
+
return label[0]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
demo = gr.Interface(fn=infer,
|
| 35 |
+
inputs=[gr.inputs.Image(type="pil")],
|
| 36 |
+
outputs=[gr.outputs.Textbox(label="Output Text")]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
demo.launch(share=True)
|