def test_init_method_different_parameters(self): test_dir = File("../../character_examples").getPath() nr_of_training_examples = 90 nr_of_test_examples = 10 for size_classification_factor in drange(0.7, 6.0, 0.3): print str(size_classification_factor) + ' &', for nr_of_segs in range(4, 13): #print(nr_of_segs) test_scores = [] for test_nr in range(10): #print(test_nr) extracor = SimpleImageFeatureExtractor( nr_of_divisions=nr_of_segs, size_classification_factor=size_classification_factor) training_examples, test_examples = extracor.extract_training_and_test_examples( test_dir, nr_of_training_examples, nr_of_test_examples) classifier = CharacterClassifier( training_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=False, initialisation_method=SpecializedHMM.InitMethod. count_based) test_scores.append(classifier.test(test_examples)) score = sum(test_scores) / len(test_scores) print ' $' + str(score) + '$ ', if nr_of_segs == 12: print '\\\\' else: print '&',
def create_character_classification_count_matrix(): ''' This function do the following: 1. Creates a character classifier with 90 training examples 2. Runs a test with 10 test examples for every character. The results are put in a matrix M so element M[i][j] contains the number of test examples for the character with index i that are classified to be the character with index j. ''' extracor = SimpleImageFeatureExtractor(nr_of_divisions=11, size_classification_factor=3.4) training_examples, test_examples = extracor.extract_training_and_test_examples(File(File(File(".."),".."),"character_examples").getCanonicalPath(), nr_of_training_examples=90, nr_of_test_examples=10) classifier = CharacterClassifier(training_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=False, initialisation_method=SpecializedHMM.InitMethod.count_based) alphabet = get_example_alphabet() classification_count_matrix = zeros(len(alphabet),len(alphabet)) for label, examples in test_examples: character = label.lower() character_index = alphabet.index(character) for example in examples: classified_character = classifier.classify_character_string(example).lower() classified_character_index = alphabet.index(classified_character) count = classification_count_matrix[character_index][classified_character_index] classification_count_matrix[character_index][classified_character_index] = count + 1 return classification_count_matrix
def test_init_method_different_parameters(self): test_dir = File("../../character_examples").getPath() nr_of_training_examples = 90 nr_of_test_examples = 10 for size_classification_factor in drange(0.7, 6.0, 0.3): print str(size_classification_factor) + ' &', for nr_of_segs in range(4,13): #print(nr_of_segs) test_scores = [] for test_nr in range(10): #print(test_nr) extracor = SimpleImageFeatureExtractor(nr_of_divisions=nr_of_segs, size_classification_factor=size_classification_factor) training_examples, test_examples = extracor.extract_training_and_test_examples(test_dir, nr_of_training_examples, nr_of_test_examples) classifier = CharacterClassifier(training_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=False, initialisation_method=SpecializedHMM.InitMethod.count_based) test_scores.append(classifier.test(test_examples)) score = sum(test_scores) / len(test_scores) print ' $' + str(score) +'$ ', if nr_of_segs == 12: print '\\\\' else: print '&',
def create_character_classification_count_matrix(): ''' This function do the following: 1. Creates a character classifier with 90 training examples 2. Runs a test with 10 test examples for every character. The results are put in a matrix M so element M[i][j] contains the number of test examples for the character with index i that are classified to be the character with index j. ''' extracor = SimpleImageFeatureExtractor(nr_of_divisions=11, size_classification_factor=3.4) training_examples, test_examples = extracor.extract_training_and_test_examples( File(File(File(".."), ".."), "character_examples").getCanonicalPath(), nr_of_training_examples=90, nr_of_test_examples=10) classifier = CharacterClassifier( training_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=False, initialisation_method=SpecializedHMM.InitMethod.count_based) alphabet = get_example_alphabet() classification_count_matrix = zeros(len(alphabet), len(alphabet)) for label, examples in test_examples: character = label.lower() character_index = alphabet.index(character) for example in examples: classified_character = classifier.classify_character_string( example).lower() classified_character_index = alphabet.index(classified_character) count = classification_count_matrix[character_index][ classified_character_index] classification_count_matrix[character_index][ classified_character_index] = count + 1 return classification_count_matrix
def get_character_classifier_with_init_method(traing_examples, init_method): return CharacterClassifier(traing_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=False, initialisation_method=init_method)
def create_character_classifier(save_to_file_path): example_dir = File("../../character_examples").getPath() nr_of_training_examples = 90 nr_of_test_examples = 10 extractor = SimpleImageFeatureExtractor(nr_of_divisions=11, size_classification_factor=4.6) training_examples, test_examples = extractor.extract_training_and_test_examples(example_dir, nr_of_training_examples, nr_of_test_examples) classifier = CharacterClassifier(training_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=True, initialisation_method=SpecializedHMM.InitMethod.count_based, feature_extractor=extractor) test_result = str(classifier.test(test_examples)) print('Prediction ratio:', test_result)
def create_character_classifier(save_to_file_path): example_dir = File("../../character_examples").getPath() nr_of_training_examples = 100 nr_of_test_examples = 0 extractor = SimpleImageFeatureExtractor(nr_of_divisions=7, size_classification_factor=1.3) training_examples, test_examples = extractor.extract_training_and_test_examples(example_dir, nr_of_training_examples, nr_of_test_examples) classifier = CharacterClassifier(training_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=False, initialisation_method=SpecializedHMM.InitMethod.count_based, feature_extractor=extractor) classifier_string = classifier.to_string() file = open(save_to_file_path,'w') file.write(classifier_string) file.close()
def get_character_classifier(self): path_to_this_dir = File(str(inspect.getfile( inspect.currentframe()))).getParent() classifier_filename = ( "character_classifier_" + str(self.feature_extraction_number_of_segments) + "_segments_cf_" + str(self.feature_extraction_classification_factor).replace( '.', '_') + ".dat") character_classifier_file = open( File(path_to_this_dir, classifier_filename).getPath(), 'r') character_classifier = CharacterClassifier( from_string_string=character_classifier_file.read()) character_classifier_file.close() return character_classifier
def create_character_classifier(save_to_file_path): example_dir = File("../../character_examples").getPath() nr_of_training_examples = 100 nr_of_test_examples = 0 extractor = SimpleImageFeatureExtractor(nr_of_divisions=11, size_classification_factor=4.6) training_examples, test_examples = extractor.extract_training_and_test_examples( example_dir, nr_of_training_examples, nr_of_test_examples) classifier = CharacterClassifier( training_examples, nr_of_hmms_to_try=1, fraction_of_examples_for_test=0, train_with_examples=False, initialisation_method=SpecializedHMM.InitMethod.count_based, feature_extractor=extractor) #test_result = str(classifier.test(test_examples)) #print(test_result) classifier_string = classifier.to_string() file = open(save_to_file_path + ".dat", 'w') file.write(classifier_string) file.close()
def __init__(self): ''' Constructor ''' #Create classifiers #Character classifier path_to_this_dir = File(str(inspect.getfile( inspect.currentframe() ))).getParent() character_classifier_file = open(File(path_to_this_dir,"character_classifier.dat").getPath(),'r') self.character_classifier = CharacterClassifier(from_string_string=character_classifier_file.read()) character_classifier_file.close() #Word classifier word_classifier_file = open(File(path_to_this_dir,"word_classifier.dat").getPath(),'r') self.word_classifier= WordClassifier(from_string_string=word_classifier_file.read()) word_classifier_file.close() #Set up window self.setTitle("HandReco Writer") self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) self.setLocationRelativeTo(None) self.setLayout(BorderLayout()) #Set up menu menu_bar = JMenuBar() info_menu = JMenu("Info") def instructions(event): instr = ''' The program can just recognise capital English characters. See Info -> Available Words... for available word corrections. Good Luck with the writing!''' JOptionPane.showMessageDialog(self, instr, "Instructions", JOptionPane.INFORMATION_MESSAGE) instructions_menu_item = JMenuItem("Instructions...",actionPerformed=instructions) info_menu.add(instructions_menu_item) def word_corrections(event): words_string = "" for word in self.word_classifier.words: words_string = words_string + word.upper() + "\n" text = "The words that can be corrected are:\n\n" +words_string dialog = JOptionPane(text, JOptionPane.INFORMATION_MESSAGE) dialog_wrapper = JDialog(self,"Available Words",False) dialog_wrapper.setContentPane(dialog) dialog_wrapper.pack() dialog_wrapper.setVisible(True) word_corrections_menu_item = JMenuItem("Available Words...",actionPerformed=word_corrections) info_menu.add(word_corrections_menu_item) menu_bar.add(info_menu) self.setJMenuBar(menu_bar) #Input panel input_panel = JPanel() input_panel.setLayout(BoxLayout(input_panel, BoxLayout.X_AXIS)) input_panel.setBorder(BorderFactory.createTitledBorder("Input")) self.paint_area = PaintArea(100,100) input_panel.add(self.paint_area) input_options_panel = JPanel() input_options_panel.setLayout(BoxLayout(input_options_panel, BoxLayout.Y_AXIS)) #Write Char write_char_panel = JPanel(BorderLayout()) def write_char(event): char = self.character_classifier.classify_image(self.paint_area.image()) self.text_area.setText(self.text_area.getText() + char) self.paint_area.clear() write_char_panel.add(JButton("Write Char", actionPerformed=write_char), BorderLayout.NORTH) input_options_panel.add(write_char_panel) #Space and Correct space_and_correct_panel = JPanel(BorderLayout()) def space_and_correct(event): text = self.text_area.getText() words = text.split(" ") string = words[-1] try: word = self.word_classifier.classify(string.lower()) words[-1] = word.upper() except: JOptionPane.showMessageDialog(self, "Have you entered a character which is not in the alphabet?", "Could not Correct", JOptionPane.ERROR_MESSAGE) self.text_area.setText(text + " ") return newText = "" for w in words: newText = newText + w + " " self.text_area.setText(newText) space_and_correct_panel.add(JButton("Space and Correct", actionPerformed=space_and_correct), BorderLayout.NORTH) input_options_panel.add(space_and_correct_panel) #Space space_panel = JPanel(BorderLayout()) def space(event): self.text_area.setText(self.text_area.getText() + " ") space_panel.add(JButton("Space", actionPerformed=space), BorderLayout.NORTH) input_options_panel.add(space_panel) #Clear Input Area clear_input_area_panel = JPanel(BorderLayout()) def clear(event): self.paint_area.clear() clear_input_area_panel.add(JButton("Clear Input Area", actionPerformed=clear), BorderLayout.NORTH) input_options_panel.add(clear_input_area_panel) input_panel.add(input_options_panel) self.add(input_panel, BorderLayout.NORTH) text_area_panel = JPanel() text_area_panel.setLayout(BorderLayout()) text_area_panel.setBorder(BorderFactory.createTitledBorder("Text")) self.text_area = JTextArea() self.text_area.setLineWrap(True) #Increase font size font = self.text_area.getFont() self.text_area.setFont(Font(font.getName(), Font.BOLD, 24)) text_area_panel.add(JScrollPane(self.text_area), BorderLayout.CENTER) self.add(text_area_panel, BorderLayout.CENTER) self.pack() self.setSize(Dimension(300,300)) self.setVisible(True)
def __init__(self): ''' Constructor ''' #Create classifiers #Character classifier path_to_this_dir = File(str(inspect.getfile( inspect.currentframe()))).getParent() character_classifier_file = open( File(path_to_this_dir, "character_classifier_11_segments_4_6_cf.dat").getPath(), 'r') self.character_classifier = CharacterClassifier( from_string_string=character_classifier_file.read()) character_classifier_file.close() #Word classifier word_classifier_file = open( File(path_to_this_dir, "word_classifier.dat").getPath(), 'r') self.word_classifier = WordClassifier( from_string_string=word_classifier_file.read()) word_classifier_file.close() #Set up window self.setTitle("HandReco Writer") self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) self.setLocationRelativeTo(None) self.setLayout(BorderLayout()) #Set up menu menu_bar = JMenuBar() info_menu = JMenu("Info") def instructions(event): instr = ''' The program can just recognise capital English characters. See Info -> Available Words... for available word corrections. Good Luck with the writing!''' JOptionPane.showMessageDialog(self, instr, "Instructions", JOptionPane.INFORMATION_MESSAGE) instructions_menu_item = JMenuItem("Instructions...", actionPerformed=instructions) info_menu.add(instructions_menu_item) def word_corrections(event): words_string = "" for word in self.word_classifier.words: words_string = words_string + word.upper() + "\n" text = "The words that can be corrected are:\n\n" + words_string dialog = JOptionPane(text, JOptionPane.INFORMATION_MESSAGE) dialog_wrapper = JDialog(self, "Available Words", False) dialog_wrapper.setContentPane(dialog) dialog_wrapper.pack() dialog_wrapper.setVisible(True) word_corrections_menu_item = JMenuItem( "Available Words...", actionPerformed=word_corrections) info_menu.add(word_corrections_menu_item) menu_bar.add(info_menu) self.setJMenuBar(menu_bar) #Input panel input_panel = JPanel() input_panel.setLayout(BoxLayout(input_panel, BoxLayout.X_AXIS)) input_panel.setBorder(BorderFactory.createTitledBorder("Input")) self.paint_area = PaintArea(100, 100) input_panel.add(self.paint_area) input_options_panel = JPanel() input_options_panel.setLayout( BoxLayout(input_options_panel, BoxLayout.Y_AXIS)) #Write Char write_char_panel = JPanel(BorderLayout()) def write_char(event): char = self.character_classifier.classify_image( self.paint_area.image()) self.text_area.setText(self.text_area.getText() + char) self.paint_area.clear() write_char_panel.add(JButton("Write Char", actionPerformed=write_char), BorderLayout.NORTH) input_options_panel.add(write_char_panel) #Space and Correct space_and_correct_panel = JPanel(BorderLayout()) def space_and_correct(event): text = self.text_area.getText() words = text.split(" ") string = words[-1] try: word = self.word_classifier.classify(string.lower()) words[-1] = word.upper() except: JOptionPane.showMessageDialog( self, "Have you entered a character which is not in the alphabet?", "Could not Correct", JOptionPane.ERROR_MESSAGE) self.text_area.setText(text + " ") return newText = "" for w in words: newText = newText + w + " " self.text_area.setText(newText) space_and_correct_panel.add( JButton("Space and Correct", actionPerformed=space_and_correct), BorderLayout.NORTH) input_options_panel.add(space_and_correct_panel) #Space space_panel = JPanel(BorderLayout()) def space(event): self.text_area.setText(self.text_area.getText() + " ") space_panel.add(JButton("Space", actionPerformed=space), BorderLayout.NORTH) input_options_panel.add(space_panel) #Clear Input Area clear_input_area_panel = JPanel(BorderLayout()) def clear(event): self.paint_area.clear() clear_input_area_panel.add( JButton("Clear Input Area", actionPerformed=clear), BorderLayout.NORTH) input_options_panel.add(clear_input_area_panel) input_panel.add(input_options_panel) self.add(input_panel, BorderLayout.NORTH) text_area_panel = JPanel() text_area_panel.setLayout(BorderLayout()) text_area_panel.setBorder(BorderFactory.createTitledBorder("Text")) self.text_area = JTextArea() self.text_area.setLineWrap(True) #Increase font size font = self.text_area.getFont() self.text_area.setFont(Font(font.getName(), Font.BOLD, 24)) text_area_panel.add(JScrollPane(self.text_area), BorderLayout.CENTER) self.add(text_area_panel, BorderLayout.CENTER) self.pack() self.setSize(Dimension(300, 300)) self.setVisible(True)