[python]基于python调用WechatOCR实现OCR文字识别 基于python调用WechatOCR实现OCR文字识别

        前段时间一直在寻找一种效果不错的ocr工具,但目前市面上开源的工具汉字识别效果都很一般。后面了解到了python中已经有WechatOCR这个接口,因此进行了尝试,最终达到了很好的效果

import os
import json
import time
from wechat_ocr.ocr_manager import OcrManager, OCR_MAX_TASK_ID
wechat_ocr_dir = r"C:\Users\1\AppData\Roaming\Tencent\WeChat\XPlugin\Plugins\WeChatOCR\7079\extracted\WeChatOCR.exe" # OCR的地址,可以用everythin工具查找一下地址
wechat_dir = r"E:\WeChat\[3.9.11.19]" # 一定是要包含mmmojo.dll的路径
def ocr_result_callback(img_path: str, results: dict):
 """
 OCR 结果回调函数
 """
 result_file = os.path.basename(img_path) + ".json"
 print(f"识别成功,img_path: {img_path}, result_file: {result_file}")
 with open(result_file, 'w', encoding='utf-8') as f:
 f.write(json.dumps(results, ensure_ascii=False, indent=2))
 # 打印 results 的内容以便调试
 print("OCR 结果:", json.dumps(results, ensure_ascii=False, indent=2))
 # 尝试提取识别的文字
 if 'ocrResult' in results:
 text_lines = [item['text'] for item in results['ocrResult']]
 print(f"识别的文字: {text_lines}")
 else:
 print("结果中没有 'ocrResult' 键,无法提取文字。")
def main():
 # 创建 OCR 管理器实例
 ocr_manager = OcrManager(wechat_dir)
 # 设置 WeChatOCR 目录
 ocr_manager.SetExePath(wechat_ocr_dir)
 # 设置微信所在路径
 ocr_manager.SetUsrLibDir(wechat_dir)
 # 设置 OCR 识别结果的回调函数
 ocr_manager.SetOcrResultCallback(ocr_result_callback)
 # 启动 OCR 服务
 ocr_manager.StartWeChatOCR()
 # 开始识别图片
 ocr_manager.DoOCRTask(r"D:\微信图片_20240710094901.jpg")
 # 等待 OCR 任务完成
 time.sleep(1)
 while ocr_manager.m_task_id.qsize() != OCR_MAX_TASK_ID:
 pass
 # 结束 OCR 服务
 ocr_manager.KillWeChatOCR()
if __name__ == "__main__":
 main()

如果想要提取内容,我在这声明了一个全局变量,用以提取内容:

def ocr_result_callback(img_path: str, results: dict):
 """
 OCR 结果回调函数
 """
 global global_text_lines # 使用全局变量
 result_file = os.path.basename(img_path) + ".json"
 # print(f"识别成功,img_path: {img_path}, result_file: {result_file}")
 with open(result_file, 'w', encoding='utf-8') as f:
 f.write(json.dumps(results, ensure_ascii=False, indent=2))
 # 打印 results 的内容以便调试
 # print("OCR 结果:", json.dumps(results, ensure_ascii=False, indent=2))
 # 尝试提取识别的文字
 if 'ocrResult' in results:
 global_text_lines = [item['text'] for item in results['ocrResult']]
 # print(f"识别的文字: {global_text_lines}")
 else:
 print("结果中没有 'ocrResult' 键,无法提取文字。")

欢迎交流,请勿商用,转载请注明出处!!!

作者:祝宁旺上加旺原文地址:https://blog.csdn.net/weixin_56849735/article/details/140961023

%s 个评论

要回复文章请先登录注册