Azure AI Tutorial 5 - Object Detection Trên Azure Custom Vision
Tiếp tục với bài viết về Computer Vision. Chúng ta đến với chuyên mục Object Detection trên Azure.
Các bạn chưa biết Object Detection là gì? Hoạt động như thế nào có thể đọc lại Bài 3.
Giờ mình cũng thực hành trên Azure. Mình sẽ hands-on 4 phần
- Tạo Custom Vision
- Upload data , đánh bouding box và train model
- Test model
- Publish và consume model
Hãy cùng bắt đầu nhé
1. Tạo CustomVision workspace
Tương tự bài 4
2. Tạo Project , Upload data, bouding box và train model
-
Tạo Project.
- Upload data và khoanh vùng object
p/s: Các bạn có thể download example data ở đây : https://aka.ms/fruit-objects
Đánh lable từng ảnh nhé. (Mất khoảng 5->10 phút)
Sau khi đánh bouding box cho từng object chúng ta có được: 18 apple , 19 banana, 20 orange.
- Click TRAIN
Sau khi chờ đợi training model và đây là kết quả
3. Quick Test model vừa tạo (MVClassifier)
-
Chọn Quick Test (Chúng ta có thể test bằng 2 cách: Nhập image url hoặc upload file)
4. Cách Publish MVObjectDetection
- Chọn Publish
- Copy model name, Project ID, Key, Endpoint (Tương tự bài 3.1)
Tạo Notebook
Ở đây mình dùng ngôn ngữ Python để sử dụng model
1. Nếu chưa install thư viên Azure cognitive thì install
!pip install azure-cognitiveservices-vision-customvision
2. Nhập các thông tin như: Project ID, model name, key , endpoint đã copy ở phần trước
project_id = '7b2d7831-5a07-488b-a218-889cb3b746e7'
cv_key = '3a9c6ce2710742ce89ced98e9df157d1'
cv_endpoint = 'https://mvblogcustomvision-prediction.cognitiveservices.azure.com/'
model_name = 'MVObjectDetection'
3. Project test model
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import ApiKeyCredentials
from matplotlib import pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import os
%matplotlib inline
# Load a test image and get its dimensions
test_img_file = os.path.join('data', 'object-detection', 'produce.jpg')
test_img = Image.open(test_img_file)
test_img_h, test_img_w, test_img_ch = np.array(test_img).shape
# Get a prediction client for the object detection model
credentials = ApiKeyCredentials(in_headers={"Prediction-key": cv_key})
predictor = CustomVisionPredictionClient(endpoint=cv_endpoint, credentials=credentials)
print('Detecting objects in {} using model {} in project {}...'.format(test_img_file, model_name, project_id))
# Detect objects in the test image
with open(test_img_file, mode="rb") as test_data:
results = predictor.detect_image(project_id, model_name, test_data)
# Create a figure to display the results
fig = plt.figure(figsize=(8, 8))
plt.axis('off')
# Display the image with boxes around each detected object
draw = ImageDraw.Draw(test_img)
lineWidth = int(np.array(test_img).shape[1]/100)
object_colors = {
"apple": "lightgreen",
"banana": "yellow",
"orange": "orange"
}
for prediction in results.predictions:
color = 'white' # default for 'other' object tags
if (prediction.probability*100) > 50:
if prediction.tag_name in object_colors:
color = object_colors[prediction.tag_name]
left = prediction.bounding_box.left * test_img_w
top = prediction.bounding_box.top * test_img_h
height = prediction.bounding_box.height * test_img_h
width = prediction.bounding_box.width * test_img_w
points = ((left,top), (left+width,top), (left+width,top+height), (left,top+height),(left,top))
draw.line(points, fill=color, width=lineWidth)
plt.annotate(prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100),(left,top), backgroundcolor=color)
plt.imshow(test_img)
4. Output là đây
Chúc các bạn thành công và hẹn gặp lại ở phần tiếp theo nhé