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()
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): os.remove(dummy_name)
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"
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()