Example #1
0
 def test(self, image, char=None):
     coms = connected_components(image)
     com = largest_component(coms)
     x = params_from_component(com, with_one=False)
     indexes, errors = self.kdtree.knn(x, self.k)
     scores = {lab: 0.0 for lab in self.label_set}
     for i in indexes[0]:
         scores[self.labels[i]] += 1.0 / self.k
     del scores['']
     return KNNClassifications(scores)
Example #2
0
File: main.py Project: d5h/pyocr
 def __init__(self, img):
     self.img = img
     self.binary_img = binary(img, invert=True)
     knn_test = KNNTest(data_path)
     self.char_test = Combiner([cont_test, mask_test, knn_test.test]).test
     self.objs = [
         ImgObj(x, self) for x in connected_components(self.binary_img) if self.good_component(x)
         ]
     self.compute_char_scores()
     self.group_words(score_threshold=0.2)
     self.spell_corrector = SpellingCorrector('/usr/share/dict/words')
Example #3
0
File: ml_test.py Project: d5h/pyocr
def test(image, char=None):
    classifications = MLClassifications()
    coms = connected_components(image)
    com = largest_component(coms)
    xs = params_from_component(com, with_one=True)
    for c, ws in models.items():
        s = dot(ws, Trainer.get_transformed_data(xs, polynomial_transform_order))
        print c, s
        classifications.add(c, s)

    return classifications
Example #4
0
    def generate_noise_points(self, output, num_samples):
        num_noise_points = 0
        for noise_img in self.noise_images:
            mat = cv.LoadImageM(noise_img, cv.CV_LOAD_IMAGE_GRAYSCALE)
            mat = binary(mat, invert=True)
            for com in connected_components(mat):
                if 10 < com.mask.rows and 5 < com.mask.cols:
                    num_noise_points += 1
                    output.write("\t%s\n" % float_list_to_tsv(params_from_component(com)))
                    if num_samples <= num_noise_points:
                        return num_samples

        return num_noise_points
Example #5
0
 def generate_data_points(self, output, num_samples):
     image_size = 64
     font_size = 48
     image = Image.new('L', (image_size, image_size), "#000000")
     mat = cv.CreateMatHeader(image_size, image_size, cv.CV_8U)
     rot = cv.CreateMat(2, 3, cv.CV_32F)
     mapmat = cv.CreateMat(image_size, image_size, cv.CV_8U)
     points_per_font = num_samples / len(self.fonts)
     for f in self.fonts:
         font = load_font(f, font_size)
         for c in self.chars:
             for _ in range(points_per_font):
                 draw_text(font, c, image)
                 cv.SetData(mat, image.tostring())
                 cx = mat.cols / 2.0 + random.gauss(0, mat.cols / 30.0)
                 cy = mat.rows / 2.0 + random.gauss(0, mat.rows / 30.0)
                 angle = random.gauss(0, 1 / 15.0)
                 cv.GetRotationMatrix2D((cx, cy), angle, 1.0, rot)
                 cv.WarpAffine(mat, mapmat, rot)
                 coms = connected_components(mapmat)
                 com = largest_component(coms)
                 output.write(c + "\t%s\n" % float_list_to_tsv(params_from_component(com)))