-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNN.py
More file actions
92 lines (75 loc) · 2.65 KB
/
KNN.py
File metadata and controls
92 lines (75 loc) · 2.65 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
import cv2
import csv
import numpy as np
import math
def get_captcha(filepath="test.png",demo=False):
captcha_identified = []
dataset_file = "dataset.csv"
imread = cv2.imread(filepath,0)
height,width = imread.shape
roi = imread[0:16,0:width]
imread=roi
#convert to black and white
mat,bnw = cv2.threshold(imread,127,255,cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(bnw,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#get individual datasets
for index in range(1,len(contours)):
draw = np.zeros(imread.shape,dtype="uint8")
draw.fill(255)
cv2.drawContours(draw,contours,index,(0,0,0),1)
(x,y,w,h) = cv2.boundingRect(contours[index])
cropped = imread[y:y+h,x:x+w]
mar2,bnww = cv2.threshold(cropped,127,255,cv2.THRESH_BINARY)
cropped = bnww
matrix = np.array(cropped)
column_values = np.sum(matrix,axis=1)
row_values = np.sum(matrix,axis=0)
x=0
for index in range(0,row_values.shape[0]):
x+=row_values[index]
y=0
for index in range(0,column_values.shape[0]):
y+=column_values[index]
eucs = []
#read datasets from csv
with open(dataset_file,'r') as fh:
csvreader = csv.reader(fh)
iter=0
for row in csvreader:
iter+=1
if iter<=1:
continue
row[1] = float(row[1])
row[2] = float(row[2])
#Euclidean distance Formula
q1 = (row[1]-x)*(row[1]-x)
q2 = (row[2]-y)*(row[2]-y)
euc = math.sqrt(q1+q2)
eucs.append(euc)
temp_store = eucs.copy()
temp_store.sort()
min_euc = temp_store[0]
if min_euc>x:
continue
#finding the index of euclidean distance
index = eucs.index(min_euc)+1
captcha_identified.append(index)
print("identified: "+str(index))
captcha_identified.reverse()
if demo==False:
decoded_captcha = ''.join(str(e) for e in captcha_identified)
if len(captcha_identified)>0:
print("\n")
print("DECODED CAPTCHA: ")
print(captcha_identified[:len(captcha_identified)])
return decoded_captcha
if len(captcha_identified)>0:
print("\n")
print("CAPTCHA DECODED: ")
print(captcha_identified[:len(captcha_identified)])
else:
print("CAPTCHA DECODING FAILED")
cv2.imshow("image",cv2.imread(filepath))
cv2.waitKey(0)
cv2.destroyAllWindows()
#print(get_captcha("test.png",True))