Python 破解 滑动验证码 案例

我们可以借用opencv来解决这个问题,主要步骤:

  1. 读取图片
  2. 高斯模糊处理
  3. Canny边缘检测
  4. 轮廓检测
  5. 获取位置

opencv 是什么?
OpenCV(Open Source Computer Vision Library)是开放源代码计算机视觉库,主要算法涉及图像处理、计算机视觉和机器学习相关方法,可用于开发实时的图像处理、计算机视觉以及模式识别程序。

安装

pip install opencv-python

代码

import cv2 as cv

image_path = 'captcha01.jpg'
image = cv.imread(image_path)
cv.imshow("image", image)

# 高斯模糊
blurred = cv.GaussianBlur(image, (5, 5), 0)
cv.imshow("blurred", blurred)

# 边缘检测
canny = cv.Canny(blurred, 200, 400)
cv.imshow("canny", canny)


# 轮廓识别
def contour_discern_all():
    contours, hierarchy = cv.findContours(canny, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
    for contour in contours:  # 所有轮廓
        x, y, w, h = cv.boundingRect(contour)
        cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
        cv.imshow('contour_all', image)


# 添加限制
def contour_discern():
    contours, hierarchy = cv.findContours(canny, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
    for contour in contours:  # 所有轮廓
        print('轮廓面积:', cv.contourArea(contour), '轮廓周长:', cv.arcLength(contour, True))
        # 对周长和面积添加限制(以下数值仅作用于当前案例)
        if 35 > cv.contourArea(contour) > 20 and 470 > cv.arcLength(contour, True) > 460:
            x, y, w, h = cv.boundingRect(contour)
            print(x, y, w, h)  # 坐标和大小
            cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv.imshow('contour', image)


contour_discern()
contour_discern_all()
cv.waitKey()
cv.destroyAllWindows()

效果图
在这里插入图片描述
如上就找到了目标位置,剩下的工作可以使用selenium或其他工具,将滑块移动到指定位置即可。

参考文献

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CtrlCV工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值