Spaces:
Sleeping
Sleeping
| """ | |
| Gradio 网页版界面 | |
| 提供浏览器端可视化的 OCR 自动录单演示 | |
| """ | |
| import gradio as gr | |
| import os | |
| from ocr_engine import OCREngine | |
| from data_extractor import DataExtractor | |
| from excel_writer import ExcelWriter | |
| # 初始化核心引擎 | |
| ocr_engine = OCREngine(lang='ch') | |
| data_extractor = DataExtractor() | |
| excel_writer = ExcelWriter(output_dir='output') | |
| def process_single_image(image_path): | |
| """处理单张图片并返回提取结果""" | |
| if not image_path: | |
| return "请先上传一张图片", None | |
| try: | |
| # 1. OCR 识别 | |
| ocr_result = ocr_engine.recognize(image_path) | |
| # 2. 提取数据 | |
| data = data_extractor.extract(ocr_result) | |
| # 3. 写入单条 Excel | |
| data['image_path'] = os.path.basename(image_path) | |
| data['status'] = '成功' | |
| excel_path = excel_writer.create_report([data], filename="temp_single_result.xlsx") | |
| # 格式化展示文本 | |
| result_text = "### 📥 提取结果:" + chr(10) + \ | |
| f"- **订单编号**: {data.get('order_no', '未提取到')}" + chr(10) + \ | |
| f"- **客户名称**: {data.get('customer', '未提取到')}" + chr(10) + \ | |
| f"- **产品名称**: {data.get('product', '未提取到')}" + chr(10) + \ | |
| f"- **数量**: {data.get('quantity', '未提取到')}" + chr(10) + \ | |
| f"- **金额**: {data.get('amount', '未提取到')}" + chr(10) + \ | |
| f"- **提取时间**: {data.get('extract_time', '')}" | |
| return result_text, excel_path | |
| except Exception as e: | |
| return f"❌ 处理失败: {str(e)}", None | |
| def process_batch_images(image_files): | |
| """批量处理多张图片并生成汇总 Excel""" | |
| if not image_files: | |
| return "请先上传图片文件", None | |
| results = [] | |
| summary_text = "### 📊 批量处理进度:" + chr(10) | |
| for i, file_obj in enumerate(image_files): | |
| # Gradio 批量上传的文件可能是 File 对象,通过 .name 获取路径 | |
| path = file_obj.name if hasattr(file_obj, 'name') else file_obj | |
| filename = os.path.basename(path) | |
| try: | |
| ocr_result = ocr_engine.recognize(path) | |
| data = data_extractor.extract(ocr_result) | |
| data['image_path'] = filename | |
| data['status'] = '成功' | |
| results.append(data) | |
| summary_text += f"- ✅ {filename} 提取成功" + chr(10) | |
| except Exception as e: | |
| results.append({ | |
| 'image_path': filename, | |
| 'status': f'失败: {str(e)}' | |
| }) | |
| summary_text += f"- ❌ {filename} 处理失败: {str(e)}" + chr(10) | |
| # 生成 Excel 汇总报告 | |
| output_path = excel_writer.create_report(results) | |
| summary_text += chr(10) + "**🎉 批量处理完成!已生成汇总 Excel 报告。**" | |
| return summary_text, output_path | |
| # 构建 Gradio 界面 | |
| with gr.Blocks(title="OCR 自动录单系统") as demo: | |
| gr.Markdown(""" | |
| # 📄 OCR 自动录单系统 (网页版) | |
| 这是一个基于 **PaddleOCR** + **Python** 的轻量级高效率自动录单工具。适合制造业、电气、设备厂等办公室自动录入采购单、送货单和发票。 | |
| """) | |
| with gr.Tab("单张图片调试"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_img = gr.Image(type="filepath", label="上传订单/采购单图片") | |
| btn_run = gr.Button("🚀 开始智能提取", variant="primary") | |
| with gr.Column(): | |
| out_text = gr.Markdown("提取结果会显示在这里...") | |
| out_file = gr.File(label="下载当前生成的 Excel 录单") | |
| btn_run.click( | |
| fn=process_single_image, | |
| inputs=input_img, | |
| outputs=[out_text, out_file] | |
| ) | |
| with gr.Tab("批量录单模式"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_files = gr.File(file_count="multiple", label="上传多张订单图片", file_types=["image"]) | |
| btn_batch_run = gr.Button("⚡ 开始批量全自动录入", variant="primary") | |
| with gr.Column(): | |
| out_batch_text = gr.Markdown("批量处理进度会显示在这里...") | |
| out_batch_file = gr.File(label="下载汇总生成的 Excel 报表") | |
| btn_batch_run.click( | |
| fn=process_batch_images, | |
| inputs=input_files, | |
| outputs=[out_batch_text, out_batch_file] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |