HCI / app.py
Faiza213's picture
Create app.py
662e0d0 verified
raw
history blame
760 Bytes
from transformers import MgpstrProcessor, MgpstrForSceneTextRecognition
import requests
from PIL import Image
# Load processor and model
processor = MgpstrProcessor.from_pretrained('alibaba-damo/mgp-str-base')
model = MgpstrForSceneTextRecognition.from_pretrained('alibaba-damo/mgp-str-base')
# Load image from a URL
url = "https://i.postimg.cc/ZKwLg2Gw/367-14.png"
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
# Process the image
pixel_values = processor(images=image, return_tensors="pt").pixel_values
# Perform inference
outputs = model(pixel_values)
# Decode the output
generated_text = processor.batch_decode(outputs.logits, skip_special_tokens=True)
# Print the recognized text
print("Recognized Text:", generated_text[0])