Python调用微信OCR识别文字和坐标

python的ocr识别最方便的最准确的方法就是直接调微信的ocr
注意:调用的时候 先把微信关掉。

import os
import json
import time
from wechat_ocr.ocr_manager import OcrManager, OCR_MAX_TASK_ID
 
wechat_ocr_dir = r"C:\Users\mydell\AppData\Roaming\Tencent\WeChat\XPlugin\Plugins\WeChatOCR\7061\extracted\WeChatOCR.exe"
wechat_dir = "C:\Program Files (x86)\Tencent\WeChat\[3.9.8.25]"
 
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_manager.DoOCRTask(r"pic\\90.png")
    ocr_manager.DoOCRTask(r"pic\\92.png")
    ocr_manager.DoOCRTask(r"pic\\96.png")
    time.sleep(1)
    while ocr_manager.m_task_id.qsize() != OCR_MAX_TASK_ID:
        pass
    # 识别输出结果
    ocr_manager.KillWeChatOCR()
     

main()

https://bbs.kanxue.com/thread-279653-1.htm
转发 执念成狂 大佬的文章

posted on 2024-01-30 12:25  耀扬  阅读(472)  评论(0编辑  收藏  举报

导航