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
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')
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')