มนุษย์ทุกคนมีอารมณ์ เช่น שמחה, ความเศร้า, เฉยเมย, ความประหลาดใจ, ความเศร้า ฯลฯ ถ้าสร้างการตรวจจับอารมณ์จากภาพใน Python สามารถใช้ Microsoft Emotion API สำหรับการพัฒนา
สามารถจัดการอารมณ์ทั้งหมดนี้ได้ง่ายด้วย Microsoft Emotion API
โค้ดตัวอย่าง
import http.client, urllib.request
import urllib.parse, urllib.error
import base64, sys
import simplejson as json
# แทนที่ด้วย subscription_key ที่ได้หลังลงทะเบียน
subscription_key = '23d39244dbe55173214b56ab45d56cla'
headers = {
# Request headers แทนที่ placeholder key ด้วย 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("\nอารมณ์ที่ตรวจพบ :: ", res)
conn.close()
except Exception as e:
print(e.args)