File size: 2,605 Bytes
66403e6 bf0a8e3 66403e6 bf0a8e3 66403e6 bf0a8e3 66403e6 bf0a8e3 66403e6 bf0a8e3 66403e6 ea978b8 66403e6 bf0a8e3 66403e6 bf0a8e3 66403e6 ee1547b bf0a8e3 66403e6 ee1547b 66403e6 bf0a8e3 66403e6 bf0a8e3 13ea0c7 ea978b8 bf0a8e3 ee1547b bf0a8e3 66403e6 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import base64
import io
import os
import gradio as gr
import requests
from PIL import Image
API_URL = "https://t7nd0cf3u89ck4bf.aistudio-hub.baidu.com/ocr"
TOKEN = os.getenv("API_TOKEN", "")
def inference(img):
with io.BytesIO() as buffer:
img.save(buffer, format="png")
img_base64 = base64.b64encode(buffer.getvalue()).decode("ascii")
headers = {
"Authorization": f"token {TOKEN}",
"Content-Type": "application/json",
}
response = requests.post(
API_URL,
json={
"file": img_base64,
"fileType": 1,
"useDocOrientationClassify": False,
"useDocUnwarping": False,
},
headers=headers,
timeout=1000,
)
response.raise_for_status()
result = response.json()
ocr_img_url = result["result"]["ocrResults"][0]["ocrImage"]
response = requests.get(ocr_img_url, timeout=10)
response.raise_for_status()
ocr_img_base64 = Image.open(io.BytesIO(response.content))
return ocr_img_base64, result["result"]["ocrResults"][0]["prunedResult"]
title = "PP-OCRv5"
description = """
- Gradio demo for PP-OCRv5. PP-OCRv5 is the new generation text recognition solution of PP-OCR, focusing on multi-scenario and multi-text type recognition. In terms of text types, PP-OCRv5 supports 5 major mainstream text types: Simplified Chinese, Chinese Pinyin, Traditional Chinese, English, and Japanese. For scenarios, PP-OCRv5 has upgraded recognition capabilities for challenging scenarios such as complex Chinese and English handwriting, vertical text, and uncommon characters.
- To use it, simply upload your image, or click one of the examples to load them. Read more at the links below.
- [Docs](https://paddlepaddle.github.io/PaddleOCR/), [Github Repository](https://github.com/PaddlePaddle/PaddleOCR).
"""
examples = [
["examples/ancient_demo.png"],
["examples/handwrite_ch_demo.png"],
["examples/handwrite_en_demo.png"],
["examples/japan_demo.png"],
["examples/magazine.png"],
["examples/pinyin_demo.png"],
["examples/research.png"],
["examples/tech.png"],
]
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
gr.Interface(
inference,
gr.Image(type="pil", label="Input Image"),
[gr.Image(type="pil", label="Output Image"), gr.JSON(label="Output JSON", show_label=True)],
title=title,
description=description,
examples=examples,
cache_examples=False,
css=css,
).launch(debug=False)
|