-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
141 lines (126 loc) · 5.76 KB
/
Copy pathmain.py
File metadata and controls
141 lines (126 loc) · 5.76 KB
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
import cv2
import numpy as np
import os
import argparse
import base64
parser = argparse.ArgumentParser()
from PIL import Image
from io import BytesIO
parser = argparse.ArgumentParser()
parser.add_argument('--image', help='Path to image file.')
parser.add_argument('--base64', help='base64 string.')
args = parser.parse_args()
# Captcha Model Files
number_weights_path = "./model/recognition.weights"
number_cfg_path = "./model/recognition.cfg"
number_classes_path = "./model/recognition.txt"
# load the captcha class labels our YOLO model was trained on
number_net = cv2.dnn.readNet(number_weights_path, number_cfg_path)
number_classes = None
with open(number_classes_path, 'r') as f:
number_classes = [line.strip() for line in f.readlines()]
# determine only the *output* layer names that we need from YOLO
def get_output_layers(net):
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
return output_layers
def process_plate(image):
scale = 0.00392
# construct a blob from the input image and then perform a forward
# pass of the YOLO object detector, giving us our bounding boxes and
# associated probabilities
blob = cv2.dnn.blobFromImage(image, scale, (200, 100), (0, 0, 0), True, crop=False)
number_net.setInput(blob)
# [,frame,no of detections,[classid,class score,conf,x,y,h,w]
outs = number_net.forward(get_output_layers(number_net))
# initialize our lists of detected bounding boxes, confidences, and
# class IDs, respectively
class_ids = []
confidences = []
boxes = []
center_X = []
conf_threshold = 0.5
nms_threshold = 0.4
# loop over each of the layer outputs
for out in outs:
# loop over each of the detections
for detection in out:
# extract the class ID and confidence (i.e., probability) of
# the current object detection
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
# filter out weak predictions by ensuring the detected
# probability is greater than the minimum probability
if confidence > 0.5:
# scale the bounding box coordinates back relative to the
# size of the image, keeping in mind that YOLO actually
# returns the center (x, y)-coordinates of the bounding
# box followed by the boxes' width and height
center_x = int(detection[0] * image.shape[1])
center_y = int(detection[1] * image.shape[0])
# use the center (x, y)-coordinates to derive the top and
# and left corner of the bounding box
w = int(detection[2] * image.shape[1])
h = int(detection[3] * image.shape[0])
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
# update our list of bounding box coordinates, confidences,
# and class IDs
boxes.append([x, y, w, h])
center_X.append(center_x)
# apply non-maxima suppression to suppress weak, overlapping bounding
# boxes
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
result = ''
valid_boxes = []
valid_classids = []
valid_centerX = []
# Sorting of box Infos
for i in indices:
i = i[0]
box = boxes[i]
x = box[0]
valid_boxes.append(box)
valid_classids.append(class_ids[i])
valid_centerX.append(x)
for i in range(0, len(valid_centerX)):
for j in range(i + 1, len(valid_centerX)):
if valid_centerX[i] > valid_centerX[j]:
temp = valid_centerX[i]
valid_centerX[i] = valid_centerX[j]
valid_centerX[j] = temp
tem = valid_classids[i]
valid_classids[i] = valid_classids[j]
valid_classids[j] = tem
for i in range(0, len(valid_classids)):
result += number_classes[valid_classids[i]]
return result
if __name__=='__main__':
if(args.image is None and args.base64 is None): # Directory Reading Part
yourpath = "./cap_dataset/"
for root, dirs, files in os.walk(yourpath, topdown=False): # File image Reading part from Directly
for name in files:
image = cv2.imread(yourpath + name)
capt_text = process_plate(image) # CaptCha Processing
cv2.imshow("Capture Result", image) # Mat Image Displaying
print(capt_text) # CaptCha Output
cv2.waitKey(0) # Delay
elif(args.image is not None):
image = cv2.imread(args.image) # File image Reading part
capt_text = process_plate(image) # CaptCha Processing
cv2.imshow("Capture Result", image) # Mat Image Displaying
print(capt_text) # CaptCha Output
cv2.waitKey(0) # Delay
else:
base64_string = args.base64
base64_string = base64_string.replace("data:image/png;base64,", "") # Base64 Reading Part
im = Image.open(BytesIO(base64.b64decode(base64_string))) # Converting Base64 string to Pillow Image
open_cv_image = np.array(im) # Converting Pillow Image to BGRA Mat Image
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGRA2BGR) # Converting BGRA Mat Image to BGR Mat Image
capt_text = process_plate(open_cv_image) # CaptCha Processing
cv2.imshow("Capture Result", open_cv_image) # Mat Image Displaying
print(capt_text) # CaptCha Output
cv2.waitKey(0) # Delay