在 python 的 AWS Rekognition 输出中保持字典元素顺序相同
Keep dictionary elements order the same in AWS Rekognition outputs in python
我正在使用 AWS rekognition 来研究情绪输出。这是我的代码:
photo = 'InVacation.jpg'
image = Image.open(photo)
stream = io.BytesIO()
image.save(stream,format="JPEG")
image_binary = stream.getvalue()
response = client.detect_faces(
Image={'Bytes':image_binary},
Attributes=['ALL']
)
response
问题是每次我处理多张照片时,情绪类型的顺序都会改变。例如:
response['FaceDetails'][0]["Emotions"]
[{'Type': 'DISGUSTED', 'Confidence': 3.6973443031311035},
{'Type': 'HAPPY', 'Confidence': 1.150834321975708},
{'Type': 'ANGRY', 'Confidence': 4.140467643737793},
{'Type': 'CALM', 'Confidence': 70.42333221435547},
{'Type': 'CONFUSED', 'Confidence': 9.918739318847656},
{'Type': 'SAD', 'Confidence': 4.620922565460205},
{'Type': 'SURPRISED', 'Confidence': 6.048351764678955}]
使用相同的代码,第二张照片产生:
[{'Type': 'SAD', 'Confidence': 4.620922565460205},
{'Type': 'ANGRY', 'Confidence': 4.140467643737793},
{'Type': 'CONFUSED', 'Confidence': 9.918739318847656},
{'Type': 'CALM', 'Confidence': 70.42333221435547},
{'Type': 'DISGUSTED', 'Confidence': 3.6973443031311035},
{'Type': 'HAPPY', 'Confidence': 1.150834321975708},
{'Type': 'SURPRISED', 'Confidence': 6.048351764678955}]
初试时,输出顺序为:厌恶,开心……惊讶。然而,在二审中,顺序变为:悲伤、愤怒……惊讶。
顺序输出的变化成为一个问题,因为我想遍历多张照片并将 confidence
值存储在一组行中。例如:
FaceEmotions = pd.DataFrame(response['FaceDetails'][0]["Emotions"])
awsConfidence = FaceEmotions[["Confidence"]].T
awsConfidence.columns = ["Surprised", "Happy", "Calm", "Sad", "Disgusted", "Angry", "Confused"]
awsConfidence
Surprised Happy Calm Sad Disgusted Angry Confused
Confidence 3.697344 6.048352 70.423332 4.620923 1.150834 9.918739 4.140468
Confidence 5.623224 3.032444 40.413132 6.420921 2.400834 1.118739 7.140412
And so on...
我必须在 boto3
模块中更改什么设置才能使输出的订单类型保持不变?
您可以通过按类型对值进行排序然后转置并设置正确的索引来获得与 pandas 一致的数据帧:
#Supposing x is the array you get
df1 = pd.DataFrame(x).sort_values(by='Type').\
set_index('Type').T.reset_index().drop('index', axis= 1)
然后您可以轻松地连接您生成的数据帧:
dfc = pd.concat([df1,df2,df3])
我正在使用 AWS rekognition 来研究情绪输出。这是我的代码:
photo = 'InVacation.jpg'
image = Image.open(photo)
stream = io.BytesIO()
image.save(stream,format="JPEG")
image_binary = stream.getvalue()
response = client.detect_faces(
Image={'Bytes':image_binary},
Attributes=['ALL']
)
response
问题是每次我处理多张照片时,情绪类型的顺序都会改变。例如:
response['FaceDetails'][0]["Emotions"]
[{'Type': 'DISGUSTED', 'Confidence': 3.6973443031311035},
{'Type': 'HAPPY', 'Confidence': 1.150834321975708},
{'Type': 'ANGRY', 'Confidence': 4.140467643737793},
{'Type': 'CALM', 'Confidence': 70.42333221435547},
{'Type': 'CONFUSED', 'Confidence': 9.918739318847656},
{'Type': 'SAD', 'Confidence': 4.620922565460205},
{'Type': 'SURPRISED', 'Confidence': 6.048351764678955}]
使用相同的代码,第二张照片产生:
[{'Type': 'SAD', 'Confidence': 4.620922565460205},
{'Type': 'ANGRY', 'Confidence': 4.140467643737793},
{'Type': 'CONFUSED', 'Confidence': 9.918739318847656},
{'Type': 'CALM', 'Confidence': 70.42333221435547},
{'Type': 'DISGUSTED', 'Confidence': 3.6973443031311035},
{'Type': 'HAPPY', 'Confidence': 1.150834321975708},
{'Type': 'SURPRISED', 'Confidence': 6.048351764678955}]
初试时,输出顺序为:厌恶,开心……惊讶。然而,在二审中,顺序变为:悲伤、愤怒……惊讶。
顺序输出的变化成为一个问题,因为我想遍历多张照片并将 confidence
值存储在一组行中。例如:
FaceEmotions = pd.DataFrame(response['FaceDetails'][0]["Emotions"])
awsConfidence = FaceEmotions[["Confidence"]].T
awsConfidence.columns = ["Surprised", "Happy", "Calm", "Sad", "Disgusted", "Angry", "Confused"]
awsConfidence
Surprised Happy Calm Sad Disgusted Angry Confused
Confidence 3.697344 6.048352 70.423332 4.620923 1.150834 9.918739 4.140468
Confidence 5.623224 3.032444 40.413132 6.420921 2.400834 1.118739 7.140412
And so on...
我必须在 boto3
模块中更改什么设置才能使输出的订单类型保持不变?
您可以通过按类型对值进行排序然后转置并设置正确的索引来获得与 pandas 一致的数据帧:
#Supposing x is the array you get
df1 = pd.DataFrame(x).sort_values(by='Type').\
set_index('Type').T.reset_index().drop('index', axis= 1)
然后您可以轻松地连接您生成的数据帧:
dfc = pd.concat([df1,df2,df3])