def create_detector_from_file(cfg_file):
    try:
        with open(cfg_file, 'r') as fp:
            config = json.load(fp)

        cropper_cfg = config['cropper']
        cropper = Cropper(cropper_cfg['crop_percent'],
                          cropper_cfg['force_square'])

        localizer_cfg = config['localizer']
        localizer = Localizer(localizer_cfg['model'],
                              localizer_cfg['weights'],
                              localizer_cfg['threshold'])

        classifier_cfg = config['classifier']
        classifier = Classifier(classifier_cfg['model'],
                                classifier_cfg['weights'],
                                classifier_cfg['labels'],
                                classifier_cfg['threshold'],
                                classifier_cfg['skip_classes'])

        detection_pipeline = DetectionPipeline(localizer, cropper, classifier)

        detector_cfg = config['detector']

        detector = Detector(detection_pipeline,
                            detector_cfg['images_path'],
                            detector_cfg['sounds_path'])

        return detector

    except:
        raise FormatNotSupportedException('The configuration file does not seems to be valid')
Пример #2
0
    def test_detector_image(self):
        # Create Localizer
        cfg_path = './data/yolo/full/trafficsigns.cfg'
        weights_path = './data/yolo/full/trafficsigns.weights'
        threshold = 0.24

        localizer = Localizer(cfg_path, weights_path, threshold, gpu=0.0)

        # Create cropper
        crop_percent = 0.25
        force_square = True

        cropper = Cropper(crop_percent, force_square)

        # Create classifier
        model_path = './data/classifier/trafficsigns.json'
        weights_path = './data/classifier/trafficsigns.h5'
        labels_path = './data/classifier/classes.txt'
        threshold = 0.5

        classifier = Classifier(model_path, weights_path, labels_path,
                                threshold)

        # Create detection pipeline
        detection_pipeline = DetectionPipeline(localizer, cropper, classifier)

        # Create detector
        images_path = './data/classifier/classes/'
        sounds_path = './data/sounds/'

        detector = Detector(detection_pipeline, images_path, sounds_path)

        # Detect on image
        img = cv2.imread('./tests/data/test.png')

        image, detections = detector.detect_image(img,
                                                  show_confidence=True,
                                                  return_image=True)

        true_detections = [{
            'class_id': 15,
            'coordinates': [1434, 456, 1590, 612],
            'label': 'max-60',
            'confidence': 1.0
        }]

        assert detections == true_detections
Пример #3
0
    def test_detector_video(self):
        # Create Localizer
        cfg_path = './data/yolo/full/trafficsigns.cfg'
        weights_path = './data/yolo/full/trafficsigns.weights'
        threshold = 0.24

        localizer = Localizer(cfg_path, weights_path, threshold, gpu=0.0)

        # Create cropper
        crop_percent = 0.25
        force_square = True

        cropper = Cropper(crop_percent, force_square)

        # Create classifier
        model_path = './data/classifier/trafficsigns.json'
        weights_path = './data/classifier/trafficsigns.h5'
        labels_path = './data/classifier/classes.txt'
        threshold = 0.5

        classifier = Classifier(model_path, weights_path, labels_path,
                                threshold)

        # Create detection pipeline
        detection_pipeline = DetectionPipeline(localizer, cropper, classifier)

        # Create detector
        images_path = './data/classifier/classes/'
        sounds_path = './data/sounds/'

        detector = Detector(detection_pipeline, images_path, sounds_path)

        # Detect on video
        video_feed = './tests/data/test.mp4'

        output_mp4 = './tests/data/test_output.mp4'
        output_csv = './tests/data/test_output.csv'

        exit_code = detector.detect_video_feed(video_feed,
                                               output=output_mp4,
                                               output_csv=output_csv,
                                               show_confidence=True,
                                               sound_notifications=True)

        assert exit_code == True
Пример #4
0
    def test_detector_image_output_json(self):
        # Create Localizer
        cfg_path = './data/yolo/full/trafficsigns.cfg'
        weights_path = './data/yolo/full/trafficsigns.weights'
        threshold = 0.24

        localizer = Localizer(cfg_path, weights_path, threshold, gpu=0.0)

        # Create cropper
        crop_percent = 0.25
        force_square = True

        cropper = Cropper(crop_percent, force_square)

        # Create classifier
        model_path = './data/classifier/trafficsigns.json'
        weights_path = './data/classifier/trafficsigns.h5'
        labels_path = './data/classifier/classes.txt'
        threshold = 0.5

        classifier = Classifier(model_path, weights_path, labels_path,
                                threshold)

        # Create detection pipeline
        detection_pipeline = DetectionPipeline(localizer, cropper, classifier)

        # Create detector
        images_path = './data/classifier/classes/'
        sounds_path = './data/sounds/'

        detector = Detector(detection_pipeline, images_path, sounds_path)

        # Detect on image
        img = cv2.imread('./tests/data/test.png')

        output_filename = './tests/data/detector_image.json'
        detections = detector.detect_image(img, output=output_filename)

        assert os.path.exists(output_filename) == True
    def test_detector_pipeline(self):
        # Create Localizer
        cfg_path = './data/yolo/full/trafficsigns.cfg'
        weights_path = './data/yolo/full/trafficsigns.weights'
        threshold = 0.24

        localizer = Localizer(cfg_path, weights_path, threshold, gpu=0.0)

        # Create cropper
        crop_percent = 0.25
        force_square = True

        cropper = Cropper(crop_percent, force_square)

        # Create classifier
        model_path = './data/classifier/trafficsigns.json'
        weights_path = './data/classifier/trafficsigns.h5'
        labels_path = './data/classifier/classes.txt'
        threshold = 0.5

        classifier = Classifier(model_path, weights_path, labels_path, threshold)

        # Create detection pipeline
        pipeline = DetectionPipeline(localizer, cropper, classifier)


        # Detect on image
        img = cv2.imread('./tests/data/test.png')

        detections = pipeline.detect_objects_in_image(img)

        print(detections)



        assert 0 == 0