Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,77 @@
|
|
1 |
-
import base64
|
2 |
-
import io
|
3 |
-
import os
|
4 |
-
|
5 |
-
import gradio as gr
|
6 |
-
import requests
|
7 |
-
from PIL import Image
|
8 |
-
|
9 |
-
API_URL = "https://t7nd0cf3u89ck4bf.aistudio-hub.baidu.com/ocr"
|
10 |
-
TOKEN = os.getenv("API_TOKEN", "")
|
11 |
-
|
12 |
-
|
13 |
-
def inference(img):
|
14 |
-
with io.BytesIO() as buffer:
|
15 |
-
img.save(buffer, format="png")
|
16 |
-
img_base64 = base64.b64encode(buffer.getvalue()).decode("ascii")
|
17 |
-
|
18 |
-
headers = {
|
19 |
-
"Authorization": f"token {TOKEN}",
|
20 |
-
"Content-Type": "application/json",
|
21 |
-
}
|
22 |
-
|
23 |
-
response = requests.post(
|
24 |
-
API_URL,
|
25 |
-
json={
|
26 |
-
"file": img_base64,
|
27 |
-
"fileType": 1,
|
28 |
-
"useDocOrientationClassify": False,
|
29 |
-
"useDocUnwarping": False,
|
30 |
-
"useTextlineOrientation": False,
|
31 |
-
},
|
32 |
-
headers=headers,
|
33 |
-
timeout=1000,
|
34 |
-
)
|
35 |
-
response.raise_for_status()
|
36 |
-
|
37 |
-
result = response.json()
|
38 |
-
ocr_img_url = result["result"]["ocrResults"][0]["ocrImage"]
|
39 |
-
|
40 |
-
response = requests.get(ocr_img_url, timeout=10)
|
41 |
-
response.raise_for_status()
|
42 |
-
ocr_img_base64 = Image.open(io.BytesIO(response.content))
|
43 |
-
|
44 |
-
return ocr_img_base64, result["result"]["ocrResults"][0]["prunedResult"]
|
45 |
-
|
46 |
-
|
47 |
-
title = "PP-OCRv5"
|
48 |
-
description = """
|
49 |
-
-
|
50 |
-
-
|
51 |
-
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
["examples/
|
58 |
-
["examples/
|
59 |
-
["examples/
|
60 |
-
["examples/
|
61 |
-
["examples/
|
62 |
-
["examples/
|
63 |
-
]
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import io
|
3 |
+
import os
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import requests
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
API_URL = "https://t7nd0cf3u89ck4bf.aistudio-hub.baidu.com/ocr"
|
10 |
+
TOKEN = os.getenv("API_TOKEN", "")
|
11 |
+
|
12 |
+
|
13 |
+
def inference(img):
|
14 |
+
with io.BytesIO() as buffer:
|
15 |
+
img.save(buffer, format="png")
|
16 |
+
img_base64 = base64.b64encode(buffer.getvalue()).decode("ascii")
|
17 |
+
|
18 |
+
headers = {
|
19 |
+
"Authorization": f"token {TOKEN}",
|
20 |
+
"Content-Type": "application/json",
|
21 |
+
}
|
22 |
+
|
23 |
+
response = requests.post(
|
24 |
+
API_URL,
|
25 |
+
json={
|
26 |
+
"file": img_base64,
|
27 |
+
"fileType": 1,
|
28 |
+
"useDocOrientationClassify": False,
|
29 |
+
"useDocUnwarping": False,
|
30 |
+
"useTextlineOrientation": False,
|
31 |
+
},
|
32 |
+
headers=headers,
|
33 |
+
timeout=1000,
|
34 |
+
)
|
35 |
+
response.raise_for_status()
|
36 |
+
|
37 |
+
result = response.json()
|
38 |
+
ocr_img_url = result["result"]["ocrResults"][0]["ocrImage"]
|
39 |
+
|
40 |
+
response = requests.get(ocr_img_url, timeout=10)
|
41 |
+
response.raise_for_status()
|
42 |
+
ocr_img_base64 = Image.open(io.BytesIO(response.content))
|
43 |
+
|
44 |
+
return ocr_img_base64, result["result"]["ocrResults"][0]["prunedResult"]
|
45 |
+
|
46 |
+
|
47 |
+
title = "PP-OCRv5 online demo"
|
48 |
+
description = """
|
49 |
+
- PP-OCRv5 is the latest generation of the PP-OCR series model, designed to handle a wide range of scene and text types.
|
50 |
+
- It supports five major text types: Simplified Chinese, Traditional Chinese, Chinese Pinyin, English, and Japanese.
|
51 |
+
- PP-OCRv5 has enhanced recognition capabilities for challenging use cases, including complex handwritten Chinese and English, vertical text, and rare characters.
|
52 |
+
- To use it, simply upload your image, or click one of the examples to load them. Read more at the links below.
|
53 |
+
- [Docs](https://paddlepaddle.github.io/PaddleOCR/), [Github Repository](https://github.com/PaddlePaddle/PaddleOCR).
|
54 |
+
"""
|
55 |
+
|
56 |
+
examples = [
|
57 |
+
["examples/ancient_demo.png"],
|
58 |
+
["examples/handwrite_ch_demo.png"],
|
59 |
+
["examples/handwrite_en_demo.png"],
|
60 |
+
["examples/japan_demo.png"],
|
61 |
+
["examples/magazine.png"],
|
62 |
+
["examples/pinyin_demo.png"],
|
63 |
+
["examples/research.png"],
|
64 |
+
["examples/tech.png"],
|
65 |
+
]
|
66 |
+
|
67 |
+
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
|
68 |
+
gr.Interface(
|
69 |
+
inference,
|
70 |
+
gr.Image(type="pil", label="Input Image"),
|
71 |
+
[gr.Image(type="pil", label="Output Image"), gr.JSON(label="Output JSON", show_label=True)],
|
72 |
+
title=title,
|
73 |
+
description=description,
|
74 |
+
examples=examples,
|
75 |
+
cache_examples=False,
|
76 |
+
css=css,
|
77 |
+
).launch(debug=False)
|