Пример #1
0
 def test_textgrounder(self):
     grounder = TextGrounder()
     characters = "0" * len(self.segments)
     grounder.ground(self.img, self.segments, characters)
     self.assertTrue(self.img.is_grounded())
     self.assertEquals(reconstruct_chars(self.img.ground.classes),
                       characters)
Пример #2
0
 def _test_ocr(self, train_file, test_file):
     # get data from images
     ground_truth = test_file.ground.classes
     test_file.remove_ground()
     # create OCR
     segmenter = ContourSegmenter(blur_y=5, blur_x=5)
     extractor = SimpleFeatureExtractor()
     classifier = KNNClassifier()
     ocr = OCR(segmenter, extractor, classifier)
     # train and test
     ocr.train(train_file)
     chars, classes, _ = ocr.ocr(test_file, show_steps=False)
     print chars
     print reconstruct_chars(ground_truth)
     self.assertEqual(chars, reconstruct_chars(ground_truth))
     self.assertEqual(list(classes), list(ground_truth))
Пример #3
0
    def test_terminal_grounder(self):
        terminal = TerminalGrounder()
        characters = "0" * len(self.segments)
        mock_input_gen = iter(characters)

        def mock_input(prompt):
            return next(mock_input_gen)

        with mock.patch('__builtin__.raw_input', mock_input):
            terminal.ground(self.img, self.segments)
        self.assertTrue(self.img.is_grounded())
        self.assertEquals(reconstruct_chars(self.img.ground.classes),
                          "0" * len(self.segments))
Пример #4
0
    def test_usergrounder(self):
        ESC_KEY = 27
        ZERO_KEY = 48
        keys = [ZERO_KEY] * len(self.segments) + [ESC_KEY]
        mock_generator = iter(keys)

        def mock_input(*args):
            return next(mock_generator)

        grounder = UserGrounder()
        with mock.patch('cv2.waitKey', mock_input):
            with mock.patch('cv2.imshow'):
                grounder.ground(self.img, self.segments)
        self.assertTrue(self.img.is_grounded)
        self.assertEqual(reconstruct_chars(self.img.ground.classes), "0" * len(self.segments))
Пример #5
0
 def test_ocr_digits(self):
     # get data from images
     img1 = ImageFile('digits1')
     img2 = ImageFile('digits2')
     ground_truth = img2.ground.classes
     img2.remove_ground()
     # create OCR
     segmenter = ContourSegmenter()
     extractor = SimpleFeatureExtractor()
     classifier = KNNClassifier()
     ocr = OCR(segmenter, extractor, classifier)
     # train and test
     ocr.train(img1)
     chars, classes, _ = ocr.ocr(img2, show_steps=False)
     self.assertEqual(list(classes), list(ground_truth))
     self.assertEqual(chars, reconstruct_chars(ground_truth))
Пример #6
0
from feature_extraction import SimpleFeatureExtractor
from classification import KNNClassifier
from ocr import OCR, accuracy, show_differences, reconstruct_chars

segmenter=  ContourSegmenter( blur_y=5, blur_x=5, block_size=11, c=10)
extractor=  SimpleFeatureExtractor( feature_size=10, stretch=False )
classifier= KNNClassifier()
ocr= OCR( segmenter, extractor, classifier )

ocr.train( ImageFile('digits1') )

test_image= ImageFile('digits2')
test_classes, test_segments= ocr.ocr( test_image, show_steps=True )

print "accuracy:", accuracy( test_image.ground.classes, test_classes )
print "OCRed text:\n", reconstruct_chars( test_classes )
show_differences( test_image.image, test_segments, test_image.ground.classes, test_classes)

########NEW FILE########
__FILENAME__ = feature_extraction
import numpy
import cv2
from segmentation import region_from_segment
from opencv_utils import background_color

FEATURE_DATATYPE=   numpy.float32
#FEATURE_SIZE is defined on the specific feature extractor instance
FEATURE_DIRECTION=  1 #horizontal - a COLUMN feature vector
FEATURES_DIRECTION= 0 # vertical - ROWS of feature vectors

class FeatureExtractor( object ):
Пример #7
0
from files import ImageFile
from segmentation import ContourSegmenter, draw_segments
from feature_extraction import SimpleFeatureExtractor
from classification import KNNClassifier
from ocr import OCR, accuracy, show_differences, reconstruct_chars

segmenter = ContourSegmenter(blur_y=5, blur_x=5, block_size=11, c=10)
extractor = SimpleFeatureExtractor(feature_size=10, stretch=False)
classifier = KNNClassifier()
ocr = OCR(segmenter, extractor, classifier)

ocr.train(ImageFile('digits1'))

test_image = ImageFile('digits2')
test_classes, test_segments = ocr.ocr(test_image, show_steps=True)

print "accuracy:", accuracy(test_image.ground.classes, test_classes)
print "OCRed text:\n", reconstruct_chars(test_classes)
show_differences(test_image.image, test_segments, test_image.ground.classes, test_classes)