AlexTransformer commited on
Commit
fd84736
·
verified ·
1 Parent(s): ad2f67a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -75
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
- - 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.
50
- - To use it, simply upload your image, or click one of the examples to load them. Read more at the links below.
51
- - [Docs](https://paddlepaddle.github.io/PaddleOCR/), [Github Repository](https://github.com/PaddlePaddle/PaddleOCR).
52
- """
53
-
54
- examples = [
55
- ["examples/ancient_demo.png"],
56
- ["examples/handwrite_ch_demo.png"],
57
- ["examples/handwrite_en_demo.png"],
58
- ["examples/japan_demo.png"],
59
- ["examples/magazine.png"],
60
- ["examples/pinyin_demo.png"],
61
- ["examples/research.png"],
62
- ["examples/tech.png"],
63
- ]
64
-
65
- css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
66
- gr.Interface(
67
- inference,
68
- gr.Image(type="pil", label="Input Image"),
69
- [gr.Image(type="pil", label="Output Image"), gr.JSON(label="Output JSON", show_label=True)],
70
- title=title,
71
- description=description,
72
- examples=examples,
73
- cache_examples=False,
74
- css=css,
75
- ).launch(debug=False)
 
 
 
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)