Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
| 8 |
+
model = VisionEncoderDecoderModel.from_pretrained("aico/TrOCR-MNIST")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def process_image(image):
|
| 12 |
+
|
| 13 |
+
#print(np.shape(image))
|
| 14 |
+
#print(image)
|
| 15 |
+
#rint(image.astype('uint8'))
|
| 16 |
+
#cv2.imwrite("image.png",image.astype('uint8'),(28, 28))
|
| 17 |
+
img = Image.fromarray(image.astype('uint8')).convert("RGB")
|
| 18 |
+
#img = Image.open("image.png").convert("RGB")
|
| 19 |
+
print(img)
|
| 20 |
+
# prepare image
|
| 21 |
+
pixel_values = processor(img, return_tensors="pt").pixel_values
|
| 22 |
+
|
| 23 |
+
# generate (no beam search)
|
| 24 |
+
generated_ids = model.generate(pixel_values)
|
| 25 |
+
|
| 26 |
+
# decode
|
| 27 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 28 |
+
|
| 29 |
+
return generated_text
|
| 30 |
+
|
| 31 |
+
title = "Interactive demo: Single Digits MNIST"
|
| 32 |
+
description = "Aico - University Utrecht"
|
| 33 |
+
iface = gr.Interface(fn=process_image,
|
| 34 |
+
inputs="sketchpad",
|
| 35 |
+
outputs="label",
|
| 36 |
+
title = title,
|
| 37 |
+
description = description)
|
| 38 |
+
iface.launch(debug=True)
|