def ground(self, imagefile, segments, _=None): """asks the user to label each segment as either a character or "<" for unknown""" print( "For each shown segment, please write the character that it represents, or spacebar if it's not a " "character. To undo a classification, press backspace. Press ESC when completed, arrow keys to move" ) i = 0 if imagefile.is_grounded: classes = classes_from_numpy(imagefile.ground.classes) segments = imagefile.ground.segments else: classes = [BLANK_CLASS] * len(segments) done = False allowed_chars = list( map(ord, string.digits + string.ascii_letters + string.punctuation)) while not done: image = imagefile.image.copy() draw_segments(image, [segments[i]]) draw_classes(image, segments, classes) key = show_image_and_wait_for_key(image, "segment " + str(i)) if key == 27: # ESC break elif key == 8: # backspace classes[i] = BLANK_CLASS i += 1 elif key == 32: # space classes[i] = NOT_A_SEGMENT i += 1 elif key == 65361: # <- i -= 1 elif key == 65363: # -> i += 1 elif key in allowed_chars: classes[i] = unichr(key) i += 1 if i >= len(classes): i = 0 if i < 0: i = len(classes) - 1 classes = numpy.array(classes) is_segment = classes != NOT_A_SEGMENT classes = classes[is_segment] segments = segments[is_segment] classes = list(classes) classes = classes_to_numpy(classes) print("classified ", numpy.count_nonzero(classes != classes_to_numpy(BLANK_CLASS)), "characters out of", max(classes.shape)) imagefile.set_ground(segments, classes)
def ground(self, imagefile, segments, _=None): '''asks the user to label each segment as either a character or "<" for unknown''' print '''For each shown segment, please write the character that it represents, or spacebar if it's not a character. To undo a classification, press backspace. Press ESC when completed, arrow keys to move''' i = 0 if imagefile.isGrounded(): classes = classes_from_numpy(imagefile.ground.classes) segments = imagefile.ground.segments else: classes = [BLANK_CLASS] * len( segments ) #char(10) is newline. it represents a non-assigned label, and will b filtered done = False allowed_chars = map( ord, string.digits + string.letters + string.punctuation) while not done: image = imagefile.image.copy() draw_segments(image, [segments[i]]) draw_classes(image, segments, classes) key = show_image_and_wait_for_key( image, "segment " + str(i), return_arrow_keys=True) if key == 27: #ESC break elif key == 8: #backspace classes[i] = BLANK_CLASS i += 1 elif key == 32: #space classes[i] = NOT_A_SEGMENT i += 1 elif key == 65361: #<- i -= 1 elif key == 65363: #-> i += 1 elif key in allowed_chars: classes[i] = unichr(key) i += 1 if i >= len(classes): i = 0 if i < 0: i = len(classes) - 1 classes = numpy.array(classes) is_segment = classes != NOT_A_SEGMENT classes = classes[is_segment] segments = segments[is_segment] classes = list(classes) classes = classes_to_numpy(classes) print "classified ", numpy.count_nonzero( classes != classes_to_numpy(BLANK_CLASS) ), "characters out of", max(classes.shape) imagefile.set_ground(segments, classes)
def write_boxfile(path, classes, segments): classes, segments = classes_from_numpy(classes), segments_from_numpy( segments) with open(path, 'w') as f: for c, s in zip(classes, segments): f.write(c.encode('utf-8') + ' ' + ' '.join(map(str, s)) + " 0\n")
def write_boxfile(path, classes, segments): classes, segments= classes_from_numpy(classes), segments_from_numpy(segments) with open(path, 'w') as f: for c,s in zip(classes, segments): f.write( c.encode('utf-8')+' '+ ' '.join(map(str, s))+" 0\n")