def single_classify(self, image_path, directory, extractors, dict_classes):
        '''
        '''
        from extraction import FeatureExtractor
        from os import remove
        test_file = 'temp'
        fextractor=FeatureExtractor(extractors)
        fextractor.extract_one_file(directory, image_path, output_file = test_file)
        
        predicted = self.classify(directory, test_dir='.tmp', test_data=test_file+'.arff', image=None)

        remove(directory+'/'+test_file+'.arff')
        return predicted[0]
示例#2
0
 def run_extractors(self):
     """Perform a feature extraction on all images of dataset, using the current collection of extractors.
     """
     self.tk.write_log("Running extractors on all images in %s", self.dataset)
     self.tk._root.update_idletasks()
     fextractor = FeatureExtractor(self.extractors,self.tk)
     self.tk.append_log("%s", '\n'.join([extraction._extractor_list[extractor].label for extractor in extraction._extractor_list
                                             if extraction._extractor_list[extractor].value == True ]))
     
     output_file, run_time = fextractor.extract_all(self.dataset, "training")
     self.tk.append_log("\nOutput file saved in %s", output_file)
     self.tk.append_log("Time elapsed: %0.3f seconds", run_time)
     
     if self.classifier: self.classifier.reset()
示例#3
0
    def train(self, train_generator):
        x, y = [], []
        for i, (image, mask) in enumerate(train_generator):
            point_generator = PointGeneratorBalanced(mask, self.__patch_count,
                                                     balance=self.__training_balance)
            feature_extractor = FeatureExtractor(image, self.__patch_size, point_generator)
            pixel_extractor = PixelExtractor(mask, point_generator)

            x.extend(feature_extractor.extract())
            y.extend(pixel_extractor.extract())

            stdout.write("\rTraining features: %.1f%%" % (100.0 * (i + 1) / train_generator.size))
            stdout.flush()
        stdout.write("\nTrain...")
        stdout.flush()
        self.__model.fit(x, y)
        stdout.write("\rTraining done\n")
示例#4
0
    def run_training(self):
        start_time = TimeUtils.get_time()
        
        # Training do not need an image opened (consider removing these two lines)
        #      if self._const_image is None:
        #          raise IException("Image not found")
        
        if self.classifier.must_train():
            
            if self.classifier.must_extract_features():
                self.tk.append_log("Creating training data... (%0.3f seconds)", (TimeUtils.get_time() - start_time))
                fextractor = FeatureExtractor(self.extractors)
                output_file, run_time = fextractor.extract_all(self.dataset, "training", overwrite = False)
        
            self.tk.append_log("Training classifier...")
            
            self.classifier.train(self.dataset, "training")

            self.tk.append_log("DONE (%0.3f seconds)",  (TimeUtils.get_time() - start_time))
        
        self._image = self._const_image
        self.has_trained=True
示例#5
0
    def experimenter_all(self):
        """Perform a test in all availabel classifiers e show the results.
        
        Raises
        ------
        IException 'You must install python-weka-wrapper'
            The user must install the required dependencies to classifiers.
        """
        if self.classifier is None:
            raise IException("Classifier not found! Select from the menu the option Training>Choose Classifier!")
        
        if self.tk.ask_ok_cancel("Experimenter All", "This may take several minutes to complete. Are you sure?"):
            if self.classifier.must_train():
                self.tk.write_log("Creating training data...")

                fextractor = FeatureExtractor(self.extractors)
                output_file, run_time = fextractor.extract_all(self.dataset, "training", overwrite = False)
                self.classifier.train(self.dataset, "training")
                
            self.tk.write_log("Running Experimenter All on %s...", self.classifier.get_name())
            
            popup_info = self.classifier.experimenter()
            self.tk.append_log("\nExperimenter All finished")
            self.tk.popup(popup_info)
示例#6
0
 def cross_validation(self):
     """Run a cross validation on all generated segments in image dataset.
     
     Raises
     ------
     IException 'You must install python-weka-wrapper'
         The user must install the required dependencies to classifiers.
     """
     if self.classifier is None:
         raise IException("Classifier not found! Select from the menu the option Training>Choose Classifier!")
     
     if self.classifier.must_train():
         self.tk.write_log("Creating training data...")
         
         fextractor = FeatureExtractor(self.extractors)
         output_file, run_time = fextractor.extract_all(self.dataset, "training", overwrite = False)
         self.classifier.train(self.dataset, "training")
     
     self.tk.write_log("Running Cross Validation on %s...", self.classifier.get_name())
     self.tk.append_log("\n%s", str(self.classifier.get_summary_config()))
     
     popup_info = self.classifier.cross_validate()
     self.tk.append_log("Cross Validation finished")
     self.tk.popup(popup_info)
示例#7
0
 def _inner_classify(self, input_image):
     point_generator = PointGeneratorAll(input_image.shape)
     feature_extractor = FeatureExtractor(input_image, self.__patch_size, point_generator)
     return self.__model.predict([*feature_extractor.extract()]).reshape(input_image.shape)
示例#8
0
        timer_detector = UtilDebug.PageDetectorTimer()
        timer_detector.startTimer(1)

        img = cv2.imread(imgPath[i], 0)
        img = PageDetector.enderezarImagen(img)
        page = PageDetector.detectPage(img)

        timer_detector.endTimer()

        if page is not None:
            # plt.imshow(page[0],'gray')
            # plt.title(' Es la página: '+str(page[1][0]))
            # plt.show()
            print(' This image can be processed')
            if page[1][1] == 0:  # esta orientado de manera normal
                FeatureExtractor.extractPageData(page[0], page[1][0], None,
                                                 os.path.basename(imgPath[i]))
                print('Total time: {0} seconds'.format(time.time() -
                                                       start_time))
            else:
                if page[1][1] == 1:  # esta al revez
                    flipped = cv2.flip(page[0], 0)
                    flipped = cv2.flip(flipped, 1)
                    FeatureExtractor.extractPageData(
                        flipped, page[1][0], None,
                        os.path.basename(imgPath[i]))
                    print('Total time: {0} seconds'.format(time.time() -
                                                           start_time))
                else:
                    raise ValueError(
                        'Image cannot be processed, Check its quality\nUse a different scanner and try again'
                    )
示例#9
0
    def run_classifier(self):
        """Run the classifier on the current image.
        As result, paint the image with color corresponding to predicted class of all segment.

        Raises
        ------
        IException 'You must install python-weka-wrapper'
            The user must install the required dependencies to classifiers.
        IException 'Image not found'
            If there's no image opened.
        """
        if self.classifier is None:
            raise IException("Classifier not found! Select from the menu the option Training>Choose Classifier!")

        if self._const_image is None:
            raise IException("Image not found!  Open an image to test, select in the menu the option File>Open Image!")

        self.tk.write_log("Running %s...", self.classifier.get_name())
        self.tk.append_log("\n%s", str(self.classifier.get_summary_config()))

        #self.classifier.set

        start_time = TimeUtils.get_time()

        # Perform a segmentation, if needed.
        list_segments = self.segmenter.get_list_segments()
        if len(list_segments) == 0:
            self.tk.append_log("Running %s... (%0.3f seconds)", self.segmenter.get_name(), (TimeUtils.get_time() - start_time))

            self._image, _ = self.segmenter.run(self._const_image)
            self.tk.refresh_image(self._image)
            list_segments = self.segmenter.get_list_segments()
            self._gt_segments = [None]*(max(list_segments)+1)

        #  New and optimized classification
        tmp = ".tmp"
        File.remove_dir(File.make_path(self.dataset, tmp))

        self.tk.append_log("Generating test images... (%0.3f seconds)", (TimeUtils.get_time() - start_time))

        len_segments = {}

        print("Wait to complete processes all images!")
        with tqdm(total=len(list_segments)) as pppbar:
            for idx_segment in list_segments:
                segment, size_segment, idx_segment = self.segmenter.get_segment(self, idx_segment=idx_segment)[:-1]
                # Problem here! Dataset removed.
                filepath = File.save_only_class_image(segment, self.dataset, tmp, self._image_name, idx_segment)
                len_segments[idx_segment] = size_segment
                pppbar.update(1)
            pppbar.close()


        # Perform the feature extraction of all segments in image ( not applied to ConvNets ).
        if self.classifier.must_extract_features():
            self.tk.append_log("Running extractors on test images... (%0.3f seconds)", (TimeUtils.get_time() - start_time))
            fextractor = FeatureExtractor(self.extractors)
            output_file, _ = fextractor.extract_all(self.dataset, "test", dirs=[tmp])

        self.tk.append_log("Running classifier on test data... (%0.3f seconds)", (TimeUtils.get_time() - start_time))

        # Get the label corresponding to predict class for each segment of image.
        labels = self.classifier.classify(self.dataset, test_dir=tmp, test_data="test.arff", image=self._const_image)
        File.remove_dir(File.make_path(self.dataset, tmp))

        # Result is the class for each superpixel
        if type(labels) is types.ListType:
            self.tk.append_log("Painting segments... (%0.3f seconds)", (TimeUtils.get_time() - start_time))

            # If ground truth mode, show alternative results
            if self._ground_truth == True:
                return self._show_ground_truth(list_segments, len_segments, labels, start_time)

            # Create a popup with results of classification.
            popup_info = "%s\n" % str(self.classifier.get_summary_config())

            len_total = sum([len_segments[idx] for idx in len_segments])
            popup_info += "%-16s%-16s%0.2f%%\n" % ("Total", str(len_total), (len_total*100.0)/len_total)

            # Paint the image.
            self._mask_image = np.zeros(self._const_image.shape[:-1], dtype="uint8")
            height, width, channels = self._image.shape
            self.class_color = np.zeros((height,width,3), np.uint8)
            for (c, cl) in enumerate(self.classes):
                idx_segment = [ list_segments[idx] for idx in range(0, len(labels)) if cl["name"].value == labels[idx] or c == labels[idx]]
                if len(idx_segment) > 0:
                    self._image, _ = self.segmenter.paint_segment(self._image, cl["color"].value, idx_segment=idx_segment, border=False)
                    for idx in idx_segment:
                        self._mask_image[self.segmenter._segments == idx] = c
                        self.class_color[self.segmenter._segments == idx] = X11Colors.get_color(cl["color"].value)

                len_classes = sum([len_segments[idx] for idx in idx_segment])
                popup_info += "%-16s%-16s%0.2f%%\n" % (cl["name"].value, str(len_classes), (len_classes*100.0)/len_total)


            self.tk.refresh_image(self._image)
            self.tk.popup(popup_info)
        else:
            # Result is an image
            self._mask_image = labels
            height, width, channels = self._image.shape
            self.class_color = np.zeros((height,width,3), np.uint8)

            for (c, cl) in enumerate(self.classes):
                self.class_color[labels == c] = X11Colors.get_color(cl["color"].value)

            self._image = cv2.addWeighted(self._const_image, 0.7, self.class_color, 0.3, 0)
            self.tk.refresh_image(self._image)


        end_time = TimeUtils.get_time()

        self.tk.append_log("\nClassification finished")
        self.tk.append_log("Time elapsed: %0.3f seconds", (end_time - start_time))
        gc.collect()