-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathprocessing.py
72 lines (53 loc) · 1.96 KB
/
processing.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
import cv2
import numpy as np
def remove_lines(image):
gray = cv2.bitwise_not(image)
# Remove horizontal
v_image = gray.copy()
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,2))
v_image_eroded = cv2.erode(v_image, vertical_kernel)
v_image_dilated = cv2.dilate(v_image_eroded, vertical_kernel)
return v_image_dilated
def checkBlackCol(gray, col):
for p in range(gray.shape[0]):
if gray[p][col] > 50:
return False
return True
def checkBlackRow(gray, row):
for p in range(gray.shape[1]):
if gray[row][p] > 50:
return False
return True
def extract_basic_image(path):
image = cv2.imread(path, 0)
elements = remove_lines(image)
return elements
def processImage(filename):
img = cv2.imread('BBB20/captchas/' + filename, 0)
imgBorder = cv2.copyMakeBorder(img, 8, 8, 8, 8, cv2.BORDER_REPLICATE)
cv2.imwrite('BBB20/processedCaptchas/' + filename, imgBorder)
def findInCaptcha(filename):
processImage(filename)
elementFile = 'BBB20/elementsCaptcha/' + filename
captchaFile = 'BBB20/processedCaptchas/' + filename
template = cv2.imread(elementFile,0)
img = cv2.imread(captchaFile,0)
# print(template)
if template is None:
cv2.imwrite('BBB20/elementsCaptcha/' + filename, img)
return []
else:
# TODO: find out if we should just load the image from the dataset or not
# remove the lines to make the detection easier
template = extract_basic_image(elementFile)
img = extract_basic_image(captchaFile)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img,template,cv2.TM_SQDIFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img,top_left, bottom_right, 0, 2)
# print("salvando")
found = img[top_left[1]:top_left[1]+h, top_left[0]:top_left[0]+w]
cv2.imwrite('BBB20/matchCaptcha/' + filename, found)
return [top_left[0] + w/2, top_left[1] + h/2]