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))
__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 FEATURE_DATATYPE= numpy.float32 #FEATURE_SIZE is defined on the specific feature extractor instance FEATURE_DIRECTION= 1 #horizontal - a COLUMN feature vector
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)
if use_tesseract: api = tesseract.TessBaseAPI() api.Init(tesslangpath, "eng", tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_SINGLE_CHAR) api.SetVariable("classify_enable_learning", "0") api.SetVariable("classify_enable_adaptive_matcher", "0") image_dict = OrderedDict() segment_text_list = [] cluster_pattern_list = [] # classify for fname in test_images: test_image = ImageFile(fname) test_classes, test_segments = ocr.ocr(test_image, show_steps=verbose) if use_tesseract: tesseract_image = tesseract.pixRead(fname) tesseract_classes = [] cluster_list = [] for segment in test_segments: cluster_segments = prim.get_cluster(segment, test_segments) if len(cluster_segments) == 10: add = True for list_cluster in cluster_list: add = add and not tesseract_utils.is_cluster_match( cluster_segments, list_cluster) if not add: break if add: cluster_list.append(cluster_segments)
if use_tesseract: api = tesseract.TessBaseAPI() api.Init(tesslangpath, "eng", tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_SINGLE_CHAR) api.SetVariable("classify_enable_learning", "0") api.SetVariable("classify_enable_adaptive_matcher", "0") image_dict = OrderedDict() segment_text_list = [] cluster_pattern_list = [] # classify for fname in test_images: test_image = ImageFile(fname) test_classes, test_segments = ocr.ocr(test_image, show_steps=verbose) if use_tesseract: tesseract_image = tesseract.pixRead(fname) tesseract_classes = [] cluster_list = [] for segment in test_segments: cluster_segments = prim.get_cluster(segment, test_segments) if len(cluster_segments) == 10: add = True for list_cluster in cluster_list: add = add and not tesseract_utils.is_cluster_match(cluster_segments, list_cluster) if not add: break if add: cluster_list.append(cluster_segments) pattern = tesseract_utils.get_pattern(cluster_segments)
# Assume all current candidates are equally likely s = sum(c[i - 1] for c in self.candidates) n = len(self.candidates) return int(round(float(s) / n)) def __self__(self): return "<DeckReconstructor, n=%d, avgdeck={1:%d, 2:%d, 3:%d}>" % (len( self.candidates), self[1], self[2], self[3]) def __repr__(self): return "<DeckReconstructor, candidates=%s>" % self.candidates if __name__ == '__main__': # simple test sequence import sys, os dirname, startfn = sys.argv[1:] deck = None from ocr import OCR ocr = OCR("LGE Nexus 5") imglist = sorted([fn for fn in os.listdir(dirname) if fn >= startfn]) for fn in imglist: print(fn) board, tileset = ocr.ocr(os.path.join(dirname, fn)) if deck is None: deck = DeckReconstructor(board) deck.update(tileset[0]) print(deck)
elif len(self.candidates) == 1: return self.candidates[0][i-1] # Assume all current candidates are equally likely s = sum(c[i-1] for c in self.candidates) n = len(self.candidates) return int(round(float(s)/n)) def __self__(self): return "<DeckReconstructor, n=%d, avgdeck={1:%d, 2:%d, 3:%d}>" % (len(self.candidates), self[1], self[2], self[3]) def __repr__(self): return "<DeckReconstructor, candidates=%s>" % self.candidates if __name__ == '__main__': # simple test sequence import sys, os dirname, startfn = sys.argv[1:] deck = None from ocr import OCR ocr = OCR("LGE Nexus 5") imglist = sorted([fn for fn in os.listdir(dirname) if fn >= startfn]) for fn in imglist: print fn board, tileset = ocr.ocr(os.path.join(dirname, fn)) if deck is None: deck = DeckReconstructor(board) deck.update(tileset[0]) print deck