跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 如何提高滑块验证码的识别精度?

发表于

最近碰到一个需求,需要自动化操作一个网站,第一步就遇到一个问题,滑块验证码登录。

核心逻辑是:

1. 获取两张图片,一张左侧的验证码图片,一张完整的图片
2. 比对两张图片,获取到验证码图片与完整图片重合区域在 X 轴上需要移动的举例
3. 通过 selenium 缓慢移动验证码图片到重合区域,完成登录

在网上找到一个类似的需求和实现方案,跟着写了下代码 https://blog.csdn.net/weixin_53300120/article/details/138068169

写了两个实现的方式:

  1. 采用 ddddocr:

     with open('left.png', 'rb') as f:
    
     target_bytes = f.read()
    
     with open('full.png', 'rb') as f:
         background_bytes = f.read()
    
     ocr = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
    
     res = ocr.slide_match(target_bytes, background_bytes, simple_target=True)
    
     print(res, res['target'][0])
    
  2. 采用 cv2:

     # 加载图片 A 和 B
     image_a = cv2.imread('left.png', cv2.IMREAD_GRAYSCALE)
     image_b = cv2.imread('full.png', cv2.IMREAD_GRAYSCALE)
    
     # 使用模板匹配方法( Template Matching )寻找图片 A 在 B 中的位置
     result = cv2.matchTemplate(image_b, image_a, cv2.TM_CCOEFF_NORMED)
    
     # 获取最大匹配值及其位置
     min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
    
     # 如果 TM_CCOEFF_NORMED 方法使用,可以通过以下方式找到匹配位置的左上角坐标
     top_left = max_loc
    
     # 获取图片 A 的宽度和高度
     height, width = image_a.shape
     print(height, ':', width)
     height2, width2 = image_b.shape
     print(height2, ':', width2)
    
     # 获取最左侧的 x 坐标(左上角)
     leftmost_x = max_loc[0]
     print('leftmost_x:', leftmost_x)
    

现在的问题是,这两种方法的准确度都不高,而且我也不了解他们内部的实现方式,不知道如何提高识别的准确度。 现在用了一个比较笨的方法,死循环,重复 10 次以内大概率是可以成功一次。

想请教大家,这种场景有没有啥好的办法可以提高识别的准确度,或者有没有相关的开源库可以直接用?

感谢!

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.