-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnetload.lua
More file actions
90 lines (83 loc) · 1.8 KB
/
netload.lua
File metadata and controls
90 lines (83 loc) · 1.8 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
local captcha = require("captcha")
local knn = captcha.solver("knn", "netload")
local function filter_regions_of_interest(r)
local f = {}
for _, a in ipairs(r) do
if #f == 4 then
table.sort(f, function(x, y) return x.size < y.size end)
if a.size > f[1].size then
table.remove(f, 1)
table.insert(f, a)
end
elseif #f > 0 then
local add = true
for i, b in ipairs(f) do
if a:intersects(b) then
if a.size > b.size then
table.remove(f, i)
else
add = false
end
break
elseif math.abs(a.width - b.width) < 4 and math.abs(a.x - b.x) < 4 then
a:concat(b)
table.remove(f, i)
break
end
end
if add then
table.insert(f, a)
end
else
table.insert(f, a)
end
end
table.sort(f, function(x, y) return x.x < y.x end)
return f
end
local function extract_feature_vector(r)
local f = {}
r:resize(12, 18)
for i = 1, r.size do
table.insert(f, r.data[i])
end
return f
end
local function solve(b)
local img, msg = captcha.load(b)
if not img then
return nil, msg
end
img:grayscale()
img:blur(3, 2)
img:threshold(41, 24)
local roi = filter_regions_of_interest(img:regions())
if #roi < 4 then
return nil, "failed to extract characters"
end
local samples = {}
for _, r in ipairs(roi) do
table.insert(samples, extract_feature_vector(img:crop(r)))
end
for _, r in ipairs(roi) do
img:rectangle(r, 255, 0, 0)
end
img:show("captcha")
img:input()
img:hide()
return knn:solve(samples)
end
local solutions = captcha.database("test").response:load()
local file = io.open("test/" .. arg[1], "rb")
local buf = file:read("*a")
file:close()
local s, e = solve(buf)
if s then
io.write("SOLUTION: " .. s)
if s ~= solutions[arg[1]] then
io.write(" - WRONG (" .. solutions[arg[1]] .. ")")
end
print()
else
print("ERROR: " .. e)
end