[공통교육-AI] Cognitive Services

1. 학습한 내용

① Chrome 개발자 도구 열기

https://portal.azure.com/

② Azure Face API Documentation

https://docs.microsoft.com/ko-kr/azure/cognitive-services/face/
https://docs.microsoft.com/ko-kr/azure/cognitive-services/face/face-api-how-to-topics/howtodetectfacesinimage
https://docs.microsoft.com/ko-kr/azure/cognitive-services/face/concepts/face-detection
https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

③ Object Detection

from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
import json

subscription_key = '~~'
vision_base_url = 'https://daeguaivision00.cognitiveservices.azure.com/vision/v2.0/'
objectDetection_url = vision_base_url + 'detect'
object_image = 'https://www.newsmin.co.kr/news/wp-content/uploads/2021/05/05180000dddd-1-1536x904.jpg'
img = Image.open(BytesIO(requests.get(object_image).content))
headers = {'Ocp-Apim-Subscription-Key':subscription_key}
data = {'url':object_image}
response = requests.post(objectDetection_url, headers=headers, json=data)
detectionResult = response.json()

draw = ImageDraw.Draw(img)

objects = detectionResult['objects']

for obj in objects:
    #print(obj)
    
    rect = obj['rectangle']
    
    x = rect['x']
    y = rect['y']
    w = rect['w']
    h = rect['h']
    
    # 사각형을 그리는 코드
    draw.rectangle(((x, y), (x + w, y + h)), outline='red')
    
    # 글자를 입력하는 코드
    objectName = obj['object']
    draw.text((x, y), objectName, fill='red')

③ Face Detection

import requests
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont

subscription_key = '~~'
faceDetection_url = 'https://facedetect00.cognitiveservices.azure.com/face/v1.0/detect'
image_url = 'https://cdn.entermedia.co.kr/news/photo/202005/10914_27367_543.jpg'
img = Image.open(BytesIO(requests.get(image_url).content))
draw = ImageDraw.Draw(img)
headers = {'Ocp-Apim-Subscription-key':subscription_key}
params = {
    'returnFaceID':'true',
    'returnFaceAttributes':'age,gender,emotion'
}
data = {'url':image_url}
response = requests.post(faceDetection_url, headers=headers, params=params, json=data)

faces = response.json()

for face in faces:
    rect = face['faceRectangle']
    
    left = rect['left']
    top = rect['top']
    width = rect['width']
    height = rect['height']
    
    draw.rectangle(((left, top), (left + width, top + height)), outline='white')
    
    face_info = face['faceAttributes']
    age = face_info['age']
    gender = face_info['gender']
    happiness = face_info['emotion']['happiness']
    result = f'Gender: {gender}\nAge: {age}'
    
    emotions = face_info['emotion']
    for emotion in emotions:
        if emotions[emotion] == 0:
            continue
        result = result + f'\n{emotion}: {emotions[emotion]}'

    draw.text((left, top), result, fill='white')

④ 구름 IDE

https://www.goorm.io/

2. 학습내용 중 어려웠던 점

  • Nothing

3. 해결방법

  • Nothing

4. 학습소감

  • Microsoft MVP 김영욱 강사님의 강의 내용을 들을 수 있어서 영광입니다.

좋은 웹페이지 즐겨찾기