File size: 3,002 Bytes
582f2a6
 
 
edc435d
582f2a6
edc435d
ee21b96
582f2a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee21b96
 
582f2a6
 
c2b99e9
582f2a6
c2b99e9
0509ee0
271a2e6
0c80503
ab591a3
ee21b96
 
edf5ee3
6ebfea3
 
 
 
 
 
ee21b96
 
6085bc1
b926706
f8816f2
edc435d
edeec3c
582f2a6
 
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
import base64
import json
from io import BytesIO
import pandas as pd
from PIL import Image

import gradio as gr
import requests


def ocr(image):

    image = Image.open(image)
    img_buffer = BytesIO()
    image.save(img_buffer, format=image.format)
    byte_data = img_buffer.getvalue()
    base64_bytes = base64.b64encode(byte_data)  # bytes
    base64_str = base64_bytes.decode()
    url = "https://www.modelscope.cn/api/v1/studio/damo/ofa_ocr_pipeline/gradio/api/predict/"
    payload = json.dumps({
        "data": [f"data:image/jpeg;base64,{base64_str}"],
        "dataType": ["image"]
    })
    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    jobj = json.loads(response.text)
    out_img_base64 = jobj['data'][0].replace('data:image/png;base64,','')
    out_img = Image.open(BytesIO(base64.urlsafe_b64decode(out_img_base64)))
    ocr_result = jobj['data'][1]['data']

    result = pd.DataFrame(ocr_result, columns=['Box ID', 'Text'])

    return out_img, result


title = "Chinese OCR"
description = """<p>Gradio Demo for OFA-OCR for Chinese text recognition. <br><br>
              Upload your own image or click any one of the examples, and click "Submit" and then wait for the generated OCR result. <br>
              中文OCR体验区。欢迎上传图片,静待检测文字返回~<br><br>
              Paper: <a href='https://arxiv.org/abs/2212.09297'>https://arxiv.org/abs/2212.09297</a> <br>
              Github: <a href='https://github.com/OFA-Sys/Chinese-CLIP'>https://github.com/OFA-Sys/OFA</a> <br><br>
              You can duplicate this space and run it privately: <a href='https://huggingface.co/spaces/OFA-Sys/chinese-clip-zero-shot-image-classification?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>"""
article = "<p style='text-align: center'><a href='https://github.com/OFA-Sys/OFA' target='_blank'>OFA Github " \
          "Repo</a></p> "
examples = [['shupai.png'], ['chinese.jpg'], ['gaidao.jpeg'],['qiaodaima.png']]
io = gr.Interface(fn=ocr, inputs=gr.inputs.Image(type='filepath', label='Image'),
                  examples=examples,
                  outputs=[gr.outputs.Image(type='pil', label='Image'),
                           gr.outputs.Dataframe(headers=['Box ID', 'Text'], type='pandas', label='OCR Results')],
                  title=title, description=description, article=article)
io.launch()