2017-11-07 2 views
1

Ich verwende derzeit Microsoft Azure Emotion API, um Emotionen bestimmter Bilder zu betrachten. Obwohl der Beispielcode funktioniert (Python 2.7), möchte ich, dass es mehr als ein Bild gibt.Microsoft Emotion Api mehrere Bilder PYTHON 2.7

Ich werde ein Verzeichnis (URL) haben, das 100 Bilder enthält, beschriftet mit image1, image2, image3.

Was ich suche ist eine Änderung des Codes, um eine durchschnittliche Bewertung/Punktzahl für die Bilder zu geben, die es geloopt hat.

Der Code, den ich habe, ist:

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

ich eine While-Schleife denke:

for x in range (0,100): 

und die URL ändern: mit (x) auf den Weg Aber ich kann nicht bekommen das funktioniert. Jede Hilfe würde wirklich geschätzt werden.

Danke, Nathan.

+0

Ich bin mir nicht sicher, was Sie eigentlich wollen? Wie ich weiß, ist das Ergebnis der Aufruf der Emotion-API facecrewangle und scores.Sie möchten das facet_rectangle erhalten und Durchschnittsnoten von 100 Bildern direkt über die API erhalten? –

+0

Irgendwelche Updates jetzt? –

Antwort

0

Wie ich in meinem Kommentar erwähnt habe, nehme ich an, dass Sie das facecrectangle bekommen möchten und Durchschnittswerte von 100 Bildern erzielen. Meiner Erfahrung nach können wir das gewünschte Ergebnis nicht direkt über die Emotion API erhalten.

Ich schlage vor, Sie verwenden die Loop-Invoke-API, um die Ergebnisse zusammenzufassen, dann berechnen Sie das durchschnittliche Ergebnis.

Sie könnten auf den folgenden Code verweisen, den ich bereits erfolgreich getestet habe.

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) 

Druckausgabe:

enter image description here

Hoffe, dass es Ihnen hilft.

Verwandte Themen