Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

54
LINES

< > BotCompany Repo | #97 // Font recognition test

Lua code

1  
-- load image to recognize
2  
loadSnippet("#93")() -- actually 93
3  
imgWidth, imgHeight, imgPixels = width, height, pixels
4  
5  
-- load font
6  
loadSnippet("#92")()
7  
fontWidth, fontHeight, fontPixels = width, height, pixels
8  
9  
cw, ch = 16, 16 -- size of chars
10  
11  
-- original text
12  
otext = {"!\"#$%&'()*+,-./0123456789:;<=>?@",
13  
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
14  
  "abcdefghijklmnopqrstuvwxyz{|}~  "
15  
}
16  
17  
function matchCharacter(x1, y1, xf, yf)
18  
  for y = 0, ch-1 do
19  
    for x = 0, cw-1 do
20  
      local from = xf+x + (yf+y)*fontWidth + 1
21  
      local to = x1+x + (y1+y)*imgWidth + 1
22  
      if imgPixels[to] ~= fontPixels[from] then
23  
        --print("from="..tostring(from)..", to="..tostring(to))
24  
        --print("fail at "..tostring(x).."/"..tostring(y).." "..tostring(imgPixels[to]).." "..tostring(fontPixels[from]))
25  
        return false
26  
      end
27  
    end
28  
  end
29  
  return true
30  
end
31  
32  
floor = math.floor
33  
len=floor(imgWidth/cw)
34  
35  
result = {}
36  
37  
for i=1, len do
38  
  x1 = (i-1)*cw
39  
  y1 = 0
40  
41  
  for yf = 0, fontHeight-1, ch do
42  
    for xf = 0, fontWidth-1, cw do
43  
      if matchCharacter(x1, y1, xf, yf) then
44  
        local fx = floor(xf/cw)+1
45  
        result[i] = otext[floor(yf/ch)+1]:sub(fx, fx)
46  
        --print("found character: "..tostring(result[i]))
47  
        break
48  
      end
49  
    end
50  
    if result[i] then break end
51  
  end
52  
end
53  
54  
return table.concat(result, "")

Author comment

It works! :)

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 1378 [visualize]

Snippet ID: #97
Snippet name: Font recognition test
Eternal ID of this version: #97/1
Text MD5: ed2126e8aa1ff8c650bf6b976b91b0b6
Author: stefan
Category: font recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2014-01-05 20:50:26
Source code size: 1378 bytes / 54 lines
Pitched / IR pitched: Yes / Yes
Views / Downloads: 947 / 314
Referenced in: [show references]