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))
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))
return result_classes ########NEW FILE######## __FILENAME__ = example 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) ########NEW FILE######## __FILENAME__ = feature_extraction import numpy import cv2 from segmentation import region_from_segment from opencv_utils import background_color
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)
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'), ImageFile('alpha')) test_image = ImageFile('alpha') 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)
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): os.remove(dummy_name) if args.file != None and len(args.file) > 0: for file_to_classify in args.file: img = find_image_file(file_to_classify) if img: test_images.append(img) elif args.dir != None: test_images = get_image_filenames(args.dir) else: raise Exception("Need --dir [directory] or [--file <image> ..]")
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): os.remove(dummy_name) if args.file != None and len(args.file) > 0: for file_to_classify in args.file: img = find_image_file(file_to_classify) if img: test_images.append(img) elif args.dir != None: test_images = get_image_filenames(args.dir) else: raise Exception("Need --dir [directory] or [--file <image> ..]")