學習 Python 時怎樣利用 Microsoft 情感 API 來獲取影像的情感?
每個人都具有像快樂、悲傷、中立、驚喜、悲傷等情緒,如果我們透過 Python 建立影像情緒,例如快樂、悲傷、中立、驚喜等。我們可以將 Microsoft 情感 API 用於任何開發目的。
我們可以使用 Microsoft 情感 API 輕鬆闡述所有這些情緒。
示例程式碼
import http.client, urllib.request import urllib.parse, urllib.error import base64, sys import simplejson as json # replace with subscription_key # you obtained after registration subscription_key = '23d39244dbe55173214b56ab45d56cla' headers = { # Request for headers. And also replace the placeholder key with # our subscription key. 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': subscription_key, } params = urllib.parse.urlencode({ }) url1 = 'IMAGE OF URL ' body = { 'url': url1 } newbody =str(body) try: conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com') conn.request("POST", "/emotion/v1.0/recognize?%s" % params, newbody, headers) response = conn.getresponse() data = response.read() my_parsed = json.loads(data) print ("Response is:") print (json.dumps(my_parsed, sort_keys=True, indent=2)) val = my_parsed[0]["scores"] res = max(val, key = val.get) print ("\nEmotionoutput :: ",res) conn.close() except Exception as e: print(e.args)
廣告