imgbin = getBinaryImage(getGrayscaleImage(img)) imgbin = 255 - dilate(erode(imgbin, 1), 1) contours = getImageContours(imgbin) contourSets = geom.mergeContours(contours, mergCrit) imObjs = getBinaryImageObjectsFromContourSets(imgbin, contourSets) # ------------------------------ angle, center = geom.getBinaryImgObjectsAngleAndCenter(imObjs) imgbin = getRotatedImage(imgbin, angle, center) imgbin = getBinaryImage(imgbin) # ------------------------------ contours = getImageContours(imgbin) contourSets = geom.mergeContours(contours, mergCrit) imObjs = getBinaryImageObjectsFromContourSets(imgbin, contourSets) imObjs = sorted(imObjs, key=lambda o: o[0][0]) displayImageGrid(([imo[1] for imo in imObjs]), 1, plotImage) plt.show() rowImObjs = imObjs distances = np.array(geom.getConsecutiveXDistancesBetweenRects(geom.imObjToRects(rowImObjs))) kmeans = geom.getKMeans(2, distances.reshape(len(distances), 1)) classificationInput = [nn.transformImageForAnn(obj[1]) for obj in rowImObjs] outputIndices = nn.classify(ann, classificationInput) print nn.getStringOutputWithSpaces(outputIndices, kmeans.labels_, alphabet)
kmeans = geom.getKMeans(2, imObjsY.reshape(len(imObjsY), 1)) rows = geom.partition(imObjs, kmeans.labels_, 2) rowsWithY = sorted(zip(rows, kmeans.cluster_centers_), key = lambda i: i[1]) rows = zip(*rowsWithY)[0] plt.figure() displayImageGrid([imo[1] for imo in imObjs], 5, plotImage) #------------------------------------------------- for row in rows: rowImObjs = sorted(row, key = lambda o: o[0][0]) distances = np.array(geom.getConsecutiveXDistancesBetweenRects(geom.imObjToRects(rowImObjs))) kmeans = geom.getKMeans(2, distances.reshape(len(distances), 1)) classificationInput = [nn.transformImageForAnn(obj[1]) for obj in rowImObjs] outputIndices = nn.classify(ann, classificationInput) spaces = kmeans.labels_ if kmeans.cluster_centers_[0] > kmeans.cluster_centers_[1]: spaces = [0 if v == 1 else 1 for v in spaces] print nn.getStringOutputWithSpaces(outputIndices, spaces, alphabet) plt.show()