今天给大家推荐一款验证码识别神器:dddocr

1、介绍

dddocr是一个基于深度学习的OCR(Optical Character Recognition,光学字符识别)库,用于识别图片中的文字。它可以识别各种类型的文字,包括印刷体、手写体、表格、条形码等。dddocr库使用了深度卷积神经网络(CNN)和循环神经网络(RNN)等先进的模型,具有较高的准确性和稳定性。

使用dddocr库可以方便地进行文字识别的开发和应用。它提供了简单易用的API接口,可以接收图片作为输入,返回识别结果。用户只需要将待识别的图片传入dddocr库的API接口,即可获取识别出的文字信息。同时,dddocr库还支持批量处理多张图片,提供了多线程和分布式处理的功能,可以提高识别速度和效率。

dddocr库可以广泛应用于各种场景,例如文档数字化、图像检索、自动化办公等。它可以帮助用户快速准确地提取图片中的文字信息,方便进行后续的处理和分析。在实际应用中,dddocr库已经被广泛应用于金融、医疗、物流等领域,取得了良好的效果。

2、基本使用

安装:pip install dddocr

利用dddocr库可以支持识别不同类型的验证码。

示例1:英文字母验证码识别

 
  1. import dddocr

  2. def recognize_letter_captcha(image_path):

  3.     ocr = dddocr.DddOcr()

  4.     result = ocr.classification(image_path, model_type='letter')

  5.     return result

  6. image_path = 'letter_captcha.png'

  7. result = recognize_letter_captcha(image_path)

  8. print(result)

示例2:数字验证码识别

 
  1. import dddocr

  2. def recognize_number_captcha(image_path):

  3.     ocr = dddocr.DddOcr()

  4.     result = ocr.classification(image_path, model_type='number')

  5.     return result

  6. image_path = 'number_captcha.png'

  7. result = recognize_number_captcha(image_path)

  8. print(result)

示例3:混合验证码识别

 
  1. import dddocr

  2. def recognize_mixed_captcha(image_path):

  3.     ocr = dddocr.DddOcr()

  4.     result = ocr.classification(image_path, model_type='mixed')

  5.     return result

  6. image_path = 'mixed_captcha.png'

  7. result = recognize_mixed_captcha(image_path)

  8. print(result)

示例4:滑动验证码识别

 
  1. import dddocr

  2. def recognize_slide_captcha(image_path):

  3.     ocr = dddocr.DddOcr()

  4.     result = ocr.slide_captcha(image_path)

  5.     return result

  6. image_path = 'slide_captcha.png'

  7. result = recognize_slide_captcha(image_path)

  8. print(result)

示例5:中文验证码识别

 
  1. import dddocr

  2. def recognize_chinese_captcha(image_path):

  3.     ocr = dddocr.DddOcr()

  4.     result = ocr.classification(image_path, model_type='chinese')

  5.     return result

  6. image_path = 'chinese_captcha.png'

  7. result = recognize_chinese_captcha(image_path)

  8. print(result)

以上示例中,image_path为待识别的验证码图片路径,通过调用不同的识别函数来识别不同类型的验证码。每个示例都创建了一个dddocr的实例,然后调用相应的方法进行识别。识别结果会以字符串形式返回。实际应用中,可能需要根据具体情况进行参数调整和模型训练,以提高识别准确性。

3、selenium+dddorc自动化登录识别验证码

使用selenium和dddocr进行自动登录时,可以通过以下步骤识别验证码:

  1. 安装selenium和dddocr库:

 
  1.   pip install selenium

  2.   pip install dddocr

  1. 导入必要的库和模块:

 
  1.    from selenium import webdriver

  2.    from selenium.webdriver.common.by import By

  3.    from selenium.webdriver.support.ui import WebDriverWait

  4.    from selenium.webdriver.support import expected_conditions as EC

  5.    import dddocr

  1. 创建一个dddocr的实例:

  ocr = dddocr.DddOcr()
  1. 使用selenium打开登录页面,并找到验证码图片元素:

 
  1.    driver = webdriver.Chrome()

  2.    driver.get('https://example.com/login')

  3.    captcha_image = driver.find_element(By.ID, 'captcha-image')

  1. 获取验证码图片的src属性,并下载保存到本地:

 
  1.   captcha_image_src = captcha_image.get_attribute('src')

  2.   driver.get_screenshot_as_file('screenshot.png')

  1. 使用dddocr识别验证码:

 
  1.    result = ocr.classification('screenshot.png', model_type='mixed')

  2.    captcha_code = result[0]['text']

  1. 在登录页面找到验证码输入框,并输入识别出的验证码:

 
  1.    captcha_input = driver.find_element(By.ID, 'captcha-input')

  2.    captcha_input.send_keys(captcha_code)

  1. 输入其他登录信息,并提交表单:

 
  1.    username_input = driver.find_element(By.ID, 'username-input')

  2.    password_input = driver.find_element(By.ID, 'password-input')

  3.    username_input.send_keys('your_username')

  4.    password_input.send_keys('your_password')

  5.    submit_button = driver.find_element(By.ID, 'submit-button')

  6.    submit_button.click()

完整的代码示例:

 
  1. from selenium import webdriver

  2. from selenium.webdriver.common.by import By

  3. from selenium.webdriver.support.ui import WebDriverWait

  4. from selenium.webdriver.support import expected_conditions as EC

  5. import dddocr

  6. ocr = dddocr.DddOcr()

  7. driver = webdriver.Chrome()

  8. driver.get('https://example.com/login')

  9. captcha_image = driver.find_element(By.ID, 'captcha-image')

  10. captcha_image_src = captcha_image.get_attribute('src')

  11. driver.get_screenshot_as_file('screenshot.png')

  12. result = ocr.classification('screenshot.png', model_type='mixed')

  13. captcha_code = result[0]['text']

  14. captcha_input = driver.find_element(By.ID, 'captcha-input')

  15. captcha_input.send_keys(captcha_code)

  16. username_input = driver.find_element(By.ID, 'username-input')

  17. password_input = driver.find_element(By.ID, 'password-input')

  18. username_input.send_keys('your_username')

  19. password_input.send_keys('your_password')

  20. submit_button = driver.find_element(By.ID, 'submit-button')

  21. submit_button.click()

以上代码示例中,假设登录页面的验证码图片元素的id为'captcha-image',验证码输入框的id为'captcha-input',用户名输入框的id为'username-input',密码输入框的id为'password-input',登录按钮的id为'submit-button'。根据实际情况,需要替换这些id值为实际的页面元素id。

注意:上述示例仅适用于验证码图片直接以img标签的形式嵌入在页面中的情况。

4、验证码通过Ajax请求加载如何识别

如果验证码是通过Ajax请求加载的,可以通过以下步骤识别验证码:

  1. 使用selenium打开登录页面,并等待验证码图片加载完成:

 
  1.    driver = webdriver.Chrome()

  2.    driver.get('https://example.com/login')

  3.    wait = WebDriverWait(driver, 10)

  4.    captcha_image = wait.until(EC.presence_of_element_located((By.ID, 'captcha-image')))

  1. 执行JavaScript代码,获取验证码图片的base64编码:

   captcha_image_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(21);", captcha_image)
  1. 将base64编码解码为图片,并保存到本地:

 
  1.    with open('captcha.png', 'wb') as f:

  2.        f.write(base64.b64decode(captcha_image_base64))

  1. 使用dddocr识别验证码:

 
  1.   result = ocr.classification('captcha.png', model_type='mixed')

  2.   captcha_code = result[0]['text']

  1. 在登录页面找到验证码输入框,并输入识别出的验证码:

 
  1.    captcha_input = driver.find_element(By.ID, 'captcha-input')

  2.    captcha_input.send_keys(captcha_code)

  1. 输入其他登录信息,并提交表单:

 
  1.    username_input = driver.find_element(By.ID, 'username-input')

  2.    password_input = driver.find_element(By.ID, 'password-input')

  3.    username_input.send_keys('your_username')

  4.    password_input.send_keys('your_password')

  5.    submit_button = driver.find_element(By.ID, 'submit-button')

  6.    submit_button.click()

完整的代码示例:

 
  1. from selenium import webdriver

  2. from selenium.webdriver.common.by import By

  3. from selenium.webdriver.support.ui import WebDriverWait

  4. from selenium.webdriver.support import expected_conditions as EC

  5. import dddocr

  6. import base64

  7. ocr = dddocr.DddOcr()

  8. driver = webdriver.Chrome()

  9. driver.get('https://example.com/login')

  10. wait = WebDriverWait(driver, 10)

  11. captcha_image = wait.until(EC.presence_of_element_located((By.ID, 'captcha-image')))

  12. captcha_image_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(21);", captcha_image)

  13. with open('captcha.png', 'wb') as f:

  14.     f.write(base64.b64decode(captcha_image_base64))

  15. result = ocr.classification('captcha.png', model_type='mixed')

  16. captcha_code = result[0]['text']

  17. captcha_input = driver.find_element(By.ID, 'captcha-input')

  18. captcha_input.send_keys(captcha_code)

  19. username_input = driver.find_element(By.ID, 'username-input')

  20. password_input = driver.find_element(By.ID, 'password-input')

  21. username_input.send_keys('your_username')

  22. password_input.send_keys('your_password')

  23. submit_button = driver.find_element(By.ID, 'submit-button')

  24. submit_button.click()

以上代码示例中,假设登录页面的验证码图片元素的id为'captcha-image',验证码输入框的id为'captcha-input',用户名输入框的id为'username-input',密码输入框的id为'password-input',登录按钮的id为'submit-button'。根据实际情况,需要替换这些id值为实际的页面元素id。

注意:上述示例仅适用于验证码图片通过Ajax请求加载,并且返回的是base64编码的情况。如果验证码图片是通过其他方式加载的,或者返回的是其他格式的数据(如图片的URL),则需要根据具体情况进行相应的处理。

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

更多推荐