第三代滑块验证码识别接口

  • 图像的处理
  • 切割图像的代码块
  • PNG2Base64
  • 请求参数
  • 返回参数


图像的处理

通过传入模型图和背景图进行匹配,返回位置。

android 滑块验证码开发 滑块验证码识别_爬虫

模板图片:

android 滑块验证码开发 滑块验证码识别_selenium_02


背景图片需要以模板图片的像素大小进行切割,以提高识别率:

android 滑块验证码开发 滑块验证码识别_Image_03

切割图像的代码块

def get_img(browser):
    time.sleep(2)
    while (1):
        try:
            image1 = browser.find_element_by_xpath('//*[@id="captcha_container"]/div/div[2]/img[2]')  # 获取img元素
            location1 = image1.location  # 获取img元素在页面中的位置
            print(location1)
            img_url = image1.get_attribute('src')  # 获取img元素里面的src链接
            print(img_url)
            data = requests.get(img_url)  # 请求链接
            with open("./picture/front.png", 'wb') as f:
                f.write(data.content)  # 把图片以二进制保存
            break
        except Exception as e:
            print("等待匹配图片加载!!!")
    front = Image.open('./picture/front.png').convert("RGBA") 
    front = front.resize((70, 60), Image.ANTIALIAS)  # 因为下载下来的图片大小为68px*68px,和验证码上的图片大小不相同,得修改图片大小
    front.save('./picture/front.png', "png")  # 再次保存图片
    image2 = cv2.imread('./picture/front.png')  # 用cv2读取图片
    w, h = image2.shape[:2]  # 获取图片的宽和高,待会要用
    while(1):
        try:
            image = browser.find_element_by_xpath('//*[@id="captcha-verify-image"]')
            location = image.location  # 获取图片的位置
            size = image.size  # 获取图片的大小
            screenshot = browser.get_screenshot_as_png()  # 获取浏览器页面截图 数据类型以utf-8编码
            screenshot = Image.open(BytesIO(screenshot))  # 将utf-8编码转化为二进制,并以图片的方式打开
            print(location)
            top, bottom, left, right = location1['y'], location1['y'] + h, location1['x'] + w, location['x'] + size['width']  #用小图片和大图片的长宽高去进行剪裁
            captcha = screenshot.crop((left, top, right, bottom))  # 进行裁剪
            captcha.save("./picture/bg.png")  # 保存在bg.png中
            break
        except Exception as e:
            print(e)
            print("等待背景图片加载!!!")

PNG2Base64

因为该接口用的是get请求的方法,所以传入图像需要转化为base64格式。

def Png2Basse64(fileName):
    PngFileName = './picture/' + fileName + '.png'
    TxtFileName = './picture/' + fileName + '.txt'
    with open(PngFileName,"rb") as f:#转为二进制格式
        base64_data = base64.b64encode(f.read())#使用base64进行加密
        print(base64_data)
        file=open(TxtFileName,'wb')#写成文本格式
        file.write(base64_data)
        file.close()

请求参数

请求类型:GET, POST

模板:

url = "http://112.74.47.53:5000//SliderVerification"
 params = {"frontBase64Code": bytes.decode(frontCode),"bgBase64Code": bytes.decode(bgCode),"isNeedGray":'1'}

参数名

参数值

描述

frontBase64Code

base64

模板图片的base64

bgBase64Code

base64

背景图片的base64

isNeedGray

0/1

是否要使用灰度图进行判断,默认是1

特别注意: 当验证码的边缘与背景的区别不是特别明显的时候需关闭使用灰度图进行识别,这样能提高识别率(1是开启,0是关闭)。

android 滑块验证码开发 滑块验证码识别_python_04


灰度匹配:

android 滑块验证码开发 滑块验证码识别_Image_05


原始图像匹配:

android 滑块验证码开发 滑块验证码识别_android 滑块验证码开发_06

返回参数

distance = {"x-start":top_left[0],"x-end ":top_left[0]+w ,"msg":"1"}
   return json.dumps(distance, ensure_ascii=False, indent=4)  # 输出json格式

返回的是匹配后模板最右边的x轴距离。

android 滑块验证码开发 滑块验证码识别_爬虫_07


识别成功后返回的JSON:

参数名

注释

msg

提示消息(成功,失败,参数错误等)

x-start

匹配后的模板图片x轴起始位置

x-end

匹配后的模板图片x轴终点位置