美文网首页Python学习
随记-点选验证码

随记-点选验证码

作者: DragonFangQy | 来源:发表于2024-03-01 09:45 被阅读0次

文字验证码(点击文字)

  • 模板匹配(从一张图片中寻找 icon),放弃,目前准确率不高,且处理过程复杂

    • 灰度处理
    • 将 complete_image_path 截取并另存为 target_image_path, verify_image_path
    • 将 target_image_path 截取并另存为 icon
    • 从 verify_image_path 中找到 icon
  • 借助 ddddocr

    • ddddocr
    • 直接将 complete_image_path,target_image_path,verify_image_path 丢给 ddddocr 获取结果

主要代码

save_image



def save_image(style_value):
    """
        保存图片(目标图片 和 验证图片)
    """
    
    # 获取验证图片
    bg_img_url = re.search(r"""url\("(?P<url>.*?)"\);""", style_value)
    bg_img_width = re.search(r""".*width:.*?(?P<width>.*?)px;""", style_value)
    bg_img_height = re.search(r""".*height:.*?(?P<height>.*?)px;""", style_value)
    bg_img_right = re.search(r""".*right:.*?(?P<right>.*?)px;""", style_value)
    bg_img_top = re.search(r""".*top:.*?(?P<top>.*?)px;""", style_value)
 
    complete_image_path = None
    image_name = ""

    size_info = {}
    if bg_img_url:
        url = bg_img_url.groupdict().get("url", "")
        re_image_name = re.search(r""".*/(?P<image_name>.*?).jpg\?""", url)

        if re_image_name:
            temp_key = "image_name"
            image_name = re_image_name.groupdict().get(temp_key, "")

            size_info.update(bg_img_width.groupdict())
            size_info.update(bg_img_height.groupdict())
            size_info.update(bg_img_right.groupdict())
            size_info.update(bg_img_top.groupdict())

        data = requests.get(url, verify=False)

        complete_image_path = f"{base_path}{image_name}_complete.jpg"

        # 保存验证图片(原图)
        with open(complete_image_path, "wb") as wbf:
            wbf.write(data.content)
    
    image_obj = cv2.imread(complete_image_path)

    height, width, channel = image_obj.shape

    for element_key, element_value in size_info.items():
        size_info[element_key] = int(element_value)

    point = (0, height-size_info["height"], size_info["width"], height)
    image_crop_target = image_obj[height-size_info["height"]:height, 0:size_info["width"]]
    image_crop_verify = image_obj[0:height-size_info["height"], 0:width]
    
    # 保存目标图片
    target_image = f"{image_name}_target.jpg"
    target_image_path = f"{base_path}{target_image}"
    # cv2.imshow("image_crop", image_crop_target)
    # cv2.waitKey(0)
    cv2.imwrite(target_image_path, image_crop_target)
    
    # 保存verify图片
    verify_image = f"{image_name}_verify.jpg"
    verify_image_path = f"{base_path}{verify_image}"
    # cv2.imshow("image_crop", image_crop_target)
    # cv2.waitKey(0)
    cv2.imwrite(verify_image_path, image_crop_verify)

    return complete_image_path, target_image_path, verify_image_path

save_image_crop


def save_image_crop(image_path, width_size = 26):
    """
        裁切目标图片
    """
    
    image_obj = Image.open(image_path)

    width, height = image_obj.width, image_obj.height 

    left_top_point = [] # 左上
    for i in range(0, width, width_size):
        left_top_point.append((i, 0))

    right_bottom_point = [] # 右下
    for i in range(width_size, width, width_size):
        right_bottom_point.append((i, height))
 
    crop_target_list = []
    min_len = min(len(left_top_point), len(right_bottom_point))
    for i in range(min_len):

        temp_list = []
        temp_list.extend(left_top_point[i])
        temp_list.extend(right_bottom_point[i])

        point = tuple(temp_list) 
        image_crop = image_obj.crop(point)
        
        re_image_name = re.search(r""".*/(?P<image_name>.*?).jpg""", image_path)

        image_name = ""
        if re_image_name:
            temp_dict = re_image_name.groupdict()
            image_name = temp_dict["image_name"] 

        image_name_list = image_name.split("_")
        image_name_list.append(str(i))

        new_image_name = "_".join(image_name_list)
        new_image_path = f"{base_path}/{new_image_name}.jpg"

        image_crop.save(new_image_path)
        crop_target_list.append(new_image_path)
    return crop_target_list, height
    

object_detection


def object_detection(complete_image_path):
    det = ddddocr.DdddOcr(det=True, show_ad=False)

    with open(complete_image_path, 'rb') as f:
        image = f.read()

    poses = det.detection(image)
    print(poses) 
        
    im = cv2.imread(complete_image_path)

    for box in poses:
        x1, y1, x2, y2 = box
        im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)

    cv2.imshow("object_detection", im)
    cv2.waitKey(0)

main


if __name__ == "__main__":
    driver = get_driver()  
    
    driver.get('https://www.bixxxxli.com/')

    click_element(driver, """ //div[@class="header-login-entry"] """)
    click_element(driver, """ //div[@class="login-tab-item"] """)
    input_element(driver, """ //div[@class="login-sms-wp__cid"]/../input """, "15266666666")
    click_element(driver, """ //div[@class="login-sms-wp__cid"]/../div[@class="login-sms-send clickable "] """)

    style_value = get_element_value(driver, """ //div[@class="geetest_widget"]//div[@class="geetest_tip_img"] """, "style")
    complete_image_path, target_image_path, verify_image_path = save_image(style_value)

    object_detection(complete_image_path)
    object_detection(target_image_path)
    object_detection(verify_image_path)
    print
    

Source Code


到此结  DragonFangQy 2024.03.02

本博文仅供学习参考之用,不得用于其他任何目的。如有任何内容侵犯到您的隐私或权益,敬请立即联系我,我将及时删除或修正相关内容。感谢您的理解与支持,期待与您共同维护一个友善、尊重知识产权的网络环境。

相关文章

  • 验证码分类

    验证码类型 1、图形验证码 2、交互式验证码 1)滑动拼图 2)点选 3)短信上行

  • 验证码识别综述

    目录 1.输入识别出的字符类验证码2.点选类验证码3.拖动滑块到指定位置的验证码4.OCR资源集合 1. 输入识别...

  • 你天天在用的验证码,我决定曝光它!!

    很多人觉得验证码很烦,其实作用大着呢~ 今天和大家分享,验证码设计!主要包括: 1.滑块方式 2.文字点选 3.图...

  • 简单的表单验证码

    一般表单中都有验证码,网易易盾有个验证码服务,有些验证做的挺好看。 比如有滑动拼图验证: 还有点选验证: 网易易盾...

  • android 图形验证码(顺序点选文字)

    之前公司接到的一个图形验证码的需求,需求是那种按顺序点选验证码的效果,网上查了下资料,没发现对应的需求,然后就自己...

  • Android自定义点选验证码

    上一篇文章里,通过简单的思路分析和代码演示,最终实现了自定义拼图验证码的效果。接下来,我们要实现的是如下图所示的汉...

  • Android 自定义点选验证码

    代码实现 TapVerificationView.java VerifyCationDialog.java dia...

  • jQuery + SpringMVC 集成极验验证码插件

    极验有一款行为验证的插件,其实就是个验证码插件,包括滑块和点选的验证方式,这里记录一下如何接入基于 jQuery ...

  • Vue + SpringBoot 集成极验验证码插件

    极验有一款行为验证的插件,其实就是个验证码插件,包括滑块和点选的验证方式,这里记录一下如何接入基于 Vue + S...

  • 使用jQuery框架实现图形点选验证码

    一、下载框架文件,放到项目素材(js、css、img)目录 jQuery验证码插件 二、新建页面,按格式调用验证码...

网友评论

    本文标题:随记-点选验证码

    本文链接:https://www.haomeiwen.com/subject/ankczdtx.html