Example #1
0
    def postprocess(self, predictions):
        postprocessed_result = []

        for result, input_info in zip(predictions[0], predictions[1]):
            scores = np.array(
                result.getLayerFp16(result.getAllLayerNames()[0]),
            ).reshape((1, 1, 80, 80))
            geometry1 = np.array(
                result.getLayerFp16(result.getAllLayerNames()[1]),
            ).reshape((1, 4, 80, 80))
            geometry2 = np.array(
                result.getLayerFp16(result.getAllLayerNames()[2]),
            ).reshape((1, 1, 80, 80))
            scale_x, scale_y = input_info.scales
            (boxes, confidences) = decode_predictions(
                scores, geometry1, geometry2, self.threshold, scale_x, scale_y,
            )
            boxes = non_max_suppression(boxes, probs=confidences)

            image_predictions = []
            for box in boxes:
                if len(box) == 0:
                    continue
                original_w = self.input_width / scale_x
                original_h = self.input_height / scale_y
                points = rotated_rectangle(box)
                points = [
                    Point(x=np.clip(x, 0, original_w), y=np.clip(y, 0, original_h))
                    for x, y in points
                ]
                image_predictions.append(TextPolygon(points=points))
            postprocessed_result.append(image_predictions)
        return postprocessed_result
Example #2
0
    def postprocess(self, predictions):
        postprocessed_result = []

        for result, input_info in zip(predictions[0], predictions[1]):
            scale, pads = input_info.scales[0], input_info.pads
            original_w, original_h = (
                input_info.original_width,
                input_info.original_height,
            )
            h, w = self.input_height, self.input_width
            result = np.concatenate(
                [
                    np.array(result.getLayerFp16(
                        result.getAllLayerNames()[3])).reshape(
                            -1,
                            5,
                        ),
                    np.array(result.getLayerFp16(
                        result.getAllLayerNames()[2])).reshape(
                            -1,
                            8,
                        ),
                    np.array(result.getLayerFp16(
                        result.getAllLayerNames()[1])).reshape(
                            -1,
                            4,
                        ),
                    np.array(result.getLayerFp16(
                        result.getAllLayerNames()[0])).reshape(
                            -1,
                            2,
                        ),
                ],
                axis=-1,
            )

            quads = self.postprocessor.decode_results(result,
                                                      self.threshold).reshape(
                                                          -1,
                                                          4,
                                                          2,
                                                      )
            quads[:, :, 0] = np.clip(
                (quads[:, :, 0] * w - pads[1]) / scale,
                0,
                original_w,
            )
            quads[:, :, 1] = np.clip(
                (quads[:, :, 1] * h - pads[0]) / scale,
                0,
                original_h,
            )

            image_predictions = []
            for quad in quads:
                points = [Point(x=int(x), y=int(y)) for x, y in quad]
                image_predictions.append(TextPolygon(points=points))
            postprocessed_result.append(image_predictions)

        return postprocessed_result
Example #3
0
    def postprocess(self, predictions):
        postprocessed_result = []

        for results, face_bboxes in zip(predictions[0], predictions[1]):
            image_predictions = []
            for result, face_bbox in zip(results, face_bboxes):
                keypoints = np.array(result.getLayerFp16(result.getAllLayerNames()[0]))
                bbox_w, bbox_h = (
                    (face_bbox.x2 - face_bbox.x1),
                    (face_bbox.y2 - face_bbox.y1),
                )
                image_predictions.append(
                    FacialLandmarks(
                        bbox=face_bbox,
                        keypoints=[
                            Point(
                                x=face_bbox.x1 + bbox_w * x,
                                y=face_bbox.y1 + bbox_h * y,
                            )
                            for x, y in zip(keypoints[::2], keypoints[1::2])
                        ],
                    ),
                )
            postprocessed_result.append(image_predictions)

        return postprocessed_result
Example #4
0
    def postprocess(self, predictions):
        postprocessed_result = []

        for result, input_info in zip(predictions[0], predictions[1]):
            (scale_x, scale_y), pads = input_info.scales, input_info.pads
            original_w, original_h = (
                input_info.original_width,
                input_info.original_height,
            )
            h, w = self.input_height, self.input_width
            image_predictions = []
            result = [
                np.array(result.getLayerFp16(self.output_names[0])),
                np.array(result.getLayerFp16(self.output_names[1])).reshape(
                    -1, 18),
            ]
            if result[0].size == 0:
                postprocessed_result.append(image_predictions)
                return postprocessed_result
            boxes, keypoints = self.postprocessor.decode_predictions(
                result,
                self.output_names,
            )
            for box, kps in zip(boxes, keypoints):
                if box[4] > self.threshold:
                    palm_box = BBox(
                        x1=int(
                            np.clip((box[0] * w - pads[1]) / scale_x, 0,
                                    original_w), ),
                        y1=int(
                            np.clip((box[1] * h - pads[0]) / scale_y, 0,
                                    original_h), ),
                        x2=int(
                            np.clip((box[2] * w - pads[1]) / scale_x, 0,
                                    original_w), ),
                        y2=int(
                            np.clip((box[3] * h - pads[0]) / scale_y, 0,
                                    original_h), ),
                        score=float(box[4]),
                        class_name=self.class_names[1],
                    )
                    image_predictions.append(
                        Landmarks(
                            bbox=palm_box,
                            keypoints=[
                                Point(
                                    x=int(
                                        (keypoint[0] * w - pads[1]) / scale_x),
                                    y=int(
                                        (keypoint[1] * h - pads[0]) / scale_y),
                                ) for keypoint in kps
                            ],
                        ), )
            postprocessed_result.append(image_predictions)

        return postprocessed_result
Example #5
0
    def postprocess(self, predictions):
        postprocessed_result = []
        for result, input_info in zip(predictions[0], predictions[1]):
            scale_y, scale_x = input_info.scales
            original_h, original_w = (
                input_info.original_height,
                input_info.original_width,
            )
            hm = np.array(result.getLayerFp16(
                result.getAllLayerNames()[2])).reshape((1, 1, 120, 160), )
            box = np.array(result.getLayerFp16(
                result.getAllLayerNames()[1])).reshape((1, 4, 120, 160), )
            landmark = np.array(
                result.getLayerFp16(result.getAllLayerNames()[0]), ).reshape(
                    (1, 10, 120, 160))
            objs = detect(
                hm=hm,
                box=box,
                landmark=landmark,
                threshold=self.threshold,
                nms_iou=0.5,
            )
            image_predictions = []
            for obj in objs:
                box, confidence, landmark = obj
                image_predictions.append(
                    FacialLandmarks(
                        bbox=BBox(
                            x1=int(np.clip(box[0] / scale_x, 0, original_w)),
                            y1=int(np.clip(box[1] / scale_y, 0, original_h)),
                            x2=int(np.clip(box[2] / scale_x, 0, original_w)),
                            y2=int(np.clip(box[3] / scale_y, 0, original_h)),
                            score=float(confidence),
                            class_name="face",
                        ),
                        keypoints=[
                            Point(
                                x=int(np.clip(x / scale_x, 0, original_w)),
                                y=int(np.clip(y / scale_y, 0, original_h)),
                            ) for x, y in landmark
                        ],
                    ), )
            postprocessed_result.append(image_predictions)

        return postprocessed_result