Skip to content

Instantly share code, notes, and snippets.

@dan1901
Forked from alexcasalboni/amazon-rekognition.md
Created February 3, 2020 07:07
Show Gist options
  • Save dan1901/0ba26e76cee027b7cc2e15da7cc742ea to your computer and use it in GitHub Desktop.
Save dan1901/0ba26e76cee027b7cc2e15da7cc742ea to your computer and use it in GitHub Desktop.

Revisions

  1. @alexcasalboni alexcasalboni revised this gist Dec 22, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions test-2-detect-faces.py
    Original file line number Diff line number Diff line change
    @@ -19,13 +19,13 @@ def detect_faces(bucket, key, attributes=['ALL'], region="eu-west-1"):

    for face in detect_faces(BUCKET, KEY):
    print "Face ({Confidence}%)".format(**face)
    # emotions
    # emotions
    for emotion in face['Emotions']:
    print " {Type} : {Confidence}%".format(**emotion)
    # quality
    # quality
    for quality, value in face['Quality'].iteritems():
    print " {quality} : {value}".format(quality=quality, value=value)
    # facial features
    # facial features
    for feature, data in face.iteritems():
    if feature not in FEATURES_BLACKLIST:
    print " {feature}({data[Value]}) : {data[Confidence]}%".format(feature=feature, data=data)
  2. @alexcasalboni alexcasalboni revised this gist Dec 19, 2016. 2 changed files with 11 additions and 11 deletions.
    12 changes: 6 additions & 6 deletions test-1-detect-labels.py
    Original file line number Diff line number Diff line change
    @@ -19,15 +19,15 @@ def detect_labels(bucket, key, max_labels=10, min_confidence=90, region="eu-west


    for label in detect_labels(BUCKET, KEY):
    print "{Name} - {Confidence}".format(**label)
    print "{Name} - {Confidence}%".format(**label)

    """
    Expected output:
    People - 99.2436447144
    Person - 99.2436447144
    Human - 99.2351226807
    Clothing - 96.7797698975
    Suit - 96.7797698975
    People - 99.2436447144%
    Person - 99.2436447144%
    Human - 99.2351226807%
    Clothing - 96.7797698975%
    Suit - 96.7797698975%
    """
    10 changes: 5 additions & 5 deletions test-3-compare-faces.py
    Original file line number Diff line number Diff line change
    @@ -27,18 +27,18 @@ def compare_faces(bucket, key, bucket_target, key_target, threshold=80, region="
    source_face, matches = compare_faces(BUCKET, KEY_SOURCE, BUCKET, KEY_TARGET)

    # the main source face
    print "Source Face ({Confidence})".format(**source_face)
    print "Source Face ({Confidence}%)".format(**source_face)

    # one match for each target face
    for match in matches:
    print "Target Face ({Confidence})".format(**match['Face'])
    print "Target Face ({Confidence}%)".format(**match['Face'])
    print " Similarity : {}%".format(match['Similarity'])

    """
    Expected output:
    Source Face (99.945602417)
    Target Face (99.9963378906)
    Similarity : 89.0
    Source Face (99.945602417%)
    Target Face (99.9963378906%)
    Similarity : 89.0%
    """
  3. @alexcasalboni alexcasalboni revised this gist Dec 19, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions amazon-rekognition.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Amazon Rekognition - Python Code Samples

    * Labels Detection
    * Faces Detection
    * Faces Comparison
    * Faces Indexing
    * Faces Search
    1. Labels Detection
    2. Faces Detection
    3. Faces Comparison
    4. Faces Indexing
    5. Faces Search
  4. @alexcasalboni alexcasalboni created this gist Dec 19, 2016.
    7 changes: 7 additions & 0 deletions amazon-rekognition.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Amazon Rekognition - Python Code Samples

    * Labels Detection
    * Faces Detection
    * Faces Comparison
    * Faces Indexing
    * Faces Search
    33 changes: 33 additions & 0 deletions test-1-detect-labels.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import boto3

    BUCKET = "amazon-rekognition"
    KEY = "test.jpg"

    def detect_labels(bucket, key, max_labels=10, min_confidence=90, region="eu-west-1"):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.detect_labels(
    Image={
    "S3Object": {
    "Bucket": bucket,
    "Name": key,
    }
    },
    MaxLabels=max_labels,
    MinConfidence=min_confidence,
    )
    return response['Labels']


    for label in detect_labels(BUCKET, KEY):
    print "{Name} - {Confidence}".format(**label)

    """
    Expected output:
    People - 99.2436447144
    Person - 99.2436447144
    Human - 99.2351226807
    Clothing - 96.7797698975
    Suit - 96.7797698975
    """
    51 changes: 51 additions & 0 deletions test-2-detect-faces.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    import boto3

    BUCKET = "amazon-rekognition"
    KEY = "test.jpg"
    FEATURES_BLACKLIST = ("Landmarks", "Emotions", "Pose", "Quality", "BoundingBox", "Confidence")

    def detect_faces(bucket, key, attributes=['ALL'], region="eu-west-1"):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.detect_faces(
    Image={
    "S3Object": {
    "Bucket": bucket,
    "Name": key,
    }
    },
    Attributes=attributes,
    )
    return response['FaceDetails']

    for face in detect_faces(BUCKET, KEY):
    print "Face ({Confidence}%)".format(**face)
    # emotions
    for emotion in face['Emotions']:
    print " {Type} : {Confidence}%".format(**emotion)
    # quality
    for quality, value in face['Quality'].iteritems():
    print " {quality} : {value}".format(quality=quality, value=value)
    # facial features
    for feature, data in face.iteritems():
    if feature not in FEATURES_BLACKLIST:
    print " {feature}({data[Value]}) : {data[Confidence]}%".format(feature=feature, data=data)

    """
    Expected output:
    Face (99.945602417%)
    SAD : 14.6038293839%
    HAPPY : 12.3668470383%
    DISGUSTED : 3.81404161453%
    Sharpness : 10.0
    Brightness : 31.4071826935
    Eyeglasses(False) : 99.990234375%
    Sunglasses(False) : 99.9500656128%
    Gender(Male) : 99.9291687012%
    EyesOpen(True) : 99.9609146118%
    Smile(False) : 99.8329467773%
    MouthOpen(False) : 98.3746566772%
    Mustache(False) : 98.7549591064%
    Beard(False) : 92.758682251%
    """
    44 changes: 44 additions & 0 deletions test-3-compare-faces.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    import boto3

    BUCKET = "amazon-rekognition"
    KEY_SOURCE = "test.jpg"
    KEY_TARGET = "target.jpg"

    def compare_faces(bucket, key, bucket_target, key_target, threshold=80, region="eu-west-1"):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.compare_faces(
    SourceImage={
    "S3Object": {
    "Bucket": bucket,
    "Name": key,
    }
    },
    TargetImage={
    "S3Object": {
    "Bucket": bucket_target,
    "Name": key_target,
    }
    },
    SimilarityThreshold=threshold,
    )
    return response['SourceImageFace'], response['FaceMatches']


    source_face, matches = compare_faces(BUCKET, KEY_SOURCE, BUCKET, KEY_TARGET)

    # the main source face
    print "Source Face ({Confidence})".format(**source_face)

    # one match for each target face
    for match in matches:
    print "Target Face ({Confidence})".format(**match['Face'])
    print " Similarity : {}%".format(match['Similarity'])

    """
    Expected output:
    Source Face (99.945602417)
    Target Face (99.9963378906)
    Similarity : 89.0
    """
    42 changes: 42 additions & 0 deletions test-4-index-faces.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    import boto3

    BUCKET = "amazon-rekognition"
    KEY = "test.jpg"
    IMAGE_ID = KEY # S3 key as ImageId
    COLLECTION = "my-collection-id"

    # Note: you have to create the collection first!
    # rekognition.create_collection(CollectionId=COLLECTION)

    def index_faces(bucket, key, collection_id, image_id=None, attributes=(), region="eu-west-1"):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.index_faces(
    Image={
    "S3Object": {
    "Bucket": bucket,
    "Name": key,
    }
    },
    CollectionId=collection_id,
    ExternalImageId=image_id,
    DetectionAttributes=attributes,
    )
    return response['FaceRecords']


    for record in index_faces(BUCKET, KEY, COLLECTION, IMAGE_ID):
    face = record['Face']
    # details = record['FaceDetail']
    print "Face ({}%)".format(face['Confidence'])
    print " FaceId: {}".format(face['FaceId'])
    print " ImageId: {}".format(face['ImageId'])


    """
    Expected output:
    Face (99.945602417%)
    FaceId: dc090f86-48a4-5f09-905f-44e97fb1d455
    ImageId: f974c8d3-7519-5796-a08d-b96e0f2fc242
    """
    35 changes: 35 additions & 0 deletions test-5-search-faces.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import boto3

    BUCKET = "amazon-rekognition"
    KEY = "search.jpg"
    COLLECTION = "my-collection-id"

    def search_faces_by_image(bucket, key, collection_id, threshold=80, region="eu-west-1"):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.search_faces_by_image(
    Image={
    "S3Object": {
    "Bucket": bucket,
    "Name": key,
    }
    },
    CollectionId=collection_id,
    FaceMatchThreshold=threshold,
    )
    return response['FaceMatches']

    for record in search_faces_by_image(BUCKET, KEY, COLLECTION):
    face = record['Face']
    print "Matched Face ({}%)".format(record['Similarity'])
    print " FaceId : {}".format(face['FaceId'])
    print " ImageId : {}".format(face['ExternalImageId'])


    """
    Expected output:
    Matched Face (96.6647949219%)
    FaceId : dc090f86-48a4-5f09-905f-44e97fb1d455
    ImageId : test.jpg
    """