在python的AWS Rekognition输出中保持字典元素的顺序相同
我正在使用AWS Rekognion来研究情绪输出。这是我的代码:在python的AWS Rekognition输出中保持字典元素的顺序相同,python,python-3.x,amazon-web-services,image-processing,amazon-rekognition,Python,Python 3.x,Amazon Web Services,Image Processing,Amazon Rekognition,我正在使用AWS Rekognion来研究情绪输出。这是我的代码: 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}, Attri
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}]
在第一次试验中,输出的顺序是:恶心,高兴。。。惊讶然而,在第二次审判中,顺序变为:悲伤,愤怒。。。很惊讶
订单输出中的更改成为一个问题,因为我想循环浏览多张照片,并将置信度值存储在一组行中。例如:
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...
我必须在bot3
模块中更改什么设置,以保持输出上的订单类型不变?您可以通过按类型对值进行排序,然后进行转置并设置正确的索引,使数据帧与熊猫保持一致:
#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])
请尝试收集。订购信息
谢谢。今晚我要试试。