ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python 破解 滑动验证码 案例

2021-05-07 11:59:04  阅读:234  来源: 互联网

标签:Python image imshow 验证码 cv canny 滑动 contour discern


我们可以借用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或其他工具,将滑块移动到指定位置即可。

参考文献

标签:Python,image,imshow,验证码,cv,canny,滑动,contour,discern
来源: https://blog.csdn.net/MeYungle/article/details/116485657

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有