File size: 756 Bytes
7d12aff
605e36e
ec4eb15
605e36e
7d12aff
9602198
56bb2b0
605e36e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9602198
57e2852
605e36e
 
 
7d12aff
bbf5d0a
605e36e
 
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
import gradio as gr
import pytesseract
from PIL import Image


def ocr_image(image):
    if image is None:
        return "请先上传一张图片。"

    try:
        text = pytesseract.image_to_string(image, lang="chi_sim+eng")
        text = text.strip()

        if not text:
            return "未识别到文字,请换一张更清晰的图片。"

        return text

    except Exception as e:
        return f"OCR 识别失败:{str(e)}"


demo = gr.Interface(
    fn=ocr_image,
    inputs=gr.Image(type="pil", label="上传图片"),
    outputs=gr.Textbox(label="识别结果", lines=10),
    title="中文 OCR 识别器",
    description="上传图片,自动识别中文和英文文字。"
)

if __name__ == "__main__":
    demo.launch()