Example #1
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))
Example #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))
Example #3
0
from files import ImageFile
from segmentation import ContourSegmenter
from feature_extraction import SimpleFeatureExtractor
from classification import KNNClassifier
from ocr import OCR, accuracy, show_differences

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_chars, test_classes, test_segments = ocr.ocr(test_image, show_steps=True)

print("accuracy:", accuracy(test_image.ground.classes, test_classes))
print("OCRed text:\n", test_chars)
 def setUp(self):
     self.img = ImageFile('digits1')
     self.img.remove_ground()
     self.assertFalse(self.img.is_grounded())
     self.segments = ContourSegmenter().process(self.img.image)
Example #5
0
boxfile = 'data/' + imgfile + '.box'
new_image = ImageFile(imgfile)

# delete the box file if it's empty
if (isfile(boxfile)):
    if (getsize(boxfile) == 0):
        remove(boxfile)

# define what to focus on and ignore in the image
stack = [
    segmentation_filters.LargeFilter(),
    segmentation_filters.SmallFilter(),
    segmentation_filters.LargeAreaFilter(),
    segmentation_filters.ContainedFilter()
]

# process image, defining useful-looking segments
segmenter = ContourSegmenter(blur_y=5,
                             blur_x=5,
                             block_size=11,
                             c=10,
                             filters=stack)
segments = segmenter.process(new_image.image)

# uncomment to watch the segmenter in action
#segmenter.display()

grounder = UserGrounder()
grounder.ground(new_image, segments)
new_image.ground.write()
Example #6
0
def best_segmenter(image):
    '''returns a segmenter instance which segments the given image well'''
    return ContourSegmenter()
Example #7
0
                                    block_size=17,
                                    c=6,
                                    max_ratio=4.0)
    extractor = SimpleFeatureExtractor(feature_size=10, stretch=False)
    classifier = KNNClassifier(k=3)
    ocr = OCR(segmenter, extractor, classifier)

    for file_to_train in args.trainfile:
        training_image = ImageFile(file_to_train)
        if not training_image.isGrounded() or force_train:
            #trainingsegmenter = ContourSegmenter(blur_y=1, blur_x=1, min_width=3, min_height=15, max_height=50, min_area=30, block_size=23, c=3) # tweaked for black font
            trainingsegmenter = ContourSegmenter(
                blur_y=1,
                blur_x=1,
                min_width=3,
                min_height=15,
                max_height=50,
                min_area=30,
                block_size=3,
                c=5,
                nearline_tolerance=10.0)  # tweaked for white font
            segments = trainingsegmenter.process(training_image.image)
            if verbose:
                trainingsegmenter.display()

            # grounder = UserGrounder()   # interactive version; lets the user review, assign ground truth data
            grounder = TextGrounder(
            )  # non-interactive ground-truth - assumes clean, ordered input
            grounder.ground(training_image, segments,
                            "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                            )  # writes out a .box file of image ground truths
Example #8
0
    verbose = args.verbose
    terse = args.terse
    force_train = args.retrain
    use_tesseract = args.tesseract
    tesslangpath = args.tesslangpath

    segmenter = MinContourSegmenter(blur_y=5, blur_x=5, min_width=5, block_size=17, c=6, max_ratio=4.0)
    extractor = SimpleFeatureExtractor(feature_size=10, stretch=False)
    classifier = KNNClassifier(k=3 )
    ocr = OCR(segmenter, extractor, classifier)

    for file_to_train in args.trainfile:
        training_image = ImageFile(file_to_train)
        if not training_image.isGrounded() or force_train:
            #trainingsegmenter = ContourSegmenter(blur_y=1, blur_x=1, min_width=3, min_height=15, max_height=50, min_area=30, block_size=23, c=3) # tweaked for black font
            trainingsegmenter = ContourSegmenter(blur_y=1, blur_x=1, min_width=3, min_height=15, max_height=50, min_area=30, block_size=3 , c=5, nearline_tolerance=10.0   ) # tweaked for white font
            segments = trainingsegmenter.process(training_image.image)
            if verbose:
                trainingsegmenter.display()

            # grounder = UserGrounder()   # interactive version; lets the user review, assign ground truth data
            grounder = TextGrounder()   # non-interactive ground-truth - assumes clean, ordered input
            grounder.ground(training_image, segments, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")  # writes out a .box file of image ground truths


        ocr.train(training_image)

    # Classify given image(s) using training data
    test_images = []
    dummy_name = args.dir + "\\dummy.jpg"
    if os.path.isfile(dummy_name):
Example #9
0
from files import ImageFile
from grounding import UserGrounder
from segmentation import ContourSegmenter, draw_segments

segmenter=  ContourSegmenter( blur_y=5, blur_x=5, block_size=11, c=10)
new_image= ImageFile('digits1')
segments= segmenter.process(new_image.image)

grounder= UserGrounder()
grounder.ground(new_image, segments);
new_image.ground.write()