Exemplo n.º 1
0
 def test_invalid_inputs(self):
     """Invalid inputs"""
     bad_args = [
         None, 0,
         ScoredRect(Rect(0, 1, 0, 1), 0.5), [Rect(0, 1, 0, 1)]
     ]
     self._test_bad_args(bad_args)
    def test_typical_detection(self, mock_chunker, mock_tf_detector,
                               mock_deduper):
        """Validating expected behavior on typical mocks"""
        chunk = np.zeros((1024, 1024, 3), dtype=np.uint8)
        chunks = [((0, 0), chunk), ((512, 0), chunk), ((1024, 0), chunk)]
        tf_dets = [
            [ScoredRect(Rect(0, 512, 0, 512), 0.5)],
            [ScoredRect(Rect(0, 512, 0, 512), 0.0)],
            [
                ScoredRect(Rect(0, 10, 0, 10), 0.5),
                ScoredRect(Rect(0, 512, 0, 512), 0.4)
            ],
        ]

        ObjectDetectorTest._set_chunker_outs(mock_chunker, chunks)
        ObjectDetectorTest._set_tf_detector_outs(mock_tf_detector,
                                                 self.dummy_resolution,
                                                 tf_dets)
        ObjectDetectorTest._set_deduper_outs(mock_deduper, lambda x: x)

        detector = ObjectDetector(self.dummy_model_path)
        detections = detector.run(self.dummy_image)
        expected_output = [
            ScoredRect(Rect(0, 512, 0, 512), 0.5),
            ScoredRect(Rect(1024, 1536, 0, 512), 0.4),
        ]
        self.assertEqual(detections, expected_output)

        self._validate_mock_calls(mock_chunker, mock_tf_detector, mock_deduper)
    def _build_scored_rects(rects, scores):
        # Converts detection arrays to ScoredRects
        scored_rects = []
        for (rect, score) in zip(rects, scores):
            rect_params = tuple(map(int, map(round, rect)))
            try:
                scored_rect = ScoredRect(Rect(*rect_params), float(score))
            except DatastructsValueError:
                pass
            else:
                scored_rects.append(scored_rect)

        return scored_rects
Exemplo n.º 4
0
    def setUp(self):
        """Define inputs used for multiple individual tests"""
        self.single_box = [ScoredRect(Rect(0, 10, 0, 20), 0.0)]

        self.ground_truths = [
            ScoredRect(Rect(0, 10, 0, 10), 0.5),
            ScoredRect(Rect(10, 20, 20, 30), 0.5),
            ScoredRect(Rect(-40, -30, -20, -10), 0.5),
        ]
        self.detections = [
            ScoredRect(Rect(0, 10, 0, 10), 0.5),
            ScoredRect(Rect(10, 20, 20, 30), 0.5),
            ScoredRect(Rect(10, 20, 20, 30), 0.5),
            ScoredRect(Rect(100, 110, 20, 30), 0.5),
        ]
Exemplo n.º 5
0
    def setUp(self):
        """Define lists of automatically generated inputs"""
        roi_size = 5
        num_annots = 10
        anchor = ScoredRect(Rect(0, roi_size, 0, roi_size), 0.5)

        self.ground_truths = MetricsComputerAutomatedTest._build_annots(
            anchor, (0, roi_size), num_annots)
        self.true_dets = [det for det in self.ground_truths]
        self.false_dets = [
            det.shifted((roi_size, 0)) for det in self.ground_truths
        ]
        self.mixed_dets = self.true_dets[::2] + self.false_dets[1::2]
        self.duplicate_true_dets = self.true_dets + self.true_dets
def main():
    """Draws out bounding boxes around all detections in a dataset"""
    with open(DATASET_PATH) as dataset_handle:
        dataset_json = json.load(dataset_handle)

    if not os.path.exists(OUTPUT_FOLDER):
        os.makedirs(OUTPUT_FOLDER)

    for dataset_elem in dataset_json:
        path = dataset_elem["path"]
        detections = dataset_elem["detections"]

        image = cv2.imread(path)
        for detection in detections:
            scored_rect = ScoredRect.from_dict(detection)
            _annotate_image(image, scored_rect)

        basename = os.path.basename(path)
        output_image_path = os.path.join(OUTPUT_FOLDER, basename)
        cv2.imwrite(output_image_path, image)
Exemplo n.º 7
0
def main():
    """Runs ObjectDetector on each dataset element and grades"""
    with open(GROUNDTRUTH_PATH, "rt") as groundtruth_handle:
        groundtruth_json = json.load(groundtruth_handle)

    object_detector = ObjectDetector(MODEL_PATH)
    metrics_computer = MetricsComputer()
    confidence_scorer = ConfidenceScorer()

    for groundtruth_elem in groundtruth_json:
        groundtruth_annots_dict = groundtruth_elem["detections"]
        path = groundtruth_elem["path"]

        groundtruth_annots = [
            ScoredRect.from_dict(annot_dict)
            for annot_dict in groundtruth_annots_dict
        ]

        image = scipy.misc.imread(path)
        detection_annots = object_detector.run(image)
        metrics = metrics_computer.run(detection_annots, groundtruth_annots)
        confidence = confidence_scorer.run(**metrics)

        _print_grades(path, metrics, confidence)