Object Detection using ImageAI
ImageAI
Object Detection หมายถึงการค้นหาวัตถุที่ปรากฎอยู่ในรูปภาพ
จากตัวอย่างข้างต้น คือการหาวัตถุ โดยที่วัตถุอาจเป็นได้ทั้ง person, bus, truck, van ก็ได้ ทั้งนี้ขึ้นอยู่กับความต้องการของผู้พัฒนาโปรแกรม
จากตัวอย่างข้างต้น มีวัตถุประสงค์ที่ต้องการค้นหาวัตถุที่เป็นสีน้ำเงิน จึงใช้หลักการแปลงค่าสีจาก RGB ให้เป็น HSV และกำหนดช่วง (Range) ของค่าสีที่ต้องการค้นหา
จากตัวอย่างข้างต้น เป็นการค้นหาใบหน้า (Face Detection) ดังนั้น ในกรณีวัตถุจึงหมายถึงใบหน้า จากตัวอย่างใช้วิธี Haar-Cascase Classifier และ MMOD-CNN ในการค้นหา
การทำ Object Detection ด้วย Library ของ ImageAI สามารถทำได้ดังนี้
- ติดตั้ง Library ของ ImageAI
- เริ่มต้นเขียนโปรแกรม ดังตัวอย่างต่อไปนี้
ดาวน์โหลดโมเดล (Model) ที่ต้องการใช้สำหรับการทำ Object Detection โดยสามารถเลือกโหลดได้ดังต่อไปนี้
- RetinaNet (Size = 145 mb, high performance and accuracy, with longer detection time)
- YOLOv3 (Size = 237 mb, moderate performance and accuracy, with a moderate detection time)
- TinyYOLOv3 (Size = 34 mb, optimized for speed and moderate performance, with fast detection time)
- RetinaNet (Size = 145 mb, high performance and accuracy, with longer detection time)
- Import ไลบรารีที่จำเป็นในการเขียนโปรแกรม
from imageai.Detection import ObjectDetection
import os
import cv2
import matplotlib.pyplot as plt
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo.h5"))custom = detector.CustomObjects(person=True, dog=True)detector.loadModel()
detections = detector.detectObjectsFromImage(custom_objects=custom,
input_image= os.path.join(execution_path,
"image2.jpg"),
output_image_path=os.path.join(execution_path ,
"image2new.jpg"), minimum_percentage_probability=30)
จากตัวอย่างข้างต้น ได้เลือกโมเดลของ YOLOv3 (yolo.h5) ดังนั้น ในโปรแกรมจึงต้องเปลี่ยน ModelType ให้เป็น setModelTypeAsResNet()
prediction.setModelTypeAsYOLOv3()
หากต้องการกำหนดเฉพาะ Output ที่ต้องการเช่น ค้นหาเฉพาะ person และ dog สามารถทำได้ ดังนี้
custom = detector.CustomObjects(person=True, dog=True)
จากนั้น แสดงผลลัพธ์ที่ได้จากการค้นหา ดังนี้
for eachObject in detections:
print(eachObject["name"] , " : ",
eachObject["percentage_probability"], " : ",
eachObject["box_points"] )
print("--------------------------------")
ผลลัพธ์ที่ได้ คือ
Class : Probability : box_points
person : 58.40646028518677 : [170, 104, 285, 296]
--------------------------------
person : 62.51212954521179 : [412, 120, 567, 282]
--------------------------------
person : 84.35078859329224 : [307, 169, 384, 256]
--------------------------------
สุดท้ายเขียนโปรแกรมเพื่อแสดงผลลัพธ์
img = cv2.imread('image2new.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(15,10))
plt.imshow(img)
plt.show()
Code
from imageai.Detection import ObjectDetection
import os
import cv2
import matplotlib.pyplot as pltexecution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo.h5"))custom = detector.CustomObjects(person=True, dog=True)detector.loadModel()
detections = detector.detectObjectsFromImage(custom_objects=custom,
input_image= os.path.join(execution_path,
"image2.jpg"),
output_image_path=os.path.join(execution_path ,
"image2new.jpg"), minimum_percentage_probability=30)for eachObject in detections:
print(eachObject["name"] , " : ",
eachObject["percentage_probability"], " : ",
eachObject["box_points"] )
print("--------------------------------")img = cv2.imread('image2new.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(15,10))
plt.imshow(img)
plt.show()
หากต้องการนับจำนวน Object ที่ค้นพบทั้งหมด สามารถทำได้โดย
print(len(detections))
หากต้องการแสดง Object ที่ค้นพบ โดยแยกแสดงแต่ละ Object สามารถทำได้โดย
img = cv2.imread(source_img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in range(0, len(detections)):
x1 = detections[i]['box_points'][0]
y1 = detections[i]['box_points'][1]
x2 = detections[i]['box_points'][2]
y2 = detections[i]['box_points'][3]
plt.figure(figsize=(15,10))
plt.imshow(img[y1:y2,x1:x2])
plt.show()
ทั้งนี้ หากไม่กำหนด CustomObjects ผลลัพธ์ที่ได้แสดงดังต่อไปนี้
laptop : 87.32235431671143 : (306, 238, 390, 284)
--------------------------------
laptop : 96.86298966407776 : (121, 209, 258, 293)
--------------------------------
laptop : 98.6301600933075 : (279, 321, 401, 425)
--------------------------------
laptop : 99.78572130203247 : (451, 204, 579, 285)
--------------------------------
bed : 94.02391314506531 : (23, 205, 708, 553)
--------------------------------
apple : 48.03136885166168 : (527, 343, 557, 364)
--------------------------------
cup : 34.09906327724457 : (462, 347, 496, 379)
--------------------------------
cup : 44.65090036392212 : (582, 342, 618, 386)
--------------------------------
person : 57.70219564437866 : (27, 311, 341, 437)
--------------------------------
person : 85.26121377944946 : (304, 173, 387, 253)
--------------------------------
person : 96.33603692054749 : (415, 130, 538, 266)
--------------------------------
person : 96.95255160331726 : (174, 108, 278, 269)
--------------------------------