Beispiel #1
0
    def process_output(self, output):
        # get results
        scores = output['scores']
        top_k_results = scores.argsort()[-self.top_k:][::-1]

        annotations = []

        for i in top_k_results:
            human_string = self.labels[i]
            if self.floating_model:
                score = scores[i]
            else:
                score = scores[i] / 255.0
            anno = {
                'type': 'classification',
                'label': human_string,
                'confidence': score,
                'top': 0,
                'left': 0,
                'bottom': self.img_h,
                'right': self.img_w
            }
            annotations.append(anno)

        results = lab_tools.output_pred_detection("",
                                                  annotations,
                                                  anno_in_lab_format=True)
        results["width"] = self.img_w
        results["height"] = self.img_h

        return results
Beispiel #2
0
    def main_process(self):
        """ Main function of dyda component. """

        self.reset_output()
        self.input_data = self.uniform_input(self.input_data, 'ndarray')

        for input_img in self.input_data:
            width = input_img.shape[1]
            height = input_img.shape[0]

            # the boxes face_locations returns are in the order
            # of (top, right, bottom, left)
            boxes = face_locations(
                input_img,
                model=self.model,
                number_of_times_to_upsample=self.upsample_times)

            # box in lab_format is [top, bottom, left, right]
            annotations = [["face", -1.0, [i[0], i[2], i[3], i[1]]]
                           for i in boxes]
            self.results.append(
                lab_tools.output_pred_detection("",
                                                annotations,
                                                img_size=[width, height]))

        self.uniform_output()
Beispiel #3
0
    def process_output(self, output):
        # get results
        boxes = np.squeeze(output['boxes'][0])
        classes = np.squeeze(output['classes'][0] + 1).astype(np.int32)
        scores = np.squeeze(output['scores'][0])
        num = output['num'][0]

        annotations = []
        number_boxes = boxes.shape[0]
        for i in range(number_boxes):
            box = tuple(boxes[i].tolist())
            ymin, xmin, ymax, xmax = box

            if scores[i] < self.threshold:
                continue
            lab_dict = lab_tools._lab_annotation_dic()
            lab_dict['type'] = 'detection'
            lab_dict['label'] = self.labels[classes[i]]
            lab_dict['confidence'] = float(scores[i])
            lab_dict['left'] = int(xmin * self.img_w)
            lab_dict['top'] = int(ymin * self.img_h)
            lab_dict['right'] = int(xmax * self.img_w)
            lab_dict['bottom'] = int(ymax * self.img_h)
            annotations.append(lab_dict)
        results = lab_tools.output_pred_detection("",
                                                  annotations,
                                                  anno_in_lab_format=True)
        results["width"] = self.img_w
        results["height"] = self.img_h
        return results
Beispiel #4
0
    def main_process(self):
        """ Main function of dyda component. """

        self.reset_output()
        self.input_data = self.uniform_input()

        for input_img in self.input_data:

            im_w = input_img.shape[1]
            im_h = input_img.shape[0]

            input_img = Image.fromarray(
                cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB))
            face = self.resize_shorter_side(input_img, 400)[0]
            face_tens = self.tf(face).to(self.device)

            # Prediction
            with torch.no_grad():
                prob = self.model(
                    face_tens.unsqueeze(0))[0].sigmoid().cpu().item()
            results = lab_tools.output_pred_detection("", "")
            results["verifications"] = []
            results["verifications"].append({
                "type":
                "photoshopped_face",
                "confidence":
                prob,
                "result":
                "Passed" if prob <= self.conf_thre else "Mismatched"
            })
            self.results.append(results)
        self.uniform_output()
Beispiel #5
0
    def main_process(self, thresh=.5, hier_thresh=.5, nms=.45):
        # FIXME: the first version of DetectorYOLO accepts a list of image
        #       paths as input_data, this should be fixed (issue #30)
        nms = self.param["nms"]
        hier_thresh = self.param["hier_thresh"]
        thresh = self.param["thresh"]
        annos = None

        def nparray_to_image(img):
            data = img.ctypes.data_as(POINTER(c_ubyte))
            image = self.ndarray_image(data, img.ctypes.shape,
                                       img.ctypes.strides)
            return image

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

        for img_array in self.input_data:
            im = nparray_to_image(img_array)
            img_shape = img_array.shape
            img_w = img_shape[1]
            img_h = img_shape[0]
            boxes = self.make_boxes(self.net)
            probs = self.make_probs(self.net)
            num = self.num_boxes(self.net)
            obj_info_list = self.network_detect_objinfo(
                self.net, im, thresh, hier_thresh, nms, boxes, probs)
            objs = obj_info_list.data[:obj_info_list.len]
            annos = []
            for obj in objs:
                label = str(self.meta.names[obj.label], 'utf-8')
                conf = obj.confidence
                bb = [obj.top, obj.bottom, obj.left, obj.right]
                annos.append([label, conf, bb])
            self.free_image(im)
            self.free_object_info_list(obj_info_list)
            self.free_ptrs(cast(probs, POINTER(c_void_p)), num)
            self.free_boxes(boxes)
            res = lab_tools.output_pred_detection(self.metadata[0], annos)
            self.results.append(res)

        if package:
            self.results = self.results[0]
        return annos
    def main_process(self, thresh=.1):
        # TODO: Check whether the FIXME below affects this detector or not.
        # FIXME: the first version of DetectorYOLO accepts a list of image
        #       paths as input_data, this should be fixed (issue #30)
        thresh = self.param["thresh"]
        annos = None

        def nparray_to_image(img):
            data = img.ctypes.data_as(POINTER(c_ubyte))
            image = self.ndarray_image(data, img.ctypes.shape,
                                       img.ctypes.strides)
            return image

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

        for img_array in self.input_data:
            image_data = self.engine.process_input(img_array)
            output = self.engine.inference(image_data)
            model_outputs = self.engine.process_output(output)

            # TODO: Write another process_output method to
            #       generate dyda-preferred format instead of
            #       doing redundant format conversions.
            annos = []
            for anno in model_outputs['annotations']:
                annos.append([
                    anno['label'], anno['confidence'],
                    [
                        anno['top'],
                        anno['bottom'],
                        anno['left'],
                        anno['right'],
                    ]
                ])
            res = lab_tools.output_pred_detection(self.metadata[0], annos)
            self.results.append(res)

        if package:
            self.results = self.results[0]
        return annos
    def main_process(self):
        if len(self.input_data) == 0:
            logger.error('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)
            det_results = self.process_output(inf_results)

            # why this code looks so redundant here is beacause that
            # this script is modified from berrynet openvino_engine.py,
            # and I follow the principle that make least change of original
            # code. the annotations structure in process_output is original
            # defined in berrynet, and in order to use the lab_tools, we
            # must re-define the structure
            annotations = [[
                det_result['label'], det_result['confidence'],
                [
                    det_result['top'], det_result['bottom'],
                    det_result['left'], det_result['right']
                ]
            ] for det_result in det_results]

            # orders, labinfo = self.create_labinfo(inf_results)
            res = lab_tools.output_pred_detection(
                # input_path=self.orig_input_path,
                input_path='',
                annotations=annotations,
                img_size=(orig_h, orig_w),
                # labinfo=labinfo
                labinfo={})
            self.results.append(res)
        logger.debug('self.results: {}'.format(self.results))
Beispiel #8
0
    def main_process(self):
        """
        Main process
        """

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

        unpack = False
        if type(self.input_data) is not list:
            self.pack_input_as_list()
            unpack = True

        image_tensor = self.graph.get_tensor_by_name('image_tensor:0')
        det_boxes = self.graph.get_tensor_by_name('detection_boxes:0')
        det_scores = self.graph.get_tensor_by_name('detection_scores:0')
        det_classes = self.graph.get_tensor_by_name('detection_classes:0')
        num_dets = self.graph.get_tensor_by_name('num_detections:0')

        for _img_array in self.input_data:
            img_array = self.check_bgr_rgb(_img_array)
            rows = img_array.shape[0]
            cols = img_array.shape[1]
            img_array = np.expand_dims(img_array, axis=0)

            (boxes, scores, classes, num) = self.sess.run(
                [det_boxes, det_scores, det_classes, num_dets],
                feed_dict={image_tensor: img_array})

            # results are packaged in the list, unpack it
            boxes = boxes[0]
            scores = scores[0]
            classes = classes[0]

            annos = []
            for i, score in enumerate(scores):
                if score <= 0:
                    continue
                if score < self.thre:
                    continue
                bb = [
                    int(boxes[i][0] * rows),
                    int(boxes[i][2] * rows),
                    int(boxes[i][1] * cols),
                    int(boxes[i][3] * cols)
                ]
                annos.append([
                    self.labels[int(classes[i])][self.label_key],
                    float(score), bb
                ])

            # output_pred_detection(input_path, annotations, img_size=[],
            #                       labinfo={}, save_json=False):
            # annotations: A list of annotations [[label, conf, bb]]
            #              where bb is [top, bottom, left, right]
            res = lab_tools.output_pred_detection(self.orig_input_path,
                                                  annos,
                                                  img_size=(cols, rows))
            self.results.append(res)

        if unpack:
            self.unpack_single_input()
            self.unpack_single_results()