Exemplo n.º 1
0
    def inference(flow_id: str, frame: object) -> PyDetectionBox:
        """
        Returns a list of detection results, each a dictionary of object info
        """
        image = frame_data_2_image(frame, input_width, input_height)
        set_input_tensor(interpreter, image)
        interpreter.invoke()

        # Get all output details
        boxes = get_output_tensor(interpreter, 0)
        classes = get_output_tensor(interpreter, 1)
        scores = get_output_tensor(interpreter, 2)
        count = int(get_output_tensor(interpreter, 3))

        result = PyDetectionBox(frame_id=frame.frame_id, engine_id='tflite')
        for i in range(count):
            result.add_box(category_id=classes[i],
                           category_label=labels.get(classes[i], ''),
                           x1=boxes[i][1],
                           y1=boxes[i][0],
                           x2=boxes[i][3],
                           y2=boxes[i][2],
                           probability=float(scores[i]))

        return flow_id, result
Exemplo n.º 2
0
    def inference(flow_id: str, frame: object) -> PyDetectionBox:
        """
        Returns a list of detection results, each a dictionary of object info
        """
        image = frame_data_2_image(frame, frame.width, frame.height)
        image_t = transform(image)
        image_t = torch.unsqueeze(image_t, 0)

        output = engine(image_t)

        # Get all output details
        boxes = output[0]['boxes']
        classes = output[0]['labels']
        scores = output[0]['scores']

        result = PyDetectionBox(frame_id=frame.frame_id, engine_id='tflite')
        for i, box in enumerate(boxes):
            result.add_box(category_id=classes[i],
                           category_label=labels.get(classes[i], ''),
                           x1=box[0],
                           y1=box[1],
                           x2=box[2],
                           y2=box[3],
                           probability=float(scores[i]))

        return flow_id, result
    def inference(flow_id: str, frame: object) -> Tuple[str, PyDetectionBox]:
        image = frame_data_2_np_array(frame)
        outputs = predictor(image)
        predictions = outputs["instances"].to("cpu")
        boxes = get_boxes(predictions)
        masks = get_masks(predictions, frame.height, frame.width)
        scores = predictions.scores if predictions.has("scores") else None
        classes = predictions.pred_classes if predictions.has(
            "pred_classes") else None
        box_labels = get_labels(classes, scores, labels)

        result = PyDetectionBox(frame_id=frame.frame_id,
                                engine_id='detectron2')
        if boxes is not None or masks is not None:
            num_predictions = len(boxes) if boxes is not None else len(masks)
            for idx in range(num_predictions):
                x1, y1, x2, y2 = boxes[idx] if boxes is not None else masks[
                    idx].bbox()
                result.add_box(category_id=classes[idx],
                               category_label=box_labels[idx],
                               x1=x1,
                               y1=y1,
                               x2=x2,
                               y2=x2,
                               probability=float(scores[idx]))

        return flow_id, result
    def inference(flow_id: str, frame: object) -> PyDetectionBox:
        """
        Returns a list of detection results, each a dictionary of object info
        """
        image = frame_data_2_np_array(frame)
        input_tensor = get_input_tensor(image)
        detections, predictions_dict, shapes = interpreter(input_tensor)
        boxes = detections['detection_boxes'][0].numpy()
        classes = detections['detection_classes'][0].numpy()
        scores = detections['dection_scores'][0].numpy()

        result = PyDetectionBox(frame_id=frame.frame_id,
                                engine_id='tf-object-detection')

        for i, box in enumerate(boxes):
            ymin, xmin, ymax, xmax = box.tolist()
            probability = float(scores[i])
            class_id = classes[i]
            class_label = labels.get(class_id, 'N/A')
            result.add_box(category_id=class_id,
                           category_label=class_label,
                           x1=xmin,
                           y1=ymin,
                           x2=xmax,
                           y2=ymax,
                           probability=probability)

        return flow_id, result
def add_detection_node_2_detection_box(node: DetectionNode,
                                       container: PyDetectionBox) -> None:
    container.add_box(category_label=node.name,
                      x1=node.bbox.x1,
                      y1=node.bbox.y1,
                      x2=node.bbox.x2,
                      y2=node.bbox.y2)
    def score(flow_id: str, frame: object) -> Tuple[str, PyDetectionBox]:
        img = frame_data_2_np_array(frame)
        if capsule.input_type.size is NodeDescription.Size.NONE:
            input_node = None
        else:
            input_node = DetectionNode(name='',
                                       coords=[[0, 0], [frame.width, 0],
                                               [frame.width, frame.height],
                                               [0, frame.height]])

        if capsule.input_type.size is NodeDescription.Size.ALL:
            input_node = [input_node]

        result = capsule.process_frame(frame=img,
                                       detection_node=input_node,
                                       options=capsule.default_options,
                                       state=capsule.stream_state())
        detection_box = PyDetectionBox(frame_id=frame.frame_id,
                                       engine_id='vision_capsules')
        if isinstance(result, list):
            for node in result:
                add_detection_node_2_detection_box(node, detection_box)
        elif isinstance(result, DetectionNode):
            add_detection_node_2_detection_box(result, detection_box)

        return flow_id, detection_box
    def inference(flow_id: str, frame: object):
        image = frame_data_2_bytes(frame, width, height)
        darknet.copy_image_from_bytes(darknet_image, image)
        detections = darknet.detect_image(network,
                                          class_names,
                                          darknet_image,
                                          thresh=thresh)
        result = PyDetectionBox(frame_id=frame.frame_id, engine_id='darknet')
        for label, confidence, bbox in detections:
            left, top, right, bottom = darknet.bbox2points(bbox)
            result.add_box(category_id=labels_rev.get(label, ''),
                           category_label=label,
                           x1=left,
                           y1=top,
                           x2=right,
                           y2=left,
                           probability=float(confidence))
        darknet.free_image(darknet_image)

        return flow_id, result
Exemplo n.º 8
0
    def score(flow_id: str, frame: object) -> Tuple[str, PyDetectionBox]:
        img = frame_data_2_np_array(frame)
        frame_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        frame_gray = cv2.equalizeHist(frame_gray)

        detection_box = PyDetectionBox(frame_id=frame.frame_id,
                                       engine_id='opencv-haar-cascades')

        # detection
        detections = cascade.detectMultiScale(frame_gray)

        for (x, y, w, h) in detections:
            detection_box.add_box(category_label=primary_label,
                                  x1=x,
                                  y1=y,
                                  x2=x + w,
                                  y2=y + h)
            if secondary_cascade is not None:
                roi = frame_gray[y:y + h, x:x + w]
                secondary_detections = secondary_cascade.detectMultiScale(roi)
                for (x_s, y_s, w_s, h_s) in secondary_detections:
                    detection_box.add_box(category_label=secondary_label,
                                          x1=x_s,
                                          y1=y_s,
                                          x2=x_s + w_s,
                                          y2=y_s + h_s)

        return flow_id, detection_box