Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +42 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
def ocr(image):
|
| 10 |
+
|
| 11 |
+
image = Image.open(image)
|
| 12 |
+
img_buffer = BytesIO()
|
| 13 |
+
image.save(img_buffer, format=image.format)
|
| 14 |
+
byte_data = img_buffer.getvalue()
|
| 15 |
+
base64_bytes = base64.b64encode(byte_data) # bytes
|
| 16 |
+
base64_str = base64_bytes.decode()
|
| 17 |
+
url = "https://www.modelscope.cn/api/v1/studio/damo/ofa_ocr_pipeline/gradio/api/predict/"
|
| 18 |
+
payload = json.dumps({
|
| 19 |
+
"data": [f"data:image/jpeg;base64,{base64_str}"],
|
| 20 |
+
"dataType": ["image"]
|
| 21 |
+
})
|
| 22 |
+
headers = {
|
| 23 |
+
'Content-Type': 'application/json'
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
| 27 |
+
jobj = json.loads(response.text)
|
| 28 |
+
out_img_base64 = jobj['data'][0].replace('data:image/png;base64,','')
|
| 29 |
+
out_img = Image.open(BytesIO(base64.urlsafe_b64decode(out_img_base64)))
|
| 30 |
+
ocr_result = jobj['data'][1]['data']
|
| 31 |
+
|
| 32 |
+
result = pd.DataFrame(ocr_result, columns=['Box ID', 'Text'])
|
| 33 |
+
|
| 34 |
+
return out_img, result
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
title = "图片识别文字"
|
| 38 |
+
io = gr.Interface(fn=ocr, inputs=gr.inputs.Image(type='filepath', label='Image'),
|
| 39 |
+
outputs=[gr.outputs.Image(type='pil', label='Image'),
|
| 40 |
+
gr.outputs.Dataframe(headers=['Box ID', 'Text'], type='pandas', label='OCR Results')],
|
| 41 |
+
title=title)
|
| 42 |
+
io.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
pillow
|
| 3 |
+
pandas
|
| 4 |
+
requests
|