Microsoft 提供的 Vision 雲端服務為數眾多,例如:Computer Vision、Emotion、Face、Video 等,而本文透過 Microsoft 的 Cognitive Services Computer Vision 的服務來判斷使用者的圖片資訊 。
前置準備作業
準備一片 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 Computer Vision Services
網站
Step 3. 點擊 Get started for free
Step 4. 點選 Computer Vision - Preview
與 I agree to the Microsoft Cognitive Services Terms and Microsoft Privacy Statement
➙ 再點擊 Subscribe
Step 5. 執行完成畫面
- 此 Key 之後呼叫 Computer Vision 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 Computer Vision API - Request URL
Request URL | 說明 |
---|---|
URL | api.projectoxford.ai/vision/v1.0/analyze[?visualFeatures][&details] |
visualFeatures | 說明 |
---|---|
Categories | 依圖片的內容來進行分類 |
Tags | 列出圖片有關的標籤 |
Description | 圖片內容描述 |
Faces | 如果面孔存在,則偵測性別和年齡 |
ImageType | 偵測是否為圖片或是線條畫 |
Color | 偵測圖片的主色與強調色是黑或白 |
Adult | 偵測是否為 18 禁的內容 |
details | 說明 |
---|---|
Celebrities | 如果偵測到圖片中的是名人,則顯示是那位名人 |
- Microsoft Computer Vision API - Request headers
Request headers | 說明 |
---|---|
Content-Type | application/json |
application/octet-stream | |
Ocp-Apim-Subscription-Key | Computer Vision Service Key |
- Microsoft Computer Vision API - Request body
Request headers | Request body |
---|---|
application/json | { "url": "http://Image-URL" } |
application/octet-stream | Binary Image Data |
- microsoft_Computer_Vision_config.ini
[ComputerVision]
apiKey: 輸入 Key
- microsoft_Computer_Vision.py
# -*- coding: UTF-8 -*-
#***************************************************
# Import Package #***************************************************
import ConfigParser
import httplib, json, urllib, base64
#***************************************************
# Get Service API Key and Check Local File or URL #***************************************************
isImageFromURL = 1
Config = ConfigParser.ConfigParser()
Config.read("microsoft_Computer_Vision_config.ini")
apiKey = Config.get('ComputerVision', 'apiKey')
#***************************************************
# Set Request Header #***************************************************
if isImageFromURL:
contentType = "application/json"
else:
contentType = "application/octet-stream"
headers = {
'Content-Type': contentType,
'Ocp-Apim-Subscription-Key': apiKey,
}
params = urllib.urlencode({
'visualFeatures': 'Categories,Tags,Description,Faces,ImageType,Color,Adult',
'details': 'Celebrities',
})
host = 'api.projectoxford.ai'
requestURL = "/vision/v1.0/analyze?%s" % params
imageURL = "http://i.imgur.com/WyfYIwZ.jpg"
jsonImageURL = { 'url': imageURL }
localFilePath = '/Users/Archer/Downloads/recognition2.jpg'
#***************************************************
# POST Microsoft Computer Vision API
#***************************************************
try:
conn = httplib.HTTPSConnection(host)
if isImageFromURL:
conn.request("POST", requestURL, json.dumps(jsonImageURL), headers)
else:
conn.request("POST", requestURL, 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_Computer_Vision.py
- 透過 JSON Editor Online 來觀看 JSON
Github
參考資料
- https://www.microsoft.com/cognitive-services/en-us/computer-vision-api
- https://www.microsoft.com/cognitive-services/en-us/computer-vision-api/documentation