-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAPTCHA_object_detection_String.py
147 lines (123 loc) · 6.42 KB
/
CAPTCHA_object_detection_String.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'CAPTCHA_labelmap.pbtxt'
NUM_CLASSES = 10
# 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.compat.v1.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.compat.v1.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.compat.v1.Session(graph=detection_graph) as sess:
# Open image
image_np = cv2.imread(image)
# print('Original Dimensions : ',image_np.shape)
# scale_percent = 20 # percent of original size
# width = int(image_np.shape[1] * scale_percent / 100)
# height = int(image_np.shape[0] * scale_percent / 100)
# dim = (width, height)
# # resize image
# image_np = cv2.resize(image_np, dim, interpolation = cv2.INTER_AREA)
# print('Resized Dimensions : ',image_np.shape)
# 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=1)
# 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))
#Captcha_detection("image-67.png")
# Bellow we do filtering stuff
captcha_array = []
# loop our all detection boxes
for i,b in enumerate(boxes[0]):
for Symbol in range(11):
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'), round(mid_x,3), round(float(scores[0][i]*100),2)])
## print(captcha_array)
# 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
## print('-------------')
## print(captcha_array)
# 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)):
captcha_string += captcha_array[captcha_letter][0]
return captcha_string
##print(Captcha_detection("image-68.png"))