Azure AI Tutorial 6 - Các bài toán về khuôn mặt trên Azure
Nhận diện khuôn mặt và phân tích trạng thái khuôn mặt hiện đang là một trong những chủ đề hot của AI nói chung và Computer Vision nói riêng. Vậy chúng ta hãy cùng đến với các Face Detection, Face Analysis, Face Recognization .. trên Azure trong phần này nhé
Giờ chúng ta cùng nhau hands-on nhận diện và phân tích khuôn mặt với các dịch vụ trên Azure. Các mục mình sẽ đi qua
- Tạo Cognitive Service Resource
- Tạo notebook -> Face Detection
- Analyze facial
- Find similar Face
- Recognize faces
LET’S START
1. Tạo Cognitive Service Resource
-
Vào portal Azure -> Search Cognitive Service -> Create
- Điền thông tin và Create 1 Service (Ở đây mình đặt tên MVCognitiveService)
- Copy Keys và Endpoint
2. Tạo notebook và Face Detection
- Tạo Python Notebook (Có thể dùng máy các nhân, google colad, Azure machine learning workspace…)
- Install thư viện face cognitive service
! pip install azure-cognitiveservices-vision-face
- Nhập thông tin key và Endpoint vừa copy
cog_key = '0afbab280c9f42e589143a683845a0c5'
cog_endpoint = 'https://mvcognitiveservice.cognitiveservices.azure.com/'
Như vậy chúng ta đã có thể sử dụng các dịch vụ của face cognitive. Test detecting khuôn mặt thử nhé.
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
from python_code import faces
import os
%matplotlib inline
# Create a face detection client.
face_client = FaceClient(cog_endpoint, CognitiveServicesCredentials(cog_key))
# Open an image
image_path = os.path.join('data', 'face', 'anh02.jpg')
image_stream = open(image_path, "rb")
# Detect faces
detected_faces = face_client.face.detect_with_stream(image=image_stream)
# Display the faces (code in python_code/faces.py)
faces.show_faces(image_path, detected_faces)
Và đây là output:
3. Analyze facial attributes
# Open an image
image_path = os.path.join('data', 'face', 'anh01.jpg')
image_stream = open(image_path, "rb")
# Detect faces
detected_faces = face_client.face.detect_with_stream(image=image_stream)
# Display the faces (code in python_code/faces.py)
faces.show_faces(image_path, detected_faces, show_id=True)
Thành quả:
4. Find similar Face
image_1_path = os.path.join('data', 'face', 'anh01.jpg')
image_1_stream = open(image_1_path, "rb")
image_1_faces = face_client.face.detect_with_stream(image=image_1_stream)
face_1 = image_1_faces[0]
# Get the face IDs in a second image
image_2_path = os.path.join('data', 'face', 'anh02.jpg')
image_2_stream = open(image_2_path, "rb")
image_2_faces = face_client.face.detect_with_stream(image=image_2_stream)
image_2_face_ids = list(map(lambda face: face.face_id, image_2_faces))
# Find faces in image 2 that are similar to the one in image 1
similar_faces = face_client.face.find_similar(face_id=face_1.face_id, face_ids=image_2_face_ids)
# Show the face in image 1, and similar faces in image 2(code in python_code/face.py)
faces.show_similar_faces(image_1_path, face_1, image_2_path, image_2_faces, similar_faces)
Output:
5. Recognize faces
Các ví dụ ở bên trên đã cho chúng ta thấy FaceCognitive Service có thể nhận diện khuôn mặt, phân tích khuôn mặt, tìm khuôn mặt giống nhau. Giờ chúng ta cùng train model để nhận diện từng người.
- Tạo nhóm
group_id = 'employee_group_id'
try:
# Delete group if it already exists
face_client.person_group.delete(group_id)
except Exception as ex:
print(ex.message)
finally:
face_client.person_group.create(group_id, 'employees')
- Thêm bạn "MinhVinh" vào nhóm, thêm ảnh "MinhVinh" để training model
import matplotlib.pyplot as plt
from PIL import Image
import os
%matplotlib inline
# Add a person (minhvinh) to the group
wendell = face_client.person_group_person.create(group_id, 'Minh Vinh')
# Get photo's of minhvinh
folder = os.path.join('data', 'face', 'minhvinh')
wendell_pics = os.listdir(folder)
# Register the photos
i = 0
fig = plt.figure(figsize=(8, 8))
for pic in wendell_pics:
# Add each photo to person in person group
img_path = os.path.join(folder, pic)
img_stream = open(img_path, "rb")
face_client.person_group_person.add_face_from_stream(group_id, wendell.person_id, img_stream)
# Display each image
img = Image.open(img_path)
i +=1
a=fig.add_subplot(1,len(wendell_pics), i)
a.axis('off')
imgplot = plt.imshow(img)
plt.show()
# Train Model
face_client.person_group.train(group_id)
Các ảnh mình train như sau
- Model đã train. Cùng thử thành quả nào
image_path = os.path.join('data', 'face', 'anh1.jpg')
image_stream = open(image_path, "rb")
image_faces = face_client.face.detect_with_stream(image=image_stream)
image_face_ids = list(map(lambda face: face.face_id, image_faces))
# Get recognized face names
face_names = {}
recognized_faces = face_client.face.identify(image_face_ids, group_id)
for face in recognized_faces:
person_name = face_client.person_group_person.get(group_id, face.candidates[0].person_id).name
face_names[face.face_id] = person_name
# show recognized faces
faces.show_recognized_faces(image_path, image_faces, face_names)
Output:
Chúc các bạn thành công và hẹn gặp lại ở phần kế tiếp!