python版本 微信ocr调用

pip install wechat-ocr
import base64
import os
import json
import time
from wechat_ocr.ocr_manager import OcrManager, OCR_MAX_TASK_ID
from yscredit_tools.utils import md5string
wechat_ocr_dir = r"C:\Users\YS\AppData\Roaming\Tencent\WeChat\XPlugin\Plugins\WeChatOCR\7079\extracted\WeChatOCR.exe"
wechat_dir = "C:\Program Files (x86)\Tencent\WeChat\[3.9.10.19]"


def ocr_result_callback(img_path:str, results:dict):
    print(results["ocrResult"])
    # results["img_path"] = img_path
    # with open('1.yaml', 'a', encoding='utf-8') as f:
    #    f.write(json.dumps(results, ensure_ascii=False, indent=2))
    #    f.write('\n')
def main(ocr_manager,image_paths):
    for image_path in image_paths:
        ocr_manager.DoOCRTask(image_path)
    print('任务长度: ',ocr_manager.m_task_id.qsize())
    while ocr_manager.m_task_id.qsize() != OCR_MAX_TASK_ID:
        pass
    # 识别输出结果
    print('???')
    ocr_manager.KillWeChatOCR()


if __name__ == "__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()
    code_lst = []
    image_paths = []
    # for code in code_lst:
    #     code = base64.b64decode(code.split(':')[1])
    #     name = md5string(code)
    #     with open(f'./img/{name}.png','wb') as f:
    #         f.write(code)
    #     image_paths.append(f'./img/{md5string(code)}.png')
    image_paths.append(f'./img/img.png')
    main(ocr_manager,image_paths)

你可能感兴趣的:(python,微信,ocr)