Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize picture CAPTCHA recognition by Python+Selenium+Pytesseract

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you Python+Selenium+Pytesseract how to achieve picture CAPTCHA recognition, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. Selenium intercepts verification code import jsonfrom io import BytesIOimport timefrom test.testBefore.testDriver import driverfrom test.util.test_pytesseract import recognizefrom PIL import Imageimport allureimport unittest''/ processing verification code''# elements to capture screenshots element = driver.find_element_by_xpath ('/ * [@ id= "imgVerifyCode"]') # coordinates x, y = element.location.values () # width and height h W = element.size.values () # return the screenshot in binary form image_data = driver.get_screenshot_as_png () # Open the returned data in a new picture screenshot = Image.open (BytesIO (image_data)) # crop the screenshot result = screenshot.crop ((x, y, x + w) Y + h) # display picture # result.show () # Save CAPTCHA picture result.save ('VerifyCode.png') # call recognize method CAPTCHA code = recognize (' VerifyCode.png') # enter CAPTCHA driver.find_element_by_xpath ('/ * [@ id= "CAPTCHA"]'). Send_keys (code) 'process CAPTCHA /''

Note: driver refers to a file written by myself, and you can write one by yourself. The code to identify the picture is placed separately under the util folder, refer to the code under title 3, and refer to it if necessary. The above code positioning elements need to be modified according to their own project positioning elements.

Second, install the identification environment pytesseract+Tesseract-OCR

If there is no output, and you are not sure whether your pytesseract environment is installed, you can identify it with a picture without interference to see if you can have the output result. The following example can directly output the recognition result 8fnp in my environment.

Verify that the identification environment is normal

Use pytesseract to identify pictures directly

001.png

Text = pytesseract.image_to_string ('. / 001.png') print (text) 3. Process CAPTCHA pictures

Directly screenshot of the CAPTCHA image there are noise or interference lines, etc., directly using pytesseract recognition may not output results, if the environment is normal, but no output results, it is mostly because the picture is not properly processed, can not be identified, you can try some more ways to deal with pictures, the following code to deal with screenshots of this similar picture effect is better.

Image processing and recognition

The process of processing a picture:

In the process of image processing, you can use im.show () to see if the picture after each step is in line with expectations, and adjust the parameters if the effect is not good. In addition, in the process of learning, it is found that some children's shoes say that the picture can be identified by using cv2.resize (). You can refer to the application of the image scaling resize () function in Python.

The actual captured picture

Processed picture

Test_pytesseract.py

Import pytesseractfrom fnmatchimport fnmatchimport cv2import osdef clear_border (img, img_name):''remove the border' h, w = img.shape [: 2] for y in range (0, w): for x in range (0, h): # if y = = 0 or y = = w-1 or y = = w-2: if y

< 2 or y >

W-2: img [x, y] = 255 # if x = = 0 or x = = h-1 or x = = h-2: if x

< 1 or x >

H-1: img [x, y] = 255return imgdef interference_line (img, img_name):''interference line noise reduction''h, w = img.shape [: 2] #! The opencv matrix is inverse # img [1] 1: the height of the picture Width of the picture for r in range (0,2): for y in range (1, w-1): for x in range (1, h-1): count = 0 if img [x, y-1] > 245: count = count + 1 if img [x Y + 1] > 245: count = count + 1 if img [x-1, y] > 245: count = count + 1 if img [x + 1, y] > 245: count = count + 1 if count > 2: img [x, y] = 255 return imgdef interference_point (img Img_name, x = 0, y = 0): "point noise reduction 9 neighborhood box, field box centered on the current point, number of black dots: param x:: param y:: return:" # todo determines the lower limit of the length and width of the picture cur_pixel = img [x, y] # the value of the current pixel height, width = img.shape [: 2] for y in range (0 Width-1): for x in range (0, height-1): if y = = 0: # first line if x = = 0: # upper left vertex, 4 neighborhood # three points next to the center point sum = int (cur_pixel)\ + int (IMG [x] Y + 1])\ + int (IMG [x + 1, y])\ + int (IMG [x + 1, y + 1]) if sum

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report