Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import MgpstrProcessor, MgpstrForSceneTextRecognition
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load processor and model
|
| 6 |
+
processor = MgpstrProcessor.from_pretrained('alibaba-damo/mgp-str-base')
|
| 7 |
+
model = MgpstrForSceneTextRecognition.from_pretrained('alibaba-damo/mgp-str-base')
|
| 8 |
+
|
| 9 |
+
# Load image from a URL
|
| 10 |
+
url = "https://i.postimg.cc/ZKwLg2Gw/367-14.png"
|
| 11 |
+
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
| 12 |
+
|
| 13 |
+
# Process the image
|
| 14 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 15 |
+
|
| 16 |
+
# Perform inference
|
| 17 |
+
outputs = model(pixel_values)
|
| 18 |
+
|
| 19 |
+
# Decode the output
|
| 20 |
+
generated_text = processor.batch_decode(outputs.logits, skip_special_tokens=True)
|
| 21 |
+
|
| 22 |
+
# Print the recognized text
|
| 23 |
+
print("Recognized Text:", generated_text[0])
|