Spaces:
Sleeping
Sleeping
File size: 1,220 Bytes
4c45d43 f4d5676 91678c8 4c45d43 91678c8 4c45d43 91678c8 4c45d43 91678c8 4c45d43 91678c8 4c45d43 91678c8 4c45d43 |
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 |
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
import requests
from PIL import Image
import numpy as np
import cv2
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
model = VisionEncoderDecoderModel.from_pretrained("aico/TrOCR-MNIST")
def process_image(image):
#print(np.shape(image))
#print(image)
#rint(image.astype('uint8'))
#cv2.imwrite("image.png",image.astype('uint8'),(28, 28))
img = Image.fromarray(image.astype('uint8')).convert("RGB")
#img = Image.open("image.png").convert("RGB")
print(img)
# prepare image
pixel_values = processor(img, return_tensors="pt").pixel_values
# generate (no beam search)
generated_ids = model.generate(pixel_values)
# decode
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
return generated_text
title = "Interactive demo: Single Digits MNIST"
description = "Aico - University Utrecht"
iface = gr.Interface(fn=process_image,
inputs="sketchpad",
outputs="label",
title = title,
description = description)
iface.launch(debug=True) |