ddddocr 点选验证码识别

 

 

直接上代码

复制代码
# -*- coding: utf-8 -*-
# @Author   : xcl
# @FILE     : 1.py
# @Time     : 2025/2/15 7:26
# @Software : PyCharm

# example.py
import time
import ddddocr
import ddddocr

from PIL import Image, ImageDraw
import ddddocr

file_name = '6.jpg'

from io import BytesIO  # 必须添加此导入
def process_image(poses):
    # 初始化OCR
    ocr = ddddocr.DdddOcr(show_ad=False)

    # 直接打开本地图片 1.jpg
    img = Image.open(file_name)  # 确保图片在当前工作目录,或使用绝对路径如 "C:/path/to/1.jpg"

    draw = ImageDraw.Draw(img)
    click_identify_result = {}

    for row in poses:
        # 扩展矩形区域(防止越界)
        x1 = max(0, row[0] - 3)
        y1 = max(0, row[1] - 3)
        x2 = min(img.width, row[2] + 3)
        y2 = min(img.height, row[3] + 3)

        # 绘制红色边框
        draw.rectangle([x1, y1, x2, y2], outline="red", width=1)

        # 裁剪区域
        corp = img.crop((x1, y1, x2, y2))

        # 保存到内存并识别
        img_byte = BytesIO()
        corp.save(img_byte, format='PNG')
        img_byte.seek(0)  # 重置指针

        # OCR识别
        word = ocr.classification(img_byte.getvalue())
        click_identify_result[word] = (x1, y1, x2, y2)

    # img.show()  # 显示结果图
    return click_identify_result

import cv2
det = ddddocr.DdddOcr(det=True,show_ad=False)
with open(file_name, 'rb') as f:
    image = f.read()
poses = det.detection(image)
print(poses)
# time.sleep(222)
# poses = [(194, 31, 228, 66), (308, 106, 342, 140)]  # 替换为实际坐标列表
result = process_image(poses)
print("识别结果:", result)

# time.sleep(2)
复制代码

 

posted @   淋哥  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示