Python实现基于开源cnocr和gradio的OCR图像中文字识别工具

1.直接上代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 13 11:19:16 2024
@author: xioabai
"""
# CnOcr是开源的ocr库,支持中英文识别,准确率高
from cnocr import CnOcr
import gradio as gr

# 初始化CnOcr
ocr = CnOcr()

def perform_ocr(img_path):
    if img_path is None:
        return "请上传图片"
    # OCR识别
    result = ocr.ocr(img_path)
    # 提取text字段并组成新的段落
    paragraph = ''.join(item['text'] for item in result)
    return paragraph

def save_text(text, output_file='output.txt'):
    if not text.strip():
        return "没有内容可以保存"
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(text)  
    return output_file

# Gradio Interface
with gr.Blocks() as iface:
    gr.Markdown("## OCR 文字识别工具")
    gr.Markdown("上传图片进行OCR文字识别,支持手动编辑,结果保存为txt文件。")

    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="filepath", label="上传图片")
        with gr.Column():
            edit_output = gr.Textbox(label="识别结果(可编辑)", lines=10)

    with gr.Row():
        recognize_button = gr.Button("开始识别")
        save_button = gr.Button("保存结果(txt)")
        file_output = gr.File(label="下载txt文件", visible=False)

    # 设置按钮点击事件
    recognize_button.click(fn=perform_ocr, inputs=image_input, outputs=edit_output)
    save_button.click(fn=save_text, inputs=edit_output, outputs=file_output)

# 启动Gradio应用
if __name__ == "__main__":
    iface.launch()

2.运行代码后,在chrome浏览器打开地址
测试结果
在这里插入图片描述

3.可手动编辑识别结果,并保存为txt文件
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38773993/article/details/144451438
今日推荐