Automatic testing method for identifying computational verification code based on image processing
Through image preprocessing and OCR technology, the problem of identifying computed verification codes is solved in the existing technology's difficulty in identifying complex verification codes, and efficient identification and analysis of automated tests is realized, which improves testing efficiency and accuracy.
Patent Information
- Application Number
- CN202410913110.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-09
- Publication Date
- 2024-11-01
AI Technical Summary
When the prior art processes computational verification codes with interference lines, complex backgrounds or distorted characters, image processing and OCR technology are poor, making it difficult to achieve efficient identification of automated tests.
The image preprocessing steps include acquisition, grayscale, binarization, denoising and character segmentation, combining OCR technology and open source text recognition engine Tesseract, identify verification codes through custom keywords and ddddocr packages, and parse mathematics using Python expression.
It realizes automatic identification and solution of computed verification codes, and improves the efficiency and accuracy of automated testing.
Smart Images

Figure CN118885995A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer vision, and in particular to an automated testing method for recognizing computational verification codes based on image processing. Background Art
[0002] In Internet applications, computational CAPTCHAs are widely used to prevent attacks from automated scripts and malicious users. This type of CAPTCHA requires users to answer a simple math problem (such as addition, subtraction, etc.) to verify their identity. Although this type of CAPTCHA is user-friendly, it is a challenge for automated test scripts.
[0003] Existing technologies, such as image processing and OCR technology, do not work well when processing computational CAPTCHAs, especially when the CAPTCHAs have interference lines, complex backgrounds, or distorted characters.
[0004] In order to optimize the processing effect of image processing technology on computational verification codes, the present invention proposes an automated testing method for recognizing computational verification codes based on image processing. Summary of the invention
[0005] In order to make up for the defects of the prior art, the present invention provides a simple and efficient automatic testing method for a verification code based on image processing and recognition calculation.
[0006] The present invention is achieved through the following technical solutions:
[0007] An automated testing method for recognizing computational verification codes based on image processing, characterized in that it comprises the following steps:
[0008] Step S1: Image preprocessing
[0009] Step S1.1, image acquisition: obtaining a computational verification code image from a target website or application;
[0010] Step S1.2, grayscale processing: convert the image into a grayscale image to reduce the image complexity;
[0011] Step S1.3, binarization processing: converting the grayscale image into a black and white binary image to better segment the characters;
[0012] Step S1.4, denoising: using image processing technology to remove noise;
[0013] Step S1.5, character segmentation: separate the characters and symbols in the image to prepare for subsequent recognition;
[0014] In step S1.4, median filtering is used, and morphological operations, corrosion and / or dilation are adopted to remove noise.
[0015] Step S2: Character recognition
[0016] Step S2.1, using OCR technology to recognize the segmented characters and symbols;
[0017] Step S2.2, customize a get_verifycode method to obtain the verification code, and put the method in any Python source code py file in the selenium library and define it as a keyword;
[0018] Step S2.3, in the RF (Robot Framework, a keyword-driven automated testing framework based on Python) automation script, call the keyword get_verifycode to obtain the verification code;
[0019] In step S2.1, the open source text recognition engine Tesseract OCR is used to recognize the segmented characters and symbols.
[0020] In the step S2.2, when customizing keywords, the verification code is identified through the ddddocr package.
[0021] Step S3: mathematical expression analysis
[0022] Step S3.1, combining mathematical expressions: combining the recognized characters into mathematical expressions;
[0023] Step S3.2, solving the expression: using Python's built-in eval function or a custom parser to solve the mathematical expression;
[0024] Step S4: Result verification and script integration
[0025] Step S4.1, submit the result: submit the solved result to the target website or application for verification;
[0026] Step S4.2, automated test script integration: Integrate the above steps into the automated test script to achieve automated recognition and processing of computational verification codes.
[0027] An automated testing system for recognizing computational verification codes based on image processing, comprising:
[0028] Image preprocessing module, responsible for image acquisition, grayscale processing, binarization processing, denoising and character segmentation;
[0029] Image acquisition: Obtain computational CAPTCHA images from target websites or applications;
[0030] Grayscale processing: convert the image into a grayscale image to reduce the complexity of the image;
[0031] Binarization: Convert the grayscale image into a black and white binary image to better segment the characters;
[0032] Denoising: Use image processing technology to remove noise;
[0033] Character segmentation: separate the characters and symbols in the image to prepare for subsequent recognition;
[0034] The image preprocessing module uses median filtering, morphological operations, corrosion and / or expansion to remove noise.
[0035] The character recognition module is responsible for using OCR technology to recognize the segmented characters and symbols; customize a get_verifycode method and put it in any Python source code py file in the selenium library and define it as a keyword; finally, in the RF (Robot Framework, a keyword-driven automated testing framework based on Python) automation script, call the get_verifycode keyword to get the verification code;
[0036] The character recognition module uses the open source text recognition engine Tesseract OCR to recognize the segmented characters and symbols.
[0037] When the keyword is customized, the character recognition module recognizes the verification code through the ddddocr package.
[0038] Mathematical expression parsing module, responsible for combining recognized characters into mathematical expressions, using Python's built-in eval function or a custom parser to solve mathematical expressions;
[0039] The result verification module is responsible for submitting the solved results to the target website or application for verification;
[0040] The script integration module is responsible for integrating the automated testing solution into the automated testing script to achieve automated recognition and processing of computational verification codes.
[0041] An automated testing device based on image processing and recognition of computational verification codes, characterized in that it comprises a memory and a processor; the memory is used to store a computer program, and the processor is used to implement the above method steps when executing the computer program.
[0042] A readable storage medium, characterized in that: a computer program is stored on the readable storage medium, and the computer program implements the above method steps when executed by a processor.
[0043] The beneficial effects of the present invention are as follows: the automated testing method based on image processing to recognize computational verification codes realizes automated recognition and solution of computational verification codes, thereby improving the efficiency and accuracy of automated testing. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0045] Attached Figure 1 It is a schematic diagram of the automated testing method of the present invention based on image processing to recognize computational verification codes. DETAILED DESCRIPTION
[0046] In order to enable those skilled in the art to better understand the technical solutions in the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in combination with the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in the field without creative work should fall within the scope of protection of the present invention.
[0047] The automated testing method based on image processing and recognition of computational verification codes comprises the following steps:
[0048] Step S1: Image preprocessing
[0049] Step S1.1, image acquisition: obtaining a computational verification code image from a target website or application;
[0050] Step S1.2, grayscale processing: convert the image into a grayscale image to reduce the image complexity;
[0051] Step S1.3, binarization processing: converting the grayscale image into a black and white binary image to better segment the characters;
[0052] Step S1.4, denoising: using image processing technology to remove noise;
[0053] Step S1.5, character segmentation: separate the characters and symbols in the image to prepare for subsequent recognition;
[0054] In step S1.4, median filtering is used, and morphological operations, corrosion and / or dilation are adopted to remove noise.
[0055] The sample code for image preprocessing using Python and OpenCV is as follows:
[0056] import cv2
[0057] import numpy as np
[0058] def preprocess_image(image_path):
[0059] #Read the image
[0060] image=cv2.imread(image_path)
[0061] #Convert to grayscale image
[0062] gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
[0063] # Binarization
[0064] _,binary=cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
[0065] # Denoising (e.g. using median filtering)
[0066] denoised=cv2.medianBlur(binary,3)
[0067] #Morphological operations
[0068] kernel=np.ones((3,3),np.uint8)
[0069] processed_image=cv2.morphologyEx(denoised,cv2.MORPH_CLOSE,kernel)
[0070] return processed_image
[0071] image_path="captcha_image.png"
[0072] processed_image=preprocess_image(image_path)
[0073] cv2.imshow('Processed Image',processed_image)
[0074] cv2.waitKey(0)
[0075] cv2.destroyAllWindows()
[0076] Step S2: Character recognition
[0077] Step S2.1, using OCR technology to recognize the segmented characters and symbols;
[0078] Step S2.2, customize a get_verifycode method to obtain the verification code, and put the method in any Python source code py file in the selenium library and define it as a keyword;
[0079] Step S2.3, in the RF (Robot Framework, a keyword-driven automated testing framework based on Python) automation script, call the keyword get_verifycode to obtain the verification code;
[0080] In step S2.1, the open source text recognition engine Tesseract OCR is used to recognize the segmented characters and symbols.
[0081] In the step S2.2, when customizing keywords, the verification code is identified through the ddddocr package.
[0082] Custom keyword codes are as follows:
[0083] @keyword
[0084] def get_verifycode(self,locator):
[0085] img=self.find_element(locator)
[0086] img.screenshot('wdk.png')
[0087] #Identify the verification code through the ddddocr package
[0088] ocr=dddddocr.DdddOcr(old=True)
[0089] with open("wdk.png",'rb')as f:image=f.read()
[0090] return ocr.classification(image)
[0091] The sample code using Tesseract OCR is as follows:
[0092] import pytesseract
[0093] from PIL import Image
[0094] def recognize_characters(processed_image_path):
[0095] #Use Tesseract OCR for character recognition
[0096] image=Image.open(processed_image_path)
[0097] characters=pytesseract.image_to_string(image,config='--psm 8')
[0098] return characters
[0099] processed_image_path="processed_image.png"
[0100] characters=recognize_characters(processed_image_path)
[0101] print("Recognized characters:",characters)
[0102] Step S3: mathematical expression analysis
[0103] Step S3.1, combining mathematical expressions: combining the recognized characters into mathematical expressions;
[0104] Step S3.2, solving the expression: using Python's built-in eval function or a custom parser to solve the mathematical expression;
[0105] The sample code for parsing and solving mathematical expressions is as follows:
[0106] def parse_and_solve_expression(characters):
[0107] # Combine the recognized characters into mathematical expressions
[0108] expression=".join(characters)
[0109] # Solve mathematical expressions
[0110] try:
[0111] result = eval(expression)
[0112] return result
[0113] except Exception as e:
[0114] print("Error parsing expression:",e)
[0115] return None
[0116] result=parse_and_solve_expression(characters)
[0117] print("Calculation result:",result)
[0118] Step S4: Result verification and script integration
[0119] Step S4.1, submit the result: submit the solved result to the target website or application for verification;
[0120] Step S4.2, automated test script integration: Integrate the above steps into the automated test script to achieve automated recognition and processing of computational verification codes.
[0121] The following is an integration example that uses Selenium for automated testing:
[0122] Python
[0123] from selenium import webdriver
[0124] from selenium.webdriver.common.by import By
[0125] from selenium.webdriver.common.keys import Keys
[0126] import time
[0127] # Initialize WebDriver
[0128] driver = webdriver.Chrome()
[0129] driver.get("https: / / example.com")
[0130] #Identify the computational verification code and solve it
[0131] image_path="captcha_image.png"
[0132] processed_image=preprocess_image(image_path)
[0133] cv2.imwrite('processed_image.png',processed_image)
[0134] characters=recognize_characters('processed_image.png')
[0135] result=parse_and_solve_expression(characters)
[0136] #Enter the result in the web form and submit
[0137] captcha_input=driver.find_element(By.ID,"captcha_input_id")
[0138] captcha_input.send_keys(result)
[0139] submit_button=driver.find_element(By.ID,"submit_button_id")
[0140] submit_button.click()
[0141] #Wait for the page to load and verify the result
[0142] time.sleep(3)
[0143] #Check whether the login is successful or jump to the next page
[0144] #Close WebDriver
[0145] driver.quit()
[0146] The automatic testing system based on image processing and recognition of computational verification codes includes:
[0147] Image preprocessing module, responsible for image acquisition, grayscale processing, binarization processing, denoising and character segmentation;
[0148] Image acquisition: Obtain computational CAPTCHA images from target websites or applications;
[0149] Grayscale processing: convert the image into a grayscale image to reduce the complexity of the image;
[0150] Binarization: Convert the grayscale image into a black and white binary image to better segment the characters;
[0151] Denoising: Use image processing technology to remove noise;
[0152] Character segmentation: separate the characters and symbols in the image to prepare for subsequent recognition;
[0153] The image preprocessing module uses median filtering, morphological operations, corrosion and / or dilation to remove noise.
[0154] The character recognition module is responsible for using OCR technology to recognize the segmented characters and symbols; customize a get_verifycode method and put it in any Python source code py file in the selenium library and define it as a keyword; finally, in the RF (Robot Framework, a keyword-driven automated testing framework based on Python) automation script, call the get_verifycode keyword to get the verification code;
[0155] The character recognition module uses the open source text recognition engine Tesseract OCR to recognize the segmented characters and symbols.
[0156] When the keyword is customized, the character recognition module recognizes the verification code through the ddddocr package.
[0157] Mathematical expression parsing module, responsible for combining recognized characters into mathematical expressions, using Python's built-in eval function or a custom parser to solve mathematical expressions;
[0158] The result verification module is responsible for submitting the solved results to the target website or application for verification;
[0159] The script integration module is responsible for integrating the automated testing solution into the automated testing script to achieve automated recognition and processing of computational verification codes.
[0160] The automatic testing device for recognizing computational verification codes based on image processing comprises a memory and a processor; the memory is used to store a computer program, and the processor is used to implement the above method steps when executing the computer program.
[0161] The readable storage medium stores a computer program, and when the computer program is executed by a processor, the above method steps are implemented.
[0162] The embodiment described above is only one specific implementation of the present invention. Common changes and substitutions made by those skilled in the art within the scope of the technical solution of the present invention should be included in the protection scope of the present invention.
Claims
1. An automated testing method for recognizing computational verification codes based on image processing, characterized in that: The following steps are involved: Step S1: Image preprocessing Step S1.1, image acquisition: obtaining a computational verification code image from a target website or application; Step S1.2, grayscale processing: convert the image into a grayscale image to reduce the image complexity; Step S1.3, binarization processing: converting the grayscale image into a black and white binary image to better segment the characters; Step S1.4, denoising: using image processing technology to remove noise; Step S1.5, character segmentation: separate the characters and symbols in the image to prepare for subsequent recognition; Step S2: Character recognition Step S2.1, using OCR technology to recognize the segmented characters and symbols; Step S2.2, customize a get_verifycode method to obtain the verification code, and put the method in any Python source code py file in the selenium library and define it as a keyword; Step S2.3, in the RF automation script, call the keyword get_verifycode to get the verification code; Step S3: mathematical expression analysis Step S3.1, combining mathematical expressions: combining the recognized characters into mathematical expressions; Step S3.2, solving the expression: using Python's built-in eval function or a custom parser to solve the mathematical expression; Step S4: Result verification and script integration Step S4.1, submit the result: submit the solved result to the target website or application for verification; Step S4.2, automated test script integration: Integrate the above steps into the automated test script to achieve automated recognition and processing of computational verification codes.
2. The automated testing method for the verification code based on image processing recognition calculation according to claim 1 is characterized in that: In step S1.4, median filtering is used, and morphological operations, corrosion and / or dilation are adopted to remove noise.
3. The automated testing method for the verification code based on image processing recognition calculation according to claim 1 is characterized in that: In step S2.1, the open source text recognition engine Tesseract OCR is used to recognize the segmented characters and symbols.
4. The automated testing method for the verification code based on image processing recognition calculation according to claim 1, characterized in that: In the step S2.2, when customizing keywords, the verification code is identified through the ddddocr package.
5. An automated testing system for recognizing computational verification codes based on image processing, characterized in that: include: Image preprocessing module, responsible for image acquisition, grayscale processing, binarization processing, denoising and character segmentation; Image acquisition: Obtain computational CAPTCHA images from target websites or applications; Grayscale processing: convert the image into a grayscale image to reduce the complexity of the image; Binarization: Convert the grayscale image into a black and white binary image to better segment the characters; Denoising: Use image processing technology to remove noise; Character segmentation: separate the characters and symbols in the image to prepare for subsequent recognition; The character recognition module is responsible for using OCR technology to recognize the segmented characters and symbols; customize a get_verifycode method to obtain the verification code, and put the method in any Python source code py file in the selenium library and define it as a keyword; finally, in the RF automation script, call the get_verifycode keyword to obtain the verification code; Mathematical expression parsing module, responsible for combining recognized characters into mathematical expressions, using Python's built-in eval function or a custom parser to solve mathematical expressions; The result verification module is responsible for submitting the solved results to the target website or application for verification; The script integration module is responsible for integrating the automated testing solution into the automated testing script to achieve automated recognition and processing of computational verification codes.
6. The automated testing system based on image processing and recognition of computational verification codes according to claim 5, characterized in that: The image preprocessing module uses median filtering, morphological operations, corrosion and / or dilation to remove noise.
7. The automated testing system for the image processing-based recognition and computational verification code according to claim 5, characterized in that: The character recognition module uses the open source text recognition engine Tesseract OCR to recognize the segmented characters and symbols.
8. The automated testing system for the verification code based on image processing and recognition according to claim 5, characterized in that: When the keyword is customized, the character recognition module recognizes the verification code through the ddddocr package.
9. An automated testing device based on image processing and recognition of computational verification codes, characterized in that: The method comprises a memory and a processor; the memory is used to store a computer program, and the processor is used to implement the method steps as claimed in any one of claims 1 to 4 when executing the computer program.
10. A readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps according to any one of claims 1 to 4 are implemented.
