File size: 760 Bytes
662e0d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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])
|