Microsoft 提供的 Vision 雲端服務為數眾多,例如:Computer Vision、Emotion、Face、Video 等,而本文透過 Microsoft 的 Cognitive Services Emotion 的服務來判斷使用者的情緒 。
前置準備作業
準備一片 Linkit Smart 7688 開發板
將 Linkit Smart 7688 連接至電腦
更新 Firmware 為 0.9.3
http://goo.gl/dVLQ2Y安裝 Webcam
Linkit Smart 7688 與 Webcam 連接圖
Microsoft 端
Step 1. 到 Microsoft 網站申請帳號
Step 2. 登入 Microsoft Emotion Services
網站
Step 3. 點擊 Get started for free
Step 4. 點選 Emotion - Preview
與 I agree to the Microsoft Cognitive Services Terms and Microsoft Privacy Statement
➙ 再點擊 Subscribe
Step 5. 執行完成畫面
- 此 Key 之後呼叫 Emotion API 時會使用到
Linkit Smart 7688 端
Step 1. SSH 進入 Linkit Smart 7688 中
Step 2. 於 Linkit Smart 7688 中安裝套件
opkg update
opkg install ffmpeg
Step 3. 於 Linkit Smart 7688 中透過 Webcam 拍攝照片
- Video Streaming
Command:
mjpg_streamer -i "input_uvc.so -r 320x240 -f 15 -d /dev/video0" -o "output_http.so"
Browser:
http://IP:8080/?action=stream
- 拍攝照片
1. wget http://IP:8080/?action=snapshot -O output.jpg
OR
2. opkg update
opkg install fswebcam
fswebcam -i 0 -d v4l2:/dev/video0 --no-banner -p YUYV --jpeg 95 --save ./test.jpg
Step 4. 於 Linkit Smart 7688 中建立 Emotion 的 Python Code
- Microsoft Emotion API - Request URL
Request URL | 說明 |
---|---|
URL | api.projectoxford.ai/emotion/v1.0/recognize |
- Microsoft Emotion API - Request headers
Request headers | 說明 |
---|---|
Content-Type | application/json |
application/octet-stream | |
Ocp-Apim-Subscription-Key | Emotion Service Key |
- Request body
Request headers | Request body |
---|---|
application/json | { "url": "http://Image-URL" } |
application/octet-stream | Binary Image Data |
- microsoft_Emotion_config.ini
[Emotion]
apiKey: 輸入 Key
- microsoft_Emotion.py
# -*- coding: UTF-8 -*-
#***************************************************
# Import Package #***************************************************
import ConfigParser
import httplib, json, urllib, base64
#***************************************************
# Get Service API Key and Check Local File or URL #***************************************************
isImageFromURL = 0
Config = ConfigParser.ConfigParser()
Config.read("microsoft_Emotion_config.ini")
apiKey = Config.get('Emotion', 'apiKey')
#***************************************************
# Set Request Header #***************************************************
if isImageFromURL:
contentType = "application/json"
else:
contentType = "application/octet-stream"
headers = {
'Content-Type': contentType,
'Ocp-Apim-Subscription-Key': apiKey,
}
host = 'api.projectoxford.ai'
requestURL = '/emotion/v1.0/recognize'
imageURL = "http://i.imgur.com/XR1hU0S.jpg"
jsonImageURL = { 'url': imageURL }
localFilePath = '/Users/Archer/Downloads/recognition2.jpg'
#***************************************************
# POST Microsoft Emotion API
#***************************************************
try:
conn = httplib.HTTPSConnection(host)
if isImageFromURL:
conn.request("POST", requestURL, json.dumps(jsonImageURL), headers)
else:
conn.request("POST", "/emotion/v1.0/recognize", open(localFilePath, 'rb'), headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
conn.close()
print(data)
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
print(e.message)
Step 5. 執行 Python Code
python microsoft_Emotion.py
- 透過 JSON Editor Online 來觀看 JSON
Github
參考資料
- https://www.microsoft.com/cognitive-services/en-us/emotion-api
- https://www.microsoft.com/cognitive-services/en-us/Emotion-api/documentation/GetStartedWithPython