def train(self, charcol, meta, path=None): self._check_meta(meta) trainer = zinnia.Trainer() zinnia_char = zinnia.Character() for set_name in charcol.get_set_list(): for character in charcol.get_characters(set_name): if (not zinnia_char.parse(character.to_sexp())): raise TrainerError, zinnia_char.what() else: trainer.add(zinnia_char) if not path: if "path" in meta: path = meta["path"] else: path = os.path.join(os.environ['HOME'], ".tegaki", "models", "zinnia", meta["name"] + ".model") else: path = os.path.abspath(path) if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) meta_file = path.replace(".model", ".meta") if not meta_file.endswith(".meta"): meta_file += ".meta" trainer.train(path) self._write_meta_file(meta, meta_file)
def recognize(self, recognizer, writing): character = Character() character.set_utf8("?") character.set_writing(writing) sexp = self.char_to_sexp(character) zinnia_char = zinnia.Character() if (not zinnia_char.parse(sexp)): print zinnia_char.what() exit(1) results = recognizer.classify(zinnia_char, 10) return [results.value(i) for i in range(0, (results.size() - 1))]
def _recognize(self, writing, n=10): s = zinnia.Character() s.set_width(writing.get_width()) s.set_height(writing.get_height()) strokes = writing.get_strokes() for i in range(len(strokes)): stroke = strokes[i] for x, y in stroke: s.add(i, x, y) result = self._recognizer.classify(s, n + 1) size = result.size() return Results([(result.value(i), result.score(i)) \ for i in range(0, (size - 1))])
def train(self): trainer = zinnia.Trainer() zinnia_char = zinnia.Character() for char_code, xml_list in self.train_xml_files_dict.items(): for xml_file in xml_list: character = self.get_character(xml_file) sexp = self.char_to_sexp(character) if (not zinnia_char.parse(sexp)): print zinnia_char.what() exit(1) else: trainer.add(zinnia_char) path = os.path.join(self.ROOT, "model") trainer.train(path)
#!/usr/bin/python import zinnia input = "(character (width 1000)(height 1000)(strokes ((243 273)(393 450))((700 253)(343 486)(280 716)(393 866)(710 880))))" try: s = zinnia.Character() r = zinnia.Recognizer() r.open("handwriting-zh_CN.model") if (not s.parse(input)): print s.what() result = r.classify(s, 10) size = result.size() for i in range(0, (size - 1)): print result.value(i).decode('utf8'), '\t-\t', result.score(i) s.clear() s.set_width(300) s.set_height(300) s.add(0, 51, 29) s.add(0, 117, 41) s.add(1, 99, 65) s.add(1, 219, 77) s.add(2, 27, 131) s.add(2, 261, 131) s.add(3, 129, 17) s.add(3, 57, 203) s.add(4, 111, 71) s.add(4, 219, 173)