get("#137") -- split get("#349") -- table functions get("#350") -- RecogUtil 2 function make_sig2letter() -- first, signatures from alphabet (#1000080), then additional -- signatures for slightly varying instances, e.g. from #1000082 sigs = "121-1212 1321-12121 121-12121 121-121 1321-1 12-1 12321-12121 1-212 1-1 121-121 121-212 1-1 1-2343 1-232 121-121 121-121 12321-12121 12321-1212 12321-12121 1-1 1-21 12121-56787865 1-21 12321-1 12121-212 132321-12121" letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "VWX", "Y", "Z", "X", "B"} sigs = split(sigs, " ") sig2letter = {} for i=1, #sigs do sig = sigs[i] if sig2letter[sig] == nil then sig2letter[sig] = {} end table.insert(sig2letter[sig], letters[i]) end end -- [ILT][ILT]N[UY]?RA[ILT]NCANREA[DPOJ] :) function fix(segidx, letters) local w, h = x2-x1, y2-y1 local let = valuestokeys(letters) local cropped = { width = w, height = h, getInt = function(x, y) return img.getInt(x+x1, y+y1) end } -- distinguish I and T by width if let["I"] and let["T"] then local ratio = w/h let[ratio >= 0.5 and "I" or "T"] = nil end if let["T"] and let["L"] then let[upperToLowerWidth(cropped) < 1 and "T" or "L"] = nil end if let["I"] and let["L"] then let[upperToLowerWidth(cropped) <= 0.5 and "I" or "L"] = nil end if let["U"] and let["Y"] then local middleY = math.ceil(h/2) local middleWidth = getWidth(cropped, middleY) --[[local _x2 = getRightmostPoint(cropped, middleY) local _x1 = getLeftmostPoint(cropped, middleY) print("crop: ", x1, y1, x2, y2) print("middleWidth: "..middleWidth..", w: "..w..", middleY: "..middleY..", _x2: ".._x2..", _x1: ".._x1) for x=0, cropped.width-1 do print(cropped.getInt(x, middleY)) end]] let[middleWidth/w <= 0.5 and "U" or "Y"] = nil end if let["D"] and let["P"] then local upper = upperMaxWidth(cropped) local lower = lowerMaxWidth(cropped) print("upper", upper, "lower", lower) let[upper/lower >= 2 and "D" or "P"] = nil end if let["O"] and let["P"] then let[upperMaxWidth(cropped)/lowerMaxWidth(cropped) >= 2 and "O" or "P"] = nil end if let["D"] and let["O"] then --local gx, gy = getCenterOfGravity(cropped) --print("gx", gx, "w", w) --let[gx >= w*0.4 and "D" or "O"] = nil local avgLeft = getAverageLeft(cropped) print("D/O: avgLeft/w", avgLeft/w) let[avgLeft/w >= 0.05 and "D" or "O"] = nil end if (let["D"] or let["O"] or let["P"]) and let["J"] then local avgLeft = getAverageLeft(cropped, math.floor(h*0.33), math.floor(h*0.6)) --print("D/O vs J: avgLeft/w", avgLeft/w) if avgLeft/w >= 0.5 then let["D"], let["O"], let["P"] = nil, nil, nil else let["J"] = nil end end if (let["G"] or let["S"]) and let["Q"] then local yo, threshold = 8/9, 0.4 local yy = math.floor(h*yo) local avgLeft = getAverageLeft(cropped, yy, h) print("G vs Q: avgLeft/w", avgLeft/w, "h", h, "yo", yo, "yy", yy) if avgLeft/w >= threshold then let["G"], let["S"] = nil, nil else let["Q"] = nil end end if let["G"] and let["S"] then local middleY = math.floor(h/2) local left = getLeftmostPoint(cropped, middleY)/w print("G vs S: left", left) let[left >= 0.15 and "G" or "S"] = nil end letters = keystolist(let) return letters end -- works on x1, lastX2 function isSpace() if lastX2 == nil then return false end local d = (x1-lastX2)/(y2-y1) --print("space dist", d) return d >= 0.3 end make_sig2letter() result = otherresults["#345"] or error("Need result of #345") _, _, s = string.find(result, ": (.+)") if s == nil then return end parts = split(s, "|") -- could also recalc this from parts sigs = otherresults["#340"] or error("Need result of #340") sigs = split(sigs, " ") result = {} for i=1, #sigs do sig = sigs[i] letters = sig2letter[sig] part = parts[i] lastX2 = x2 x1, y1, x2, y2 = unpack(split(part, ", ")) x1, y1, x2, y2 = tonumber(x1), tonumber(y1), tonumber(x2), tonumber(y2) if isSpace() then table.insert(result, " ") end s = "" if letters == nil then print("Unknown signature: "..sig) s = "?" else if #letters > 1 then letters = fix(i, letters) end if #letters == 1 then s = letters[1] else s = "["..table.concat(letters, "").."]" end end table.insert(result, s) end return table.concat(result, "")