def create(dataset, work_dir): # Find all the pages in the dataset img_dir = Path(Path.cwd().ancestor(1), 'data/hwr_data/pages', dataset) ann_dir = Path(Path.cwd().ancestor(1), 'data/charannotations') images = img_dir.listdir('*.jpg') annotations = ann_dir.listdir(dataset + '*.words') files = merge(images, annotations) # Create character segmentations stats = {} for f in files: # Preprocess logging.info("Preprocessing %s", str(f[0])) pagesPathFolder = Path(work_dir, 'pages') pagesPathFolder.mkdir() pagePath = Path(pagesPathFolder, f[0].stem + '.ppm') img = cv2.imread(f[0], cv2.IMREAD_GRAYSCALE) img = preprocess(img) cv2.imwrite(pagePath, img) # Segment segmentPathFolder = Path(work_dir, 'segments') segmentPathFolder.mkdir() e = ET.parse(f[1]).getroot() logging.info("Segmenting %s", str(f[0])) segment(img, e, segmentPathFolder, stats) print_statistics(stats, dataset)
def main(state="external", img=None, words_file_name=None, minCutWindow=20, maxCutWindow=80, globalLexicon=True): # Directories sentenceDir = Path('tmp/sentences/') wordDir = Path('tmp/words/') # Logging logging.basicConfig(format='%(asctime)s %(levelname)-8s: %(message)s', level=logging.DEBUG, datefmt='%m/%d/%Y %I:%M:%S %p') # Check commandline parameters # if state is external if state == "external": if len(sys.argv) != 4: print "Usage: python %s image.ppm input.words out.words" % sys.argv[ 0] sys.exit(1) # Read input files img = cv2.imread(sys.argv[1], 0) words_file_name = sys.argv[2] # Preprocess image img = prep.preprocess(img) # Get the sorted unique list of class labels featureDir = 'tmp/features/' trainDir = featureDir + 'train/' trainLabels = np.load(trainDir + 'labels.npy') classes = sorted(set(trainLabels)) # Get lexicon information lexicon = create_lexicon.create() lexicon_means_stds = create_lexicon_means_stds.create() if globalLexicon == True: stateProbs = create_probTables.create_stateProbs(lexicon) transProbs = create_probTables.create_transProbs(lexicon) # Recognition accuracy names correct = 0 false = 0 inLex = 0 avgReduction = 0 # Recognize the words xml = ET.parse(words_file_name).getroot() recog = Recognizer(sentenceDir, wordDir, xml, img) for word, word_img in recog.next_word(): required_word = word.get('text') logging.info("Word:\t\t%s" % required_word) cuts = recog.find_cuts(word_img) if cuts is not None: reduced_lexicon, reduction = reduce_lexicon( cuts, word_img, lexicon, lexicon_means_stds) avgReduction += reduction if globalLexicon == False: stateProbs = create_probTables.create_stateProbs( reduced_lexicon) transProbs = create_probTables.create_transProbs( reduced_lexicon) cuts.insert(0, 0) # Also create a window at the start of the word estimate = recog.recursiveRecognize(word_img, cuts, reduced_lexicon, stateProbs, transProbs, classes, minCutWindow, maxCutWindow) logging.info("Estimate:\t%s" % estimate) if required_word in reduced_lexicon: inLex += 1 if required_word == estimate: correct += 1 else: false += 1 accuracy = float(correct) / (correct + false) * 100 logging.info("Correct: %s\tFalse: %s\tAccuracy: %s\n" % (correct, false, accuracy)) else: continue accuracy = float(correct) / (correct + false) * 100 totalInLex = float(inLex) / (correct + false) * 100 avgReduction = avgReduction / float(correct + false) if state == "external": logging.info( "Complete results for file: %s, Correct: %s\tFalse: %s \tAccuracy: %s" % (sys.argv[1], correct, false, accuracy)) ET.ElementTree(recog.words).write(sys.argv[3]) else: return (correct, false) logging.info( "Complete results for file: Correct: %s\tFalse: %s \tAccuracy: %s" % (correct, false, accuracy))
if __name__ == '__main__': if len(sys.argv) != 3: print('Usage: python {} <image> <words>'.format(sys.argv[0])) sys.exit(1) img_file = sys.argv[1] word_file = sys.argv[2] logger.info('Loading pretrained models...') with open('tmp/svm.pickle', 'r') as f: svm = pickle.load(f) logger.info('Loading image and words file') img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE) img = prep.preprocess(img) xml = ET.parse(word_file).getroot() out_dir.mkdir() for f in out_dir.walk('*'): f.remove() logger.info('Starting to create a split file') for sentence in xml: for word in sentence: text = word.get('text') print(text) #if '@' in text or len(text) < 6: if text != 'buton': continue # Skip short words
def main( state="external", img=None, words_file_name=None, minCutWindow = 20, maxCutWindow = 80, globalLexicon=True ): # Directories sentenceDir = Path('tmp/sentences/') wordDir = Path('tmp/words/') # Logging logging.basicConfig(format='%(asctime)s %(levelname)-8s: %(message)s', level=logging.DEBUG, datefmt='%m/%d/%Y %I:%M:%S %p') # Check commandline parameters # if state is external if state == "external": if len(sys.argv) != 4: print "Usage: python %s image.ppm input.words out.words" % sys.argv[0] sys.exit(1) # Read input files img = cv2.imread(sys.argv[1], 0) words_file_name = sys.argv[2] # Preprocess image img = prep.preprocess(img) # Get the sorted unique list of class labels featureDir = 'tmp/features/' trainDir = featureDir + 'train/' trainLabels = np.load(trainDir + 'labels.npy') classes = sorted(set(trainLabels)) # Get lexicon information lexicon = create_lexicon.create() lexicon_means_stds = create_lexicon_means_stds.create() if globalLexicon == True: stateProbs = create_probTables.create_stateProbs(lexicon) transProbs = create_probTables.create_transProbs(lexicon) # Recognition accuracy names correct = 0 false = 0 inLex = 0 avgReduction = 0 # Recognize the words xml = ET.parse(words_file_name).getroot() recog = Recognizer(sentenceDir, wordDir, xml, img) for word, word_img in recog.next_word(): required_word = word.get('text') logging.info("Word:\t\t%s" % required_word) cuts = recog.find_cuts(word_img) if cuts is not None: reduced_lexicon, reduction = reduce_lexicon(cuts, word_img, lexicon, lexicon_means_stds) avgReduction += reduction if globalLexicon == False: stateProbs = create_probTables.create_stateProbs(reduced_lexicon) transProbs = create_probTables.create_transProbs(reduced_lexicon) cuts.insert(0, 0) # Also create a window at the start of the word estimate = recog.recursiveRecognize( word_img, cuts, reduced_lexicon, stateProbs, transProbs, classes, minCutWindow, maxCutWindow ) logging.info("Estimate:\t%s" % estimate) if required_word in reduced_lexicon: inLex += 1 if required_word == estimate: correct += 1 else: false += 1 accuracy = float(correct)/(correct+false) * 100 logging.info("Correct: %s\tFalse: %s\tAccuracy: %s\n" % (correct, false, accuracy) ) else: continue accuracy = float(correct)/(correct+false) * 100 totalInLex = float(inLex)/(correct+false) * 100 avgReduction = avgReduction/float(correct+false) if state == "external": logging.info("Complete results for file: %s, Correct: %s\tFalse: %s \tAccuracy: %s" % (sys.argv[1], correct, false, accuracy) ) ET.ElementTree(recog.words).write(sys.argv[3]) else: return (correct, false) logging.info("Complete results for file: Correct: %s\tFalse: %s \tAccuracy: %s" % (correct, false, accuracy) )