dhwiii's notepad | 딥 러닝, 코덱 일기장

(Python) MS Azure Face cognition API를 사용한 파이썬 얼굴인식 예제 본문

■ Working Draft/◎ Coding

(Python) MS Azure Face cognition API를 사용한 파이썬 얼굴인식 예제

dhwiii 2020. 6. 9. 16:38
import cognitive_face as CF
 
KEY = 'PERSONAL_KEY'
# Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)
 
BASE_URL = 'https://koreacentral.api.cognitive.microsoft.com/face/v1.0'
# Replace with your regional Base URL
CF.BaseUrl.set(BASE_URL)
 
# You can use this example JPG or replace the URL below with your own URL to a JPEG image.
img_url = 'PERSONAL_IMG'
#img_url have to load web or Cloud file (not using local file)
#img_url = 'myimage.jpg'
 
faces = CF.face.detect(img_url, True , False, "age,gender,emotion")
 
for i in faces:
    print(i)
    print()
 
# draw face rect on image
import requests
from io import BytesIO
from PIL import Image, ImageDraw
 
#Convert width height to a point in a rectangle
def getRectangle(faceDictionary):
    rect = faceDictionary['faceRectangle']
    left = rect['left']
    top = rect['top']
    bottom = left + rect['height']
    right = top + rect['width']
    return ((left, top), (bottom, right))
 
#Download the image from the url
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
 
#For each face returned use the face rectangle and draw a red box.
draw = ImageDraw.Draw(img)
for face in faces:
    draw.rectangle(getRectangle(face), outline='red')
 
#Display the image in the users default image browser.
img.show()

 

(수정중)

MS Azure Face cognition API 를 사용한 얼굴인식 예제입니다.

결과값은 json 으로 반환됩니다.

 

출처 : oceancoding.blogspot.com/2019/01/ms-azure-face.html

 

Comments