[OpenCV+OCR] Computer Vision: Recognize the specified color text in the image verification code

[Author homepage]:Wu Qiulin
[Author introduction]: A high-quality creator in the Python field, an expert on Alibaba Cloud Blog, and an expert on Huawei Cloud Sharing. Long-term commitment to research and development in the field of Python and crawlers!
[Recommended by the author]: Friends who are interested in JS reversal can pay attention to "Reptile JS Reverse Practice" , for distribution Friends who are interested in the new crawler platform can follow "Practical Construction and Development of Distributed Crawler Platform"
There will also be verification code penetration and APP that will be continuously updated in the future. A series of articles on reverse engineering and Python fields

1. Write in front

  Today I bring you a trick on using fancy verification codes in the crawler field. This was recently shared by an expert. The verification code is as follows ( Yes See that there are certain conditions attached, such as entering characters of a specific color):

Insert image description here
Insert image description here

Before that, I went to the open source community to find some mature solutions. There are indeed many fancy processing solutions. Compared with collecting samples by myself and training a recognition model, the annotation of data samples is more time-consuming. , If you want to maintain a high accuracy, this is an ongoing thing, because you need to make your model able to adapt to the confrontation brought by updates. Or it may be the inefficiency of the coding platform. The way of sharing this time is more practical:

Insert image description here

Not much to say, the core code is actually only a few dozen lines. You can easily identify the above types of verification codes. The core idea of ​​the code is the following four steps:

1. Color space conversion
2. Generate mask according to HSV color threshold
3. Generate black and white result image
4. OCR text content recognition

In layman's terms, it means eliminating the content that has nothing to do with the color to be extracted, and finally identifying it!

The HSV color threshold reference is as follows (you can adjust it yourself):

Insert image description here

2. Read the verification code image

  First, use the pre-prepared verification code image, and then read the image through the program. The code is as follows:

def read_image(image_path):
    img = cv2.imread(image_path)
    if img is None:
        raise ValueError(f"读取图片失败: {
      
      image_path}")
    return img

3. Generate color mask

  HSV (hue, saturation, brightness) color space is a model for representing color spaces, similar to the RGB color model

We can use the cv2.inRange function to generate a binary mask based on the threshold range of the above HSV range. The area corresponding to the target color in the mask is set to white (255), and the area corresponding to other colors is set to black (0)

def apply_color_mask(hsv, lower, upper):
    return cv2.inRange(hsv, np.array(lower), np.array(upper))

4. Generate black and white result graphs

  The purpose of generating a black and white result image is to extract the content of the specified color from the original image for subsequent OCR text recognition. In the application scenario of verification code, the verification code may contain multiple colors, and we are only interested in one of them. By generating a black and white result map, we can retain the colors of interest and set other colors to white to highlight the content that needs to be identified. The code is as follows:

def generate_result_image(img, mask, result_path):
    result = np.zeros_like(img)
    result[mask == 255] = [0, 0, 0]
    result[mask != 255] = [255, 255, 255]
    cv2.imwrite(result_path, result)

This is the result of the verification code image being processed into a black and white image:

Insert image description here

5. OCR text recognition

  Finally, OCR is used to identify the black and white result images. Basically, the success rate is above 90%, which is basically enough. The identification code is as follows:

def ocr_classification(image_path):
    try:
        with open(image_path, 'rb') as f:
            img_bytes = f.read()
        ocr = ddddocr.DdddOcr(show_ad=False)
        return ocr.classification(img_bytes)
    except Exception as e:
        raise ValueError(f"OCR识别出错: {
      
      e}")

def verification_ocr(image_path, tips):
    """验证码识别主函数

    Args:
      image_path: 图像文件路径
      tips: 识别提示, 包括"红色"、"黄色"、"蓝色"、"全部"

    Returns:
      result: OCR识别结果
    """
    result_path = "1.png"
    img = read_image(image_path)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    color_ranges = {
    
    
        "红色": ([0, 50, 50], [10, 255, 255], [170, 50, 50], [180, 255, 255]),
        "黄色": ([17, 45, 50], [34, 255, 255]),
        "蓝色": ([100, 50, 50], [130, 255, 255]),
    }

    if tips in color_ranges:
        ranges = color_ranges[tips]
        mask = apply_color_mask(hsv, *ranges[:3])
        if tips == "红色":
            mask2 = apply_color_mask(hsv, *ranges[2:])
            mask = cv2.bitwise_or(mask, mask2)
        generate_result_image(img, mask, result_path)

    with open(result_path, 'rb') as f:
        img_bytes = f.read()
    ocr = ddddocr.DdddOcr(show_ad=False)
    res = ocr.classification(img_bytes)
    #输出识别内容
    print(res)

tipsThe parameter represents the color passed in, and the threshold is selected based on the color

cv2.cvtColor(img, cv2.COLOR_BGR2HSV)Convert the image from BGR color space to HSV color space. HSV (hue, saturation, value) is generally more suitable for color-based image processing

ddddocrNeedless to say, this library is very useful. It can meet the usage needs in many scenarios, the power of open source!

6. Test results

Insert image description here

If you only want to solve this type of verification code recognition, then this solution is completely sufficient! Finally, it is actually recommended that you experience the process of training your own samples to create a high-quality model.

  Well, it’s time to say goodbye to everyone again. Creation is not easy, please give me a thumbs up and leave. Your support is the driving force for my creation. I hope to bring you more high-quality articles.

Guess you like

Origin blog.csdn.net/qiulin_wu/article/details/134538042