def test_expand_and_crop(self): crop_percent = 0.1 force_square = True test_image_path = 'tests/data/test.jpg' image = cv2.imread(test_image_path) h, w, _ = image.shape x1 = 100 y1 = 50 width = 200 height = 300 original_coordinates = (x1, y1, x1 + width, y1 + height) crp = Cropper(crop_percent, force_square) new_coords, crop_image = crp.expand_and_crop(image, original_coordinates) crop_h, crop_w, _ = crop_image.shape assert crop_h == 330 assert crop_w == 330
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')
def test_expand_coordinates_top_side(self): crop_percent = 0.1 force_square = False image_size = (1920, 1080) x1 = 500 y1 = 0 width = 200 height = 100 original_coordinates = (x1, y1, x1 + width, y1 + height) crp = Cropper(crop_percent, force_square) expanded_coordinates = crp.expand_coordinates(original_coordinates, image_size) expected_coordinates = (400, 0, 800, 250) assert expanded_coordinates == expected_coordinates
def test_expand_coordinates_right_side(self): crop_percent = 0.1 force_square = False image_size = (600, 800) x1 = 500 y1 = 300 width = 100 height = 200 original_coordinates = (x1, y1, x1 + width, y1 + height) crp = Cropper(crop_percent, force_square) expanded_coordinates = crp.expand_coordinates(original_coordinates, image_size) expected_coordinates = (350, 200, 600, 600) assert expanded_coordinates == expected_coordinates
def test_expand_coordinates_square_bottom_side(self): crop_percent = 0.1 force_square = True image_size = (1920, 1080) x1 = 500 y1 = 1000 width = 100 height = 80 original_coordinates = (x1, y1, x1 + width, y1 + height) crp = Cropper(crop_percent, force_square) expanded_coordinates = crp.expand_coordinates(original_coordinates, image_size) expected_coordinates = (495, 985, 605, 1080) assert expanded_coordinates == expected_coordinates
def test_expand_coordinates_square_left_side(self): crop_percent = 0.1 force_square = True image_size = (1920, 1080) x1 = 0 y1 = 500 width = 100 height = 200 original_coordinates = (x1, y1, x1 + width, y1 + height) crp = Cropper(crop_percent, force_square) expanded_coordinates = crp.expand_coordinates(original_coordinates, image_size) expected_coordinates = (0, 490, 160, 710) assert expanded_coordinates == expected_coordinates
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
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
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