操作前需完成2项核心准备,所有步骤严格执行,避免兼容性问题:
创建项目文件夹(路径自定义,比如D:\archive_ai),在文件夹内新建配置文件config.ini,完整内容如下:
```ini [AI_SERVICE] host = 127.0.0.1 port = 8080 model_path = ./paddleocr verify_threshold = 0.95 ```创建启动文件app.py,完整代码如下(实现AI识别服务):
```python from paddleocr import PaddleOCR from flask import Flask, request, jsonify import configparser app = Flask(__name__) config = configparser.ConfigParser() config.read('config.ini') 初始化OCR模型 ocr = PaddleOCR(use_angle_cls=True, lang='ch', rec=True, det=True) @app.route('/ai/recognize', methods=['POST']) def recognize_archive(): try: 获取上传的档案图片路径 img_path = request.json.get('img_path') if not img_path: return jsonify({'code': 400, 'msg': '缺少img_path参数'}) 执行识别 result = ocr.ocr(img_path, cls=True) 提取有效文本 texts = [line[1][0] for line in result[0]] if result[0] else [] return jsonify({'code': 200, 'data': texts, 'msg': '识别成功'}) except Exception as e: return jsonify({'code': 500, 'msg': str(e)}) if __name__ == '__main__': app.run(host=config['AI_SERVICE']['host'], port=int(config['AI_SERVICE']['port'])) ```
打开cmd,进入项目文件夹,执行启动命令:
``` cd D:\archive_ai python app.py ```若出现「 Running on http://127.0.0.1:8080/」,说明服务启动成功,勿关闭此cmd窗口。
假设档案管理系统提供「/archive/ai_auth」接口用于接收AI校验结果,对接代码(写入档案管理系统的核心服务)如下:
```python import requests import configparser config = configparser.ConfigParser() config.read('config.ini') def ai_verify_archive(img_path, expected_fields): 调用AI识别服务 ai_response = requests.post( f"http://{config['AI_SERVICE']['host']}:{config['AI_SERVICE']['port']}/ai/recognize", json={'img_path': img_path}, timeout=10 ) if ai_response.status_code != 200: return False, 'AI服务调用失败' ai_texts = ai_response.json()['data'] 校验必填字段(如档案号、日期) for field in expected_fields: if not any(field in text for text in ai_texts): return False, f'缺失必填字段:{field}' return True, 'AI认证通过' 调用示例 img_path = r'C:\test_archive\20240501_001.jpg' expected_fields = ['档案号:20240501001', '日期:2024-05-01'] is_pass, msg = ai_verify_archive(img_path, expected_fields) print(f'认证结果:{msg}') ```重点操作:必须将expected_fields替换为实际档案的必填字段,否则会出现校验失败。