Exemplo n.º 1
0
def show_differences( image, segments, ground_classes, result_classes):
    image= image.copy()
    good= (ground_classes==result_classes)
    good.shape= (len(good),) #transform nx1 matrix into vector
    draw_segments( image, segments[good,:], (0,255,0) )
    draw_segments( image, segments[numpy.logical_not(good),:], (0,0,255)  )   
    show_image_and_wait_for_key(image, "differences")
Exemplo n.º 2
0
def show_differences( image, segments, ground_classes, result_classes):
    image= image.copy()
    good= (ground_classes==result_classes)
    good.shape= (len(good),) #transform nx1 matrix into vector
    draw_segments( image, segments[good,:], (0,255,0) )
    draw_segments( image, segments[numpy.logical_not(good),:], (0,0,255)  )   
    show_image_and_wait_for_key(image, "differences")
Exemplo n.º 3
0
 def display(self, display_before=False):
     copy = self.image.copy()
     if display_before:
         show_image_and_wait_for_key(copy, "image before segmentation")
     copy.fill(255)
     cv2.drawContours(copy, self.contours, contourIdx=-1, color=(0, 0, 0))
     show_image_and_wait_for_key(copy, "ContourSegmenter contours")
     copy = self.image.copy()
     draw_segments(copy, self.segments)
     show_image_and_wait_for_key(copy, "image after segmentation by " + self.__class__.__name__)
Exemplo n.º 4
0
 def display(self, display_before=False):
     copy= self.image.copy()
     if display_before:
         show_image_and_wait_for_key(copy, "image before segmentation")
     copy.fill( (255,255,255) )
     cv2.drawContours(copy, self.contours, contourIdx=-1, color=(0,0,0))
     show_image_and_wait_for_key( copy, "ContourSegmenter contours")
     copy= self.image.copy()
     draw_segments( copy, self.segments)
     show_image_and_wait_for_key(copy, "image after segmentation by "+self.__class__.__name__)
 def display( self, display_before=False):
     '''shows the effect of this filter'''
     try:
         copy= self.image.copy()
     except AttributeError:
         raise Exception("You need to set the Filter.image attribute for displaying")
     copy= BrightnessProcessor(brightness=0.6).process( copy )
     s, g= self._input, self.good_segments_indexes
     draw_segments( copy, s[g], (0,255,0) )
     draw_segments( copy, s[True-g], (0,0,255) )
     show_image_and_wait_for_key( copy, "segments filtered by "+self.__class__.__name__)
Exemplo n.º 6
0
    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 display(self, display_before=False):
     """shows the effect of this filter"""
     try:
         copy = self.image.copy()
     except AttributeError:
         raise Exception(
             "You need to set the Filter.image attribute for displaying")
     copy = BrightnessProcessor(brightness=0.6).process(copy)
     s, g = self._input, self.good_segments_indexes
     draw_segments(copy, s[g], (0, 255, 0))
     draw_segments(copy, s[True - g], (0, 0, 255))
     show_image_and_wait_for_key(
         copy, "segments filtered by " + self.__class__.__name__)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
                    text = result[0]
                if text == " ":
                    if len(whitelist) == 10:
                        text = "#"
                    elif len(whitelist) == 2 or len(whitelist) == 26:
                        text = "*"
                    else:
                        text = "?"
                segment_text_list.append([segment, text])
                tesseract_classes.append(text)
            test_classes = tesseract_classes
            image_dict[fname] = cluster_list
        if not terse:
            image = test_image.image.copy()
            opencv_utils.draw_segments(image,
                                       test_segments,
                                       color=(255, 0, 0),
                                       line_width=2)
            opencv_utils.draw_classes(image,
                                      test_segments,
                                      test_classes,
                                      color=(255, 255, 0),
                                      line_width=2)

            opencv_utils.show_image_and_wait_for_key(
                image, "OCR results for file " + fname)

            if test_image.isGrounded():
                print "accuracy:", accuracy(test_image.ground.classes,
                                            test_classes)
                print "OCRed text:\n", reconstruct_chars(test_classes)
                show_differences(test_image.image, test_segments,
Exemplo n.º 10
0
                if len(result) > 0:
                    text = result[0]
                if text == " ":
                    if len(whitelist) == 10:
                        text = "#"
                    elif len(whitelist) == 2 or len(whitelist) == 26:
                        text = "*"
                    else:
                        text = "?"
                segment_text_list.append([segment, text])
                tesseract_classes.append(text)
            test_classes = tesseract_classes
            image_dict[fname] = cluster_list
        if not terse:
            image = test_image.image.copy()
            opencv_utils.draw_segments(image, test_segments, color=(255, 0, 0), line_width=2)
            opencv_utils.draw_classes(image, test_segments, test_classes, color=(255, 255, 0), line_width=2)
    
            opencv_utils.show_image_and_wait_for_key(image, "OCR results for file " + fname)

            if test_image.isGrounded():
                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)
    
    if use_tesseract:
        api.End()
        
        ground_truth_dict = {}
        ground_truth_file = open("CroppedMasterIDs.csv", "r")
        for line in ground_truth_file: