Press "Enter" to skip to content

TensorFlow验证码解算器最后部分

本站内容均来自兴趣收集,如不慎侵害的您的相关权益,请留言告知,我们将尽快删除.谢谢.

您可以从此链接下载本教程代码。 this link

 

在本教程中,我们继续学习第3部分中的代码,因此如果您没有看过该部分,我建议您这样做。在本教程中,我们只编写代码来从检测到的图像中获得正确的验证码结果。在本教程中,我们将从验证码中获取所有符号,检查字符顺序、检测准确性和重叠情况。我们使用所有这些组件来编写最终的开箱即用验证码解算器。

 

文本版教程不会像视频教程那样详细。如果您对代码中的每一行都有什幺作用以及我们为什幺需要这一行感兴趣,那幺您应该看看我的YouTube教程。在编写这段代码时,我试图解释每一行代码。在本文本版教程中,我尝试向代码添加一些注释,以帮助您理解该代码部分的功能。

 

下面是最后的代码,您可以复制和使用。但是要使用它,您需要安装TensorFlow和所有必要的库。此外,您还需要“CAPTCHA_FRESTED_INSERENCE_Graph.”这是我训练过的CAPTCHA检测模型和“CAPTCHA_labelmap.pbtxt”。当您准备好所有文件后,您应该可以使用此检测模型。下载我的代码的链接在上面。它甚至包含一些图像示例,您可以测试它是否适用于您。

 

以下是检测码:

 

# Welcome to CAPTCHA break tutorial !
# Imports
import cv2
import numpy as np
import os
import sys
# run on CPU, to run on GPU comment this line or write '0'
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import tensorflow as tf
from distutils.version import StrictVersion
from collections import defaultdict
# title of our window
title = "CAPTCHA"
# Env setup
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
# Model preparation 
PATH_TO_FROZEN_GRAPH = 'CAPTCHA_frozen__graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'CAPTCHA_labelmap.pbtxt'
NUM_CLASSES = 37
# Load a (frozen) Tensorflow model into memory.
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')
# Detection
def Captcha_detection(image, average_distance_error=3):
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Open image
            image_np = cv2.imread(image)
            # Resize image if needed
            image_np = cv2.resize(image_np, (0,0), fx=3, fy=3) 
            # To get real color we do this:
            image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=0)
            # Actual detection.
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            scores = detection_graph.get_tensor_by_name('detection_scores:0')
            classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')
            # Visualization of the results of a detection.
            (boxes, scores, classes, num_detections) = sess.run(
              [boxes, scores, classes, num_detections],
              feed_dict={image_tensor: image_np_expanded})
            vis_util.visualize_boxes_and_labels_on_image_array(
              image_np,
              np.squeeze(boxes),
              np.squeeze(classes).astype(np.int32),
              np.squeeze(scores),
              category_index,
              use_normalized_coordinates=True,
              line_thickness=2)
            # Show image with detection
            #cv2.imshow(title, cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
            # Save image with detection
            cv2.imwrite("Predicted_captcha.jpg", cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
            # Bellow we do filtering stuff
            captcha_array = []
            # loop our all detection boxes
            for i,b in enumerate(boxes[0]):
                for Symbol in range(37):
                    if classes[0][i] == Symbol: # check if detected class equal to our symbols
                        if scores[0][i] >= 0.65: # do something only if detected score more han 0.65
                                            # x-left        # x-right
                            mid_x = (boxes[0][i][1]+boxes[0][i][3])/2 # find x coordinates center of letter
                            # to captcha_array array save detected Symbol, middle X coordinates and detection percentage
                            captcha_array.append([category_index[Symbol].get('name'), mid_x, scores[0][i]])
            # rearange array acording to X coordinates datected
            for number in range(20):
                for captcha_number in range(len(captcha_array)-1):
                    if captcha_array[captcha_number][1] > captcha_array[captcha_number+1][1]:
                        temporary_captcha = captcha_array[captcha_number]
                        captcha_array[captcha_number] = captcha_array[captcha_number+1]
                        captcha_array[captcha_number+1] = temporary_captcha
            # Find average distance between detected symbols
            average = 0
            captcha_len = len(captcha_array)-1
            while captcha_len > 0:
                average += captcha_array[captcha_len][1]- captcha_array[captcha_len-1][1]
                captcha_len -= 1
            # Increase average distance error
            average = average/(len(captcha_array)+average_distance_error)
            
            captcha_array_filtered = list(captcha_array)
            captcha_len = len(captcha_array)-1
            while captcha_len > 0:
                # if average distance is larger than error distance
                if captcha_array[captcha_len][1]- captcha_array[captcha_len-1][1] < average:
                    # check which symbol has higher detection percentage
                    if captcha_array[captcha_len][2] > captcha_array[captcha_len-1][2]:
                        del captcha_array_filtered[captcha_len-1]
                    else:
                        del captcha_array_filtered[captcha_len]
                captcha_len -= 1
            # Get final string from filtered CAPTCHA array
            captcha_string = ""
            for captcha_letter in range(len(captcha_array_filtered)):
                captcha_string += captcha_array_filtered[captcha_letter][0]
                
            return captcha_string
To use above code, we can insert "print(Captcha_detection("10.jpg"))" line or we can call this function from another file. So I created a new python script called "main_.py" and wrote following lines to it. This way we will try to solve "10.jpg" CAPTCHA:

 

要使用上面的代码,我们可以插入“print(Captcha_Detection(”10.jpg“))”行,也可以从另一个文件调用此函数。因此,我创建了一个名为“main_.py”的新Python脚本,并向其中写入以下代码行。这样,我们将尝试解决“10.jpg”验证码:

 

from CAPTCHA_object_detection import *
print(Captcha_detection("10.jpg"))

 

我在代码中添加了一行代码,将验证码的大小增加了三倍,因为通常情况下,验证码很小。如果不调整图像大小,我们的模型将以错误的方式检测到它。

 

如果您正在尝试本教程并为您工作,请将您的经验写在我的YouTube视频下。所以我会知道这对你有没有效果。此外,如果你尝试过,但对你不起作用,你可以在我的YouTube视频中向我寻求建议。我会尽力帮助你的。

 

以下是几个验证码图像结果:

这就是本验证码解算器教程的全部内容。我得到了我想要的结果,这样你也可以做同样的事情。现在我可以用这段代码解决任何更简单的验证码问题。使用此代码,我们不能解决所有困难的验证码,因为其中几乎没有单词。但我认为通过这种方式,除了Google reCAPTCHA之外,我们可以解决互联网上使用的至少95%的验证码。感谢大家关注我,并继续学习其他教程!将来,我可能会写另一个教程,用Word组合或Google reCAPTCHA来解决Hard CAPTCHA问题,但现在,我对我所拥有的很满意。

 

最初发表于https://pylessons.com/TensorFlow-CAPTCHA-final-detection https://pylessons.com/TensorFlow-CAPTCHA-final-detection

Be First to Comment

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注