gpt4 book ai didi

python - 微软情感API多图像PYTHON 2.7

转载 作者:行者123 更新时间:2023-12-03 04:22:37 27 4
gpt4 key购买 nike

我目前正在使用 Microsoft Azure Emotion API 来查看某些图像的情感。尽管示例代码可以工作(Python 2.7),但我希望它不仅仅是一张图像。

我将有一个目录 (URL),其中包含 100 个图像,标记为 image1、image2、image3。

我正在寻找的是对代码的更改,以为其循环的图像提供平均评级/分数。

我的代码是:

import httplib, urllib, base64

headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'MY KEY HERE',
}

params = urllib.urlencode({
})

body = "{ 'url': 'https://assets.mubi.com/images/notebook/post_images/22267/images-w1400.jpg?1474980339' }"

try:
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

我正在考虑 while 循环:

for x in range (0,100):

并将 URL: 更改为带有 (x) 的路径但我无法让它发挥作用。任何帮助将不胜感激。

谢谢,内森。

最佳答案

正如我在评论中提到的,我假设您想要获取 Facerectangle 并计算 100 张图像的平均值。根据我的经验,我们无法直接通过 Emotion API 得到您想要的结果。

我建议您使用循环调用API来汇总结果,然后计算平均结果。

可以引用下面的代码,我已经测试成功了。

import httplib, urllib, base64, json

headers = {
# Request headers. Replace the placeholder key below with your subscription key.
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'YOUR KEY',
}

params = urllib.urlencode({
})

widthTotal = 0
topTotal = 0
leftTotal = 0
heightTotal = 0
sadnessTotal = 0
neutralTotal = 0
contemptTotal = 0
disgustTotal = 0
angerTotal = 0
surpriseTotal = 0
fearTotal = 0
happinessTotal = 0

count = 100

try:
for x in range(0, count):
# Replace the example URL below with the URL of the image you want to analyze.
body = "{ 'url': 'https://assets.mubi.com/images/notebook/post_images/22267/images-w1400.jpg?1474980339' }"

# NOTE: You must use the same region in your REST call as you used to obtain your subscription keys.
# For example, if you obtained your subscription keys from westcentralus, replace "westus" in the
# URL below with "westcentralus".
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
# print(data)

Arr = json.loads(data, encoding='utf-8')
# print Arr[0].values()[0]
# print Arr[0].values()[1]
# print Arr[0].values()[0]["width"]
widthTotal += Arr[0].values()[0]["width"]
topTotal += Arr[0].values()[0]["top"]
leftTotal += Arr[0].values()[0]["height"]
heightTotal += Arr[0].values()[0]["height"]
sadnessTotal += Arr[0].values()[1]["sadness"]
neutralTotal += Arr[0].values()[1]["neutral"]
contemptTotal += Arr[0].values()[1]["contempt"]
disgustTotal += Arr[0].values()[1]["disgust"]
angerTotal += Arr[0].values()[1]["anger"]
surpriseTotal += Arr[0].values()[1]["surprise"]
fearTotal += Arr[0].values()[1]["fear"]
happinessTotal += Arr[0].values()[1]["happiness"]

conn.close()

except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

print "width avg: " + str(widthTotal / count)
print "top avg: " + str(topTotal / count)
print "left avg: " + str(leftTotal / count)
print "heigh avg: " + str(heightTotal / count)
print "sadness avg: " + str(sadnessTotal / count)
print "neutral avg: " + str(neutralTotal / count)
print "contempt avg: " + str(contemptTotal / count)
print "disgust avg: " + str(disgustTotal / count)
print "anger avg: " + str(angerTotal / count)
print "surprise avg: " + str(surpriseTotal / count)
print "fear avg: " + str(fearTotal / count)
print "happiness avg: " + str(happinessTotal / count)

打印输出:

enter image description here

希望对您有帮助。

关于python - 微软情感API多图像PYTHON 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47154016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com