OCR字符识别--WeChatocr

导入库,及定义微信OCR地址

from wechat_ocr.ocr_manager import OcrManager, OCR_MAX_TASK_ID
import threading

wechat_ocr_dir = r"./ocr/WeChatOCR/7079/extracted"  # OCR的地址,可以用everythin工具查找一下地址  这个地址是电脑用户安装APP的地址 ---C:\Users\xxxx\AppData\Roaming\Tencent\WeChat\XPlugin\Plugins\WeChatOCR

wechat_dir = r"./ocr/[3.9.12.17]"  # 一定是要包含mmmojo.dll的路径  ---这个为微信APP的安装位置

定义一个微信检测的类

class WeChatOCRWrapper:
    def __init__(self, wechat_ocr_dir, wechat_dir):
        # 初始化 OCRManager 和其他资源
        self.ocr_manager = OcrManager(wechat_dir)
        self.ocr_manager.SetExePath(wechat_ocr_dir)
        self.ocr_manager.SetUsrLibDir(wechat_dir)
        self.ocr_manager.SetOcrResultCallback(self.ocr_result_callback)
        self.temp_image_path = None
        self.result_queue = queue.Queue()  # 创建用于存储 OCR 结果的队列
        self.result_ready = threading.Condition()  # 创建条件变量

    def start_ocr_service(self):
        self.ocr_manager.StartWeChatOCR()

    def stop_ocr_service(self):
        self.ocr_manager.KillWeChatOCR()

    def ocr_result_callback(self, img_path: str, results: dict):
        # 尝试提取识别的文字
        if 'ocrResult' in results:
            text_lines = [item['text'] for item in results['ocrResult']]
            print(f"识别的文字: {text_lines}")

            # # 将识别的文字写入到文件中
            # file_path = 'recognized_texts.txt'
            # with open(file_path, 'a', encoding='utf-8') as file:
            #     for line in text_lines:
            #         file.write(line + '\n')  # 每个文本行后添加一个换行符

        with self.result_ready:
            # 将结果放入队列
            # self.result_queue.put(results)
            self.result_queue.put(text_lines)
            # 通知等待的线程
            self.result_ready.notify_all()

    def process_and_recognize(self, image_path: str, timeout: float = 5.0):
        # 启动 OCR 任务
        self.ocr_manager.DoOCRTask(image_path)

        # 等待 OCR 结果
        with self.result_ready:
            end_time = time.time() + timeout
            while True:
                try:
                    # 尝试从队列中获取结果(不阻塞)
                    result = self.result_queue.get_nowait()
                    return result
                except queue.Empty:
                    # 如果队列为空,则检查是否超时
                    current_time = time.time()
                    if current_time >= end_time:
                        # 超时则返回 None
                        return None
                    # 否则,等待直到被通知或超时
                    remaining_time = end_time - current_time
                    self.result_ready.wait(remaining_time)

微信的回调函数有可以根据需求更改

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' 键,无法提取文字。")

创建启动OCR服务

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

 image_path ="Pic_20241224111513877.bmp"  # 替换为你的图像路径   白车   thresh   10
    image = cv2.imread(image_path)
    if image is None:
        raise FileNotFoundError("指定的图片文件不存在。")
    # show_image_plot(image)
    # 转换为灰度图像
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # #  todo 应用二值化
    _, binary_image = cv2.threshold(gray_image, 10, 255, cv2.THRESH_BINARY)  # thresh  60 灰度值60以下为黑色, 以上为白
    # #
    # todo 将二值化图像中的白色和黑色翻转
    inverted_binary_image = cv2.bitwise_not(binary_image)

    # 创建一个临时文件来保存处理后的图像
    with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmpfile:
        temp_image_path = tmpfile.name
        cv2.imwrite(temp_image_path, inverted_binary_image)

    #    todo 旋转图像
    # rotate_image = rotate_gray_image(gray_image, -8)
    # # #
    # show_image_plot(inverted_binary_image)



    ocr_manager.DoOCRTask(image_path)
    # 等待 OCR 任务完成
    time.sleep(1)
    while ocr_manager.m_task_id.qsize() != OCR_MAX_TASK_ID:
        pass
    # 结束 OCR 服务
    ocr_manager.KillWeChatOCR()

    # 清理临时文件
    os.remove(temp_image_path)


if __name__ == "__main__":
    main()

你可能感兴趣的:(ocr)