Пример #1
0
    def main_process(self):
        """
        """

        input_name = "import/" + self.param["input_layer"]
        output_name = "import/" + self.param["output_layer"]

        if len(self.input_data) == 0:
            print('[classifier] ERROR: no input_data found')
            self.terminate_flag = True

        for img_array in self.input_data:
            if self.param["convert_to_rgb"]:
                img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
            tensor = self.sess.run(self.tensor_op,
                                   feed_dict={'inarray:0': img_array})

            input_operation = self.graph.get_operation_by_name(input_name)
            output_operation = self.graph.get_operation_by_name(output_name)

            inf_results = self.sess.run(output_operation.outputs[0],
                                        {input_operation.outputs[0]: tensor})

            inf_results = np.squeeze(inf_results)
            orders, labinfo = self.create_labinfo(inf_results)
            res = lab_tools.output_pred_classification(
                input_path=self.orig_input_path,
                conf=inf_results[orders[0]],
                label=self.labels[orders[0]],
                img_size=(img_array.shape[1], img_array.shape[0]),
                labinfo=labinfo)

            self.results.append(res)
Пример #2
0
    def main_process(self):
        """ Main function of dyda component. """

        if isinstance(self.input_data, dict):
            self.logger.debug("Input data is dict")
            if not lab_tools.if_result_match_lab_format(self.input_data):
                self.terminate_flag = False
                self.logger.error("Can only accept lab format dictionary")
                return False
            input_data = self.input_data
        else:
            self.logger.debug("Input data is other type (expect ndarray)")
            if isinstance(self.input_data, list):
                if len(self.input_data) == 1:
                    input_img = self.input_data[0]
                else:
                    input_img = self.input_data[0]
                    self.logger.warning(
                        "More than one input img array detected")
            else:
                input_img = self.input_data
            width = input_img.shape[1]
            height = input_img.shape[0]
            input_data = lab_tools.output_pred_classification("", 1.0, "roi")
            input_data["annotations"][0]["top"] = 0
            input_data["annotations"][0]["bottom"] = height
            input_data["annotations"][0]["left"] = 0
            input_data["annotations"][0]["right"] = width

        self.results = boxes.shrink_boxes(copy.deepcopy(input_data),
                                          self.shrink_perc,
                                          shift_to_pad=self.shift_to_pad)
Пример #3
0
    def main_process(self):
        """ Main function of dyda component. """

        self.reset_output()
        if not isinstance(self.input_data, list):
            self.input_data = [self.input_data]
            self.package = True
        else:
            self.package = False

        # let input_data will always be list of list
        if not any(isinstance(i, list) for i in self.input_data):
            self.input_data = [self.input_data]
            self.package_2 = True
        else:
            self.package_2 = False

        # open and load the pickle file only at first frame
        if self.first_time:

            if self.pickle_path == "":
                self.pickle_path = self.snapshot_folder.replace(
                    "FaceMatchAnalyzer",
                    "FaceEncodingImageProcessor/encodings.pickle")

            with open(self.pickle_path, 'rb') as rfp:
                self.known_faces = pickle.load(rfp)

            self.first_time = False

        for encodings in self.input_data:

            temp_results = []

            for encoding in encodings:
                matches = compare_faces(self.known_faces["encodings"],
                                        encoding,
                                        tolerance=self.tolerance)

                if True not in matches:
                    whose_face = "unknown"
                else:
                    candidates = [
                        i for (i, j) in zip(self.known_faces['names'], matches)
                        if j
                    ]
                    whose_face = max(set(candidates), key=candidates.count)

                temp_results.append(
                    lab_tools.output_pred_classification("", -1, whose_face))

            self.results.append(temp_results)

            if self.package_2:
                self.unpack_single_results()
        self.uniform_output()
Пример #4
0
    def main_process(self):
        """ Main function of dyda component. """

        self.results = []
        self.output_data = []

        if len(self.input_data) == 0:
            self.logger.error("no input_data found")
            self.terminate_flag = True

        _input_data = copy.deepcopy(self.input_data)
        for img_array in _input_data:
            label = 'ok'
            if image.is_rgb(img_array):
                (h, w, ch) = img_array.shape
                gray = image.conv_gray(img_array)
            else:
                (h, w) = img_array.shape
                gray = img_array
            corners_avg = [
                np.average(gray[self.space_h:self.space_h + self.dh,
                                self.space_w:self.space_w + self.dw]),
                np.average(gray[self.space_h:self.space_h + self.dh, w -
                                (self.space_w + self.dw):w - self.space_w]),
                np.average(gray[h - (self.space_h + self.dh):h - self.space_h,
                                self.space_w:self.space_w + self.dw]),
                np.average(gray[h - (self.space_h + self.dh):h - self.space_h,
                                w - (self.space_w + self.dw):w - self.space_w])
            ]

            if self.thre_type == "lt":
                min_avg = min(corners_avg)
                conf = 1.0
                if min_avg < self.thre:
                    label = 'ng'
                    # linear scale pixel average range between 50~thre to 1~0.6
                    conf = (self.thre - 0.4 * min_avg - 35) / (self.thre - 50)
            else:
                max_avg = max(corners_avg)
                conf = 1.0
                if max_avg > self.thre:
                    label = 'ng'
                    # linear scale pixel average range from thre~230 to 0.6~1
                    conf = (0.4 * max_avg - self.thre + 138) / (230 -
                                                                self.thre)

            res = lab_tools.output_pred_classification(input_path="",
                                                       conf=conf,
                                                       label=label,
                                                       img_size=(w, h),
                                                       labinfo=[])

            # this classifier only select one type of ng
            if label == 'ok':
                res['annotations'] = []
            self.results.append(res)
Пример #5
0
    def main_process(self):

        super(PathLabelConverterLab, self).main_process()
        labels = copy.deepcopy(self.results)
        self.results = []
        for label in labels:
            self.results.append(
                lab_tools.output_pred_classification("", 1.0, label)
            )
        self.unpack_single_results()
Пример #6
0
    def main_process(self):
        """ define main_process of dyda component """

        self.output_data = []
        self.results = []
        for img in self.input_data:
            diff_img = self.get_diff_img(img)
            res = lab_tools.output_pred_classification(
                input_path=self.orig_input_path,
                label=self.get_output_result(diff_img),
                # FIXME: fake conf
                conf=random.uniform(0.7, 0.95))
            self.results.append(res)
Пример #7
0
    def main_process(self):
        """ define main_process of dyda component """

        for img_array in self.input_data:
            algo_output = self.get_testing_result(img_array)

            res = lab_tools.output_pred_classification(
                input_path=self.orig_input_path,
                conf=algo_output["conf"],
                label=algo_output["label"],
                img_size=(img_array.shape[1], img_array.shape[0]))

            self.results.append(res)
            self.output_data.append(img_array)
Пример #8
0
    def main_process(self):
        """ Main function of dyda component. """

        self.results = []
        self.output_data = []

        if len(self.input_data) == 0:
            self.logger.error("no input_data found")
            self.terminate_flag = True

        _input_data = copy.deepcopy(self.input_data)
        for img_array in _input_data:
            label = 'ok'
            if image.is_rgb(img_array):
                (h, w, ch) = img_array.shape
            else:
                (h, w) = img_array.shape
            img_array = image.resize_img(img_array,
                                         size=(self.bkg_img.shape[1],
                                               self.bkg_img.shape[0]))
            l1_diff = tinycv.l1_norm_diff_cv2(img_array, self.bkg_img)
            l1_diff_mean = np.mean(l1_diff, axis=self.mean_axis)
            max_diff = np.max(l1_diff_mean[self.sel_min:self.sel_max])

            label = 'ng'
            conf = 1.0
            if max_diff <= self.thre:
                label = 'ok'

            res = lab_tools.output_pred_classification(input_path="",
                                                       conf=conf,
                                                       label=label,
                                                       img_size=(w, h),
                                                       labinfo=[])

            # this classifier only select ok samples
            if label == 'ng':
                res['annotations'] = []
            self.results.append(res)
Пример #9
0
    def main_process(self):
        if len(self.input_data) == 0:
            logger.debug('no input_data found')
            self.terminate_flag = True

        logger.debug('self.input_data len: {}'.format(len(self.input_data)))
        for img_array in self.input_data:
            orig_h, orig_w = img_array.shape[:-1]
            img_array = self.process_input(img_array)
            inf_results = self.inference(img_array)
            top_result = self.process_output(inf_results)

            # orders, labinfo = self.create_labinfo(inf_results)
            res = lab_tools.output_pred_classification(
                # input_path=self.orig_input_path,
                input_path='',
                conf=top_result['confidence'],
                label=top_result['label'],
                img_size=(orig_h, orig_w),
                # labinfo=labinfo
                labinfo={})
            self.results.append(res)
        logger.debug('self.results: {}'.format(self.results))
 def test_main_process(self):
     ref_data = [['others', '1.00'], ['others', '1.00'], ['others', '0.90'],
                 ['person', '0.90'], ['others', '0.83'], ['others', '0.80']]
     r1 = lab_tools.output_pred_classification("", 1.0, "others")
     r2 = lab_tools.output_pred_classification("", 0.9, "person")
     r3 = lab_tools.output_pred_classification("", 0.8, "others")
     r4 = lab_tools.output_pred_classification("", 0.7, "others")
     r5 = lab_tools.output_pred_classification("", 0.2, "person")
     r6 = lab_tools.output_pred_classification("", 0.9, "others")
     results = []
     d = DeterminatorByAggregatedDataSingle()
     for r in [r1, r2, r3, r4, r5, r6]:
         d.input_data = r
         d.run()
         results.append([
             d.results["annotations"][0]["label"],
             "%0.2f" % d.results["annotations"][0]["confidence"]
         ])
     report = dict_comparator.get_diff(ref_data, results)
     self.assertEqual(report['extra_field'], [])
     self.assertEqual(report['missing_field'], [])
     self.assertEqual(report['mismatch_val'], [])
Пример #11
0
    def main_process(self):
        """ main_process of YTECDataConverter """

        if len(self.input_data) != 2:
            self.terminate_flag = True
            self.logger.error(
                "input_data should be a list of [image_array, metadata]."
            )
            return False

        if isinstance(self.input_data[0], np.ndarray):
            img_data = copy.deepcopy(self.input_data[0])
        elif isinstance(self.input_data[0], list):
            img_data = copy.deepcopy(self.input_data[0][0])
            if len(self.input_data[0]) > 1:
                self.logger.warning("Multiple input image arrays detected.")

        if isinstance(self.input_data[1], dict):
            metadata = self.input_data[1]
        elif isinstance(self.input_data[0], list):
            metadata = self.input_data[1][0]
            if len(self.input_data[1]) > 1:
                self.logger.warning("Multiple input metadata detected.")

        if "metadata" not in metadata.keys():
            self.terminate_flag = True
            self.logger.error("annotations key is missing in metadata.")
            return False

        anno = metadata["metadata"]

        try:
            self.rect.reset_loc([
                anno["top"], anno["bottom"], anno["left"], anno["right"]
            ])

        except BaseException:
            self.terminate_flag = True
            self.logger.error("Fail to set location info")
            return False

        rot_angle = 0
        if "degree" in anno.keys():
            rot_angle = anno["degree"]

        if rot_angle != 0:
            rot_img = tinycv.rotate(img_data, rot_angle)
        else:
            rot_img = img_data

        cropped = tinycv.crop_img_rect_rgb(rot_img, self.rect)
        self.output_data = [cropped]
        shape = cropped.shape
        self.results = lab_tools.output_pred_classification(
            "", 1.0, "raw", img_size=[shape[1], shape[0]]
        )
        self.results["folder"] = os.path.join(
            copy.deepcopy(self.snapshot_folder), "output_data"
        )
        self.results["filename"] = copy.deepcopy(self.metadata[0]) + ".jpg.0"
        self.results["annotations"][0]["top"] = 0
        self.results["annotations"][0]["left"] = 0
        self.results["annotations"][0]["bottom"] = shape[0]
        self.results["annotations"][0]["right"] = shape[1]
        self.results["annotations"][0]["rot_angle"] = 0

        return True
Пример #12
0
    def main_process(self):
        """ Main function of dyda component. """

        self.results = []
        self.output_data = []

        if len(self.input_data) == 0:
            print('[classifier] ERROR: no input_data found')
            self.terminate_flag = True

        _input_data = copy.deepcopy(self.input_data)
        for img_array in _input_data:

            # label initialization
            result = 'ok'

            # resize
            img_box = image.resize_img(
                img_array,
                (self.param['resize_width'], self.param['resize_height']))

            # rgb to gray
            im_g = cv2.cvtColor(img_box, cv2.COLOR_RGB2GRAY)

            # image binarization global
            thre = int(np.mean(im_g.mean(axis=1)))
            ret, im_b = cv2.threshold(im_g, thre, 1, cv2.THRESH_BINARY)

            # morphological opening
            kernel_size = int(
                np.round(self.param['resize_width'] *
                         self.param['kernel_ratio']))
            iter_num = self.param['iter_num']
            kernel = np.ones((kernel_size, kernel_size), np.uint8)
            im_o = cv2.erode(im_b.astype(np.uint8), kernel, iter_num)
            im_o = cv2.dilate(im_o, kernel, iter_num)

            # connected components labeling
            cc_label = measure.label(im_o, background=0)
            cc_label = cc_label.astype(np.uint8)

            # fragment detection
            label_idx = self.param['fragment_num']
            label = self.extract_label(label_idx, cc_label)
            pixel_num = sum(sum(label))
            score = [[], []]
            if pixel_num > 0:
                result = 'ng'
                score[1].append(1)

            # projection feature
            for i in range(self.param['fragment_num']):
                feature = self.projection_feature(i, cc_label)
                class_idx = self.clf.predict([feature])[0]
                if class_idx == 1:
                    result = 'ng'
                score[class_idx].append(
                    self.clf.predict_proba([feature])[0][class_idx])

            # generate final results
            if result == 'ng':
                final_score = max(score[1])
            else:
                final_score = min(score[0])
            res = lab_tools.output_pred_classification(
                input_path="",
                conf=final_score,
                label=result,
                img_size=(img_array.shape[1], img_array.shape[0]),
                labinfo=[])

            width = res["size"]["width"]
            height = res["size"]["height"]
            # Default space is 40 in tinycv, allow it to be adjusted based
            # on the size of the images
            space = max([40, int(width / 20), int(height / 20)])

            output_data = tinycv.patch_bb_dyda(img_array,
                                               res,
                                               color=[0, 128, 0],
                                               space=space)
            self.results.append(res)
            self.output_data.append(output_data)