def recognise(image, addr, extras): result = "" x = image.width - 1 channels = getChannels(image) bestBounds = [] #cv.NamedWindow("pic", 1) #cv.NamedWindow("cols", 0) while len(result) < nSegs and x >= minW: x = cap.getBound(image, cap.CAP_BOUND_RIGHT, start=x) ratings = [] for w in xrange(minW, min(maxW + 1, x)): bounds = findBounds(image, x, w) subImage = cap.getSubImage(image, bounds) flags = findColors(subImage) for index, flag in enumerate(flags): if not flag: continue seg = getSegment(channels[index], image, bounds) seg = cap.flattenImage(adjustSize(seg, segSize)) guesses = ann.run(seg) charIndex = cap.argmax(guesses) ratings.append((guesses[charIndex], charIndex, index, bounds, seg)) best = max(ratings, key=itemgetter(0)) result += charset[best[1]] bestChannel = channels[best[2]] cv.SetImageROI(bestChannel, best[3]) cv.Set(bestChannel, 96, bestChannel) cv.ResetImageROI(bestChannel) bestBounds.append(best[3]) bestW = best[3][2] x -= bestW #print ann.run(best[4]) cap.processExtras([cap.drawComponents(image, bestBounds)], addr, extras, cap.CAP_STAGE_RECOGNISE) return result[::-1]
def doInpaint(image, mask): radius = 2 result = doCopyMakeBorder(image, radius, 255) temp = doCopyMakeBorder(mask, radius, 0) cv.Inpaint(result, temp, result, radius, cv.CV_INPAINT_NS) result = cap.getSubImage(result, (radius, radius, image.width, image.height)) return result
def recognise(image, addr, extras): result = "" x = image.width - 1 channels = getChannels(image) bestBounds = [] #cv.NamedWindow("pic", 1) #cv.NamedWindow("cols", 0) while len(result) < nSegs and x >= minW: x = cap.getBound(image, cap.CAP_BOUND_RIGHT, start=x) ratings = [] for w in xrange(minW, min(maxW + 1, x)): bounds = findBounds(image, x, w) subImage = cap.getSubImage(image, bounds) flags = findColors(subImage) for index, flag in enumerate(flags): if not flag: continue seg = getSegment(channels[index], image, bounds) seg = cap.flattenImage(adjustSize(seg, segSize)) guesses = ann.run(seg) charIndex = cap.argmax(guesses) ratings.append( (guesses[charIndex], charIndex, index, bounds, seg)) best = max(ratings, key=itemgetter(0)) result += charset[best[1]] bestChannel = channels[best[2]] cv.SetImageROI(bestChannel, best[3]) cv.Set(bestChannel, 96, bestChannel) cv.ResetImageROI(bestChannel) bestBounds.append(best[3]) bestW = best[3][2] x -= bestW #print ann.run(best[4]) cap.processExtras([cap.drawComponents(image, bestBounds)], addr, extras, cap.CAP_STAGE_RECOGNISE) return result[::-1]
def getSegment(channel, image, rect, bgcolor=96): seg = cap.getSubImage(channel, rect) original = cv.GetSubRect (image, rect) for x in xrange(original.width): for y in xrange(original.height): if original[y, x] != 0 and seg[y, x] != 255: seg[y, x] = bgcolor return seg
def deRotate(seg): box_vtx = cap.minAreaRectImage(seg) pts = sorted(box_vtx, key=lambda vtx: vtx[1], reverse=True)[:2] slope = findLineSlope(*pts) if slope < 20.0: seg = doRotate(seg, -slope, fillval=0, resize=True) seg = cap.getSubImage(seg, cap.findNonBlackRect(seg, thresh=1)) return seg
def getSegment(channel, image, rect, bgcolor=96): seg = cap.getSubImage(channel, rect) original = cv.GetSubRect(image, rect) for x in xrange(original.width): for y in xrange(original.height): if original[y, x] != 0 and seg[y, x] != 255: seg[y, x] = bgcolor return seg
def addBackground(comp, image, bgcolor=96): seg = comp[3] rect = comp[2] original = cap.getSubImage(image, rect) for x in xrange(original.width): for y in xrange(original.height): if original[y, x] != 255 and seg[y, x] != 255: seg[y, x] = bgcolor return comp
def splitRecogniseOne(image, parts, shiftRadius): result = "" splitters = linspace(0, image.width - 1, parts + 1).astype(int) for index in xrange(parts - 1, 0, -1): splitter = splitters[index] results = [] for x in xrange(splitter - shiftRadius, splitter + shiftRadius + 1): rect = findRect(image, x, splitters[index + 1]) seg = cap.getSubImage(image, rect) guess, ichar = recogniseOne(seg) results.append((guess, ichar, x, rect)) best = max(results, key=itemgetter(0)) #print best[0] splitters[index] = best[2] result += charset[best[1]] firstSeg = cap.getSubImage(image, findRect(image, 0, splitters[1])) guess, ichar = recogniseOne(firstSeg) #print guess result += charset[ichar] for s in splitters[1:-1]: cv.Line(image, (s, 0), (s, image.height - 1), 128, 1) return result[::-1]