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

54
LINES

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

Lua code

-- load image to recognize
loadSnippet("#93")() -- actually 93
imgWidth, imgHeight, imgPixels = width, height, pixels

-- load font
loadSnippet("#92")()
fontWidth, fontHeight, fontPixels = width, height, pixels

cw, ch = 16, 16 -- size of chars

-- original text
otext = {"!\"#$%&'()*+,-./0123456789:;<=>?@",
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
  "abcdefghijklmnopqrstuvwxyz{|}~  "
}

function matchCharacter(x1, y1, xf, yf)
  for y = 0, ch-1 do
    for x = 0, cw-1 do
      local from = xf+x + (yf+y)*fontWidth + 1
      local to = x1+x + (y1+y)*imgWidth + 1
      if imgPixels[to] ~= fontPixels[from] then
        --print("from="..tostring(from)..", to="..tostring(to))
        --print("fail at "..tostring(x).."/"..tostring(y).." "..tostring(imgPixels[to]).." "..tostring(fontPixels[from]))
        return false
      end
    end
  end
  return true
end

floor = math.floor
len=floor(imgWidth/cw)

result = {}

for i=1, len do
  x1 = (i-1)*cw
  y1 = 0

  for yf = 0, fontHeight-1, ch do
    for xf = 0, fontWidth-1, cw do
      if matchCharacter(x1, y1, xf, yf) then
        local fx = floor(xf/cw)+1
        result[i] = otext[floor(yf/ch)+1]:sub(fx, fx)
        --print("found character: "..tostring(result[i]))
        break
      end
    end
    if result[i] then break end
  end
end

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: 946 / 314
Referenced in: [show references]