Example #1
0
    def predict(self, image, display_image):
        """Object detection for flowchart and generate nodes for shapes
		and connectors."""

        # Image preparation
        image = Preprocessor.apply_unsharp_masking(image)
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

        # Format input image
        X, ratio = ImageTools.get_format_img_size(image, self.config)
        X = np.transpose(X, (0, 2, 3, 1))

        # get the feature maps and output from the RPN
        [Y1, Y2, F] = self.model_rpn.predict(X)
        # Instance a ROI Heper
        roi_helper = ROIHelpers(self.config,
                                overlap_thresh=self.overlap_thresh_1)
        R = roi_helper.convert_rpn_to_roi(Y1, Y2)
        # convert from (x1,y1,x2,y2) to (x,y,w,h)
        R[:, 2] -= R[:, 0]
        R[:, 3] -= R[:, 1]
        # Apply the spatial pyramid pooling to the proposed regions
        bboxes, probs = self.__apply_spatial_pyramid_pooling(R, F)

        img, all_dets = self.__generate_final_image(bboxes, probs, image,
                                                    roi_helper, ratio)
        if (display_image):
            cv2.imshow('test', cv2.resize(img, (0, 0), fx=0.4, fy=0.4))
            cv2.waitKey(0)
            cv2.destroyAllWindows()

        return self.generate_nodes(all_dets)
Example #2
0
    def predict_and_save(self, image, image_name, folder_name):
        """Perform object detection, in this case elements of flowchart and
		draw bounding boxes and image and save the same."""

        # Image preparation
        image = Preprocessor.apply_unsharp_masking(image)

        st = time.time()  # start time
        # Format input image
        X, ratio = ImageTools.get_format_img_size(image, self.config)
        X = np.transpose(X, (0, 2, 3, 1))

        # get the feature maps and output from the RPN
        [Y1, Y2, F] = self.model_rpn.predict(X)

        # Instance a ROI Heper
        roi_helper = ROIHelpers(self.config,
                                overlap_thresh=self.overlap_thresh_1)
        R = roi_helper.convert_rpn_to_roi(Y1, Y2)
        # convert from (x1,y1,x2,y2) to (x,y,w,h)
        R[:, 2] -= R[:, 0]
        R[:, 3] -= R[:, 1]
        # Apply the spatial pyramid pooling to the proposed regions
        bboxes, probs = self.__apply_spatial_pyramid_pooling(R, F)

        img, _ = self.__generate_final_image(bboxes, probs, image, roi_helper,
                                             ratio)

        print('Elapsed time = {}'.format(time.time() - st))
        # Save image
        path = self.results_path + "/" + folder_name
        if (os.path.isdir(path) == False):
            os.mkdir(path)

        cv2.imwrite(path + "/" + image_name, img)
        print("Image {}, save in {}".format(image_name, path))