【问题标题】:Using vision api function to dataframe?使用视觉 api 函数数据帧?
【发布时间】:2020-09-07 07:56:54
【问题描述】:

我正在使用google API函数来提取表情,它会从图像中检测出所有的人脸

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))

这是我得到的输出:

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)

我需要对多张图像使用此功能并希望获得一个数据框,但我不确定如何以我想要的方式将其转换为数据框输出...我需要这样的输出:

想要的输出:

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

。 .

有什么帮助吗?

【问题讨论】:

    标签: python pandas function dataframe vision-api


    【解决方案1】:

    首先启动一个新的空数据框:

    df = pd.DataFrame() 
    

    然后在打印命令旁边添加一个新行:

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

    然后将新行追加到df:

    df = df.append(newline)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-30
      • 2017-08-13
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      相关资源
      最近更新 更多