Beispiel #1
0
def main(
    input_path,
    image_output_path,
    weights_path,
    image_extensions,
    face_threshold,
    plate_threshold,
    write_json,
    keep_exif,
    obfuscation_parameters
):
    download_weights(download_directory=weights_path)

    kernel_size, sigma, box_kernel_size = obfuscation_parameters.split(',')
    obfuscator = Obfuscator(kernel_size=int(kernel_size), sigma=float(sigma), box_kernel_size=int(box_kernel_size))
    detectors = {
        'face': Detector(kind='face', weights_path=get_weights_path(weights_path, kind='face')),
        'plate': Detector(kind='plate', weights_path=get_weights_path(weights_path, kind='plate'))
    }
    detection_thresholds = {
        'face': face_threshold,
        'plate': plate_threshold
    }
    anonymizer = Anonymizer(obfuscator=obfuscator, detectors=detectors)
    anonymizer.anonymize_images(
        input_path=input_path,
        output_path=image_output_path,
        detection_thresholds=detection_thresholds,
        file_types=image_extensions.split(','),
        write_json=write_json,
        keep_exif=keep_exif
    )
Beispiel #2
0
def detect(ids,
           threshold=0.3,
           weights_path="./weights",
           panos_path="/home/www-data/panos",
           output_path="./panosout"):
    print(threshold)
    detectors = {
        'face':
        Detector(kind='face',
                 weights_path=get_weights_path(weights_path, kind='face')),
        'plate':
        Detector(kind='face',
                 weights_path=get_weights_path(weights_path, kind='plate'))
    }
    detection_thresholds = {'face': threshold, 'plate': threshold}

    if not os.path.exists(f"{output_path}/{threshold}"):
        os.makedirs(f"{output_path}/{threshold}")

    for id in ids:
        print(f"Loading {panos_path}/{id}.jpg")
        with Image.open(f"{panos_path}/{id}.jpg") as image:
            npimage = numpy.array(image.convert('RGB'))
            detected_boxes = []
            for kind, detector in detectors.items():
                new_boxes = detector.detect(npimage,
                                            detection_thresholds[kind])
                detected_boxes.extend(new_boxes)

            draw = ImageDraw.Draw(image)

            for box in detected_boxes:
                print(
                    f"{box.x_min} {box.y_min} {box.x_max} {box.y_max} {box.score} {box.kind}"
                )
                #            draw.rectangle([box.x_min, box.y_min, box.x_max, box.y_max],outline=(255, 0, 0), width=5)

                cropbox = (int(box.x_min), int(box.y_min), int(box.x_max),
                           int(box.y_max))
                crop = image.crop(cropbox)
                for i in range(10):
                    crop = crop.filter(ImageFilter.BLUR)
                image.paste(crop, cropbox)

            image.save(f"{output_path}/{threshold}/{id}.jpg", "JPEG")
    def test_it_detects_obvious_faces(tmp_path):
        weights_directory = tmp_path / 'weights'
        face_weights_path = get_weights_path(weights_directory, kind='face')
        download_weights(weights_directory)

        detector = Detector(kind='face', weights_path=face_weights_path)
        np_image = load_np_image('./test/detection/face_test_image.jpg')

        left_face = Box(x_min=267, y_min=64, x_max=311, y_max=184, score=0.0, kind='face')
        right_face = Box(x_min=369, y_min=68, x_max=420, y_max=152, score=0.0, kind='face')

        boxes = detector.detect(np_image, detection_threshold=0.2)

        assert len(boxes) >= 2
        for box in boxes:
            assert box.score >= 0.2
        assert boxes[0].score >= 0.5 and boxes[1].score >= 0.5
        assert ((box_covers_box(boxes[0], left_face) and box_covers_box(boxes[1], right_face)) or
                (box_covers_box(boxes[1], left_face) and box_covers_box(boxes[0], right_face)))