Пример #1
0
def usage_demo():
    print('-'*88)
    print("Welcome to the Amazon Rekognition face collection demo!")
    print('-'*88)

    logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')

    rekognition_client = boto3.client('rekognition')
    images = [
        RekognitionImage.from_file(
            '.media/pexels-agung-pandit-wiguna-1128316.jpg', rekognition_client,
            image_name='sitting'),
        RekognitionImage.from_file(
            '.media/pexels-agung-pandit-wiguna-1128317.jpg', rekognition_client,
            image_name='hopping'),
        RekognitionImage.from_file(
            '.media/pexels-agung-pandit-wiguna-1128318.jpg', rekognition_client,
            image_name='biking')]

    collection_mgr = RekognitionCollectionManager(rekognition_client)
    collection = collection_mgr.create_collection('doc-example-collection-demo')
    print(f"Created collection {collection.collection_id}:")
    pprint(collection.describe_collection())

    print("Indexing faces from three images:")
    for image in images:
        collection.index_faces(image, 10)
    print("Listing faces in collection:")
    faces = collection.list_faces(10)
    for face in faces:
        pprint(face.to_dict())
    input("Press Enter to continue.")

    print(f"Searching for faces in the collection that match the first face in the "
          f"list (Face ID: {faces[0].face_id}.")
    found_faces = collection.search_faces(faces[0].face_id, 80, 10)
    print(f"Found {len(found_faces)} matching faces.")
    for face in found_faces:
        pprint(face.to_dict())
    input("Press Enter to continue.")

    print(f"Searching for faces in the collection that match the largest face in "
          f"{images[0].image_name}.")
    image_face, match_faces = collection.search_faces_by_image(images[0], 80, 10)
    print(f"The largest face in {images[0].image_name} is:")
    pprint(image_face.to_dict())
    print(f"Found {len(match_faces)} matching faces.")
    for face in match_faces:
        pprint(face.to_dict())
    input("Press Enter to continue.")

    collection.delete_collection()
    print('Thanks for watching!')
    print('-'*88)
def test_index_faces(make_stubber, make_faces, error_code):
    rekognition_client = boto3.client('rekognition')
    rekognition_stubber = make_stubber(rekognition_client)
    image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client)
    max_faces = 3
    indexed_faces = [
        RekognitionFace(face)
        for face in make_faces(3, has_details=True, is_index=True)
    ]
    unindexed_faces = [RekognitionFace(face) for face in make_faces(4)]
    collection = make_collection(rekognition_client)

    rekognition_stubber.stub_index_faces(collection.collection_id,
                                         image,
                                         max_faces,
                                         indexed_faces,
                                         unindexed_faces,
                                         error_code=error_code)

    if error_code is None:
        got_indexed_faces, got_unindexed_faces = collection.index_faces(
            image, max_faces)
        assert ([face.to_dict() for face in indexed_faces
                 ] == [face.to_dict() for face in got_indexed_faces])
        assert ([face.to_dict() for face in unindexed_faces
                 ] == [face.to_dict() for face in got_unindexed_faces])
    else:
        with pytest.raises(ClientError) as exc_info:
            collection.index_faces(image, max_faces)
        assert exc_info.value.response['Error']['Code'] == error_code
def test_search_face_by_image(make_stubber, make_faces, error_code):
    rekognition_client = boto3.client('rekognition')
    rekognition_stubber = make_stubber(rekognition_client)
    collection = make_collection(rekognition_client)
    image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client)
    threshold = 80
    max_faces = 3
    image_face = RekognitionFace(make_faces(1)[0])
    faces = [
        RekognitionFace({
            'FaceIndex': f'face-{index}',
            'ImageIndex': f'image-{index}'
        }) for index in range(0, 3)
    ]

    rekognition_stubber.stub_search_faces_by_image(collection.collection_id,
                                                   image,
                                                   threshold,
                                                   max_faces,
                                                   image_face,
                                                   faces,
                                                   error_code=error_code)

    if error_code is None:
        got_image_face, got_faces = collection.search_faces_by_image(
            image, threshold, max_faces)
        assert image_face.to_dict() == got_image_face.to_dict()
        assert ([face.to_dict()
                 for face in faces] == [face.to_dict() for face in got_faces])
    else:
        with pytest.raises(ClientError) as exc_info:
            collection.search_faces_by_image(image, threshold, max_faces)
        assert exc_info.value.response['Error']['Code'] == error_code
def test_detect_faces(make_stubber, make_faces, error_code):
    rekognition_client = boto3.client('rekognition')
    rekognition_stubber = make_stubber(rekognition_client)
    image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client)
    faces = [RekognitionFace(face) for face in make_faces(3, True)]

    rekognition_stubber.stub_detect_faces(image.image,
                                          faces,
                                          error_code=error_code)

    if error_code is None:
        got_faces = image.detect_faces()
        assert ([face.to_dict()
                 for face in faces] == [face.to_dict() for face in got_faces])
    else:
        with pytest.raises(ClientError) as exc_info:
            image.detect_faces()
        assert exc_info.value.response['Error']['Code'] == error_code
def test_detect_text(make_stubber, make_texts, error_code):
    rekognition_client = boto3.client('rekognition')
    rekognition_stubber = make_stubber(rekognition_client)
    image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client)
    texts = [RekognitionText(text) for text in make_texts(3)]

    rekognition_stubber.stub_detect_text(image.image,
                                         texts,
                                         error_code=error_code)

    if error_code is None:
        got_texts = image.detect_text()
        assert ([text.to_dict()
                 for text in texts] == [text.to_dict() for text in got_texts])
    else:
        with pytest.raises(ClientError) as exc_info:
            image.detect_text()
        assert exc_info.value.response['Error']['Code'] == error_code
def test_detect_labels(make_stubber, make_labels, error_code):
    rekognition_client = boto3.client('rekognition')
    rekognition_stubber = make_stubber(rekognition_client)
    image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client)
    labels = [RekognitionLabel(label) for label in make_labels(3)]
    max_labels = 3

    rekognition_stubber.stub_detect_labels(image.image,
                                           max_labels,
                                           labels,
                                           error_code=error_code)

    if error_code is None:
        got_labels = image.detect_labels(max_labels)
        assert ([label.to_dict() for label in labels
                 ] == [label.to_dict() for label in got_labels])
    else:
        with pytest.raises(ClientError) as exc_info:
            image.detect_labels(max_labels)
        assert exc_info.value.response['Error']['Code'] == error_code
def test_compare_faces(make_stubber, make_faces, error_code):
    rekognition_client = boto3.client('rekognition')
    rekognition_stubber = make_stubber(rekognition_client)
    source_image = RekognitionImage(TEST_IMAGE, 'source-image',
                                    rekognition_client)
    target_image = RekognitionImage(TEST_IMAGE, 'target-image',
                                    rekognition_client)
    matches = [RekognitionFace(face) for face in make_faces(1)]
    unmatches = [RekognitionFace(face) for face in make_faces(2)]
    similarity = 80

    rekognition_stubber.stub_compare_faces(source_image.image,
                                           target_image.image,
                                           similarity,
                                           matches,
                                           unmatches,
                                           error_code=error_code)

    if error_code is None:
        got_matches, got_unmatches = source_image.compare_faces(
            target_image, similarity)
        assert ([face.to_dict() for face in matches
                 ] == [face.to_dict() for face in got_matches])
        assert ([face.to_dict() for face in unmatches
                 ] == [face.to_dict() for face in got_unmatches])
    else:
        with pytest.raises(ClientError) as exc_info:
            source_image.compare_faces(target_image, similarity)
        assert exc_info.value.response['Error']['Code'] == error_code
def test_recognize_celebrities(make_stubber, make_faces, error_code):
    rekognition_client = boto3.client('rekognition')
    rekognition_stubber = make_stubber(rekognition_client)
    image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client)
    celebrities = [
        RekognitionCelebrity(face) for face in make_faces(3, is_celebrity=True)
    ]
    normals = [RekognitionFace(face) for face in make_faces(2)]

    rekognition_stubber.stub_recognize_celebrities(image.image,
                                                   celebrities,
                                                   normals,
                                                   error_code=error_code)

    if error_code is None:
        got_celebrities, got_normals = image.recognize_celebrities()
        assert ([celeb.to_dict() for celeb in celebrities
                 ] == [celeb.to_dict() for celeb in got_celebrities])
        assert ([normal.to_dict() for normal in normals
                 ] == [normal.to_dict() for normal in got_normals])
    else:
        with pytest.raises(ClientError) as exc_info:
            image.recognize_celebrities()
        assert exc_info.value.response['Error']['Code'] == error_code