Using vision api function to dataframe?

289 views Asked by At

I am using the google API function to extract the expressions, it will detect all the faces from the image

def detect_faces_uri(uri):
    """Detects faces in the file located in Google Cloud Storage or the web."""
    from google.cloud import vision
    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()
    image.source.image_uri = uri

    response = client.face_detection(image=image)
    faces = response.face_annotations

    # Names of likelihood from google.cloud.vision.enums
    likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
                       'LIKELY', 'VERY_LIKELY')
    print('Faces:')

    for face in faces:
        print('anger: {}'.format(likelihood_name[face.anger_likelihood]))
        print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
        print('surprise: {}'.format(likelihood_name[face.surprise_likelihood]))

        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in face.bounding_poly.vertices])

        print('face bounds: {}'.format(','.join(vertices)))

    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

This is the output what I am getting:

Faces:
anger: VERY_UNLIKELY
joy: VERY_LIKELY
surprise: VERY_UNLIKELY
face bounds: (1077,157),(2146,157),(2146,1399),(1077,1399)
anger: VERY_UNLIKELY
joy: VERY_UNLIKELY
surprise: VERY_UNLIKELY
face bounds: (144,1273),(793,1273),(793,1844),(144,1844)
anger: VERY_UNLIKELY
joy: VERY_UNLIKELY
surprise: VERY_UNLIKELY
face bounds: (785,167),(1100,167),(1100,534),(785,534)

I need to use this function for several images and want to get a data frame but I am not really sure how I can convert it into the data frame output the way I want... I need the output like this:

desired output:

URL                   Face      Anger     Joy       Surprised
abc.com               Face1     Likely    Unlikely   Unlikely
abc.com               Face2     Unlikely  Likely    Unlikely
.

. .

Any help?

1

There are 1 answers

0
gtomer On

First start a new empty dataframe:

df = pd.DataFrame() 

Then next to the print command add a new line:

newline= pd.DataFrame({"x":[vertex.x], "y":[vertex.y]}) 

Then append the new line to the df:

df = df.append(newline)