1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Azure Face API で写真から感情判定

Last updated at Posted at 2020-08-01

https://www.pc-koubou.jp/magazine/27499
を参考にFace APIのキーとエンドポイントを取得

#cognitive_faceのインストール

pip install cognitive_face

#まずはmacで試してみる

import cognitive_face as CF
import json

KEY = 'XXXXXXXXXX'
ENDPOINT = 'XXXXXXXXXXXXXXXXXXXX(エンドポイント)/face/v1.0'

CF.Key.set(KEY)
CF.BaseUrl.set(ENDPOINT)

img = "XXXXX.jpeg"
faces = CF.face.detect(img, attributes='emotion')
f=json.dumps(faces)
j=json.loads(f)

print(len(j))
for i in range(0,len(j)):
    print(j[i]['faceAttributes']['emotion'])

#RaspberryPiで実装
ラズパイのカメラモジュールを使っています。定期的に写真を撮影し、感情を把握

import cognitive_face as CF
import json
import picamera
import time
import cv2 as cv

KEY = 'XXXXXXXXXX'
ENDPOINT = 'XXXXXXXXXXXXXXXXXXXX(エンドポイント)/face/v1.0'

CF.Key.set(KEY)
CF.BaseUrl.set(ENDPOINT)

while True:
    with picamera.PiCamera() as camera:
        camera.resolution = (512,384)
        camera.capture('test.jpg')
        print('capture')
        img = cv.imread('test.jpg')
        grayimg = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
        img = "test.jpg"
        faces = CF.face.detect(img, attributes='emotion')
        f=json.dumps(faces)
        j=json.loads(f)

        for i in range(0,len(j)):
            print(j[i]['faceAttributes']['emotion'])
            
        time.sleep(10)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?