python 使用ddddocr库实现滑块验证码滑动验证

时间:2024-01-23 20:50:57

一. 识别滑块缺口

  1. 使用ddddocr识别
    该算法识别准确率为95%左右,测试三轮,每轮测试100次

def generate_distance(slice_url, bg_url):
    """
    :param bg_url: 背景图地址
    :param slice_url: 滑块图地址
    :return: distance
    :rtype: Integer
    """
    slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
    slice_image = requests.get(slice_url).content
    bg_image = requests.get(bg_url).content
    result = slide.slide_match(target_bytes, bg_image, simple_target=True)
    return result['target'][0]
  1. 使用cv2识别
    该算法识别准确率为95%左右,测试三轮,每轮测试100次

def generate_distance(slice_url, bg_url):
    """
    :param bg_url: 背景图地址
    :param slice_url: 滑块图地址
    :return: distance
    :rtype: Integer
    """
    slice_image = np.asarray(bytearray(requests.get(slice_url).content), dtype=np.uint8)
    slice_image = cv2.imdecode(slice_image, 1)
    slice_image = cv2.Canny(slice_image, 255, 255)

    bg_image = np.asarray(bytearray(requests.get(bg_url).content), dtype=np.uint8)
    bg_image = cv2.imdecode(bg_image, 1)
    bg_image = cv2.pyrMeanShiftFiltering(bg_image, 5, 50)
    bg_image = cv2.Canny(bg_image, 255, 255)

    result = cv2.matchTemplate(bg_image, slice_image, cv2.TM_CCOEFF_NORMED)

    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    return max_loc[0]

二. 构造滑块轨迹

  1. 构造轨迹库
    图片长度为300,理论上就300种轨迹,实际上应该是200+种,还要减去滑块图的长度80
    手动滑他个几百次,并把距离和轨迹记录下来,识别出距离后直接查对应轨迹
  2. 算法构造轨迹track

def generate_track(distance):
    def __ease_out_expo(step):
        return 1 if step == 1 else 1 - pow(2, -10 * step)

    tracks = [[random.randint(20, 60), random.randint(10, 40), 0]]
    count = 30 + int(distance / 2)
    _x, _y = 0, 0
    for item in range(count):
        x = round(__ease_out_expo(item / count) * distance)
        t = random.randint(10, 20)
        if x == _x:
            continue
        tracks.append([x - _x, _y, t])
        _x = x
    tracks.append([0, 0, random.randint(200, 300)])
    times = sum([track[2] for track in tracks])
    return tracks, times

三. 结语

本篇文章篇幅不长,主要也没啥好说的,验证码研究多了,识别和轨迹就那几套方法,换汤不换药
函数a(e, t)中的重头戏:c.guid()、_.encrypt()、i.encrypt()、c.arrayToHex()四个函数我们放到浩瀚篇再说吧,不然我这紫极魔瞳四大境界变成三大境界了,哈哈哈