无需联网,使用python调用离线版微信OCR模块,快速文字识别

364 阅读1分钟

首先感谢52破解的这篇帖子,【windows 微信OCR离线版】无需联网准确快速实现文字识别 - 吾爱破解 - 52pojie.cn

原作者是使用aardio语言写的桌面应用,以下根据原作者的aardio代码,将核心功能翻译成了python版本

不需要安装微信,只需要下载离线OCR模块即可

微信OCR离线模块地址:wwko.lanzoue.com/iEbys2rrmm4… 密码:h9bc

pip install wechat-ocr
import os, json, time
from wechat_ocr.ocr_manager import OcrManager, OCR_MAX_TASK_ID

wechat_ocr_dir = os.path.abspath(r"./wxocr/WeChatOCR.exe")
wechat_dir = os.path.abspath(r"./wxocr")

def ocr_result_callback(img_path:str, results:dict):
    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))

def main():
    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服务初始化

    # 开始识别图片
    ocr_manager.DoOCRTask(os.path.abspath(r".JPG/ocr_screenshot.jpg"))
    time.sleep(1)
    while ocr_manager.m_task_id.qsize() != OCR_MAX_TASK_ID:
        pass
    # 识别输出结果
    ocr_manager.KillWeChatOCR()
    
if __name__ == "__main__":
    main()

识别之后会输出一个json结果

image.png

里面会包含识别结果和每个结果对应原始图片的坐标位置

image.png