That was covered in the last one ImageAI How to realize picture prediction , Now let's talk about ImageAI The second function of object detection .
ImageAI It provides a very convenient and powerful method to perform object detection on images and extract each object from images . ImageAI The model used is RetinaNet, And provide the model file that has been trained .
Again , It only takes a few lines of code to complete the process of object detection .
ImageAI github Address
Preparatory work and ImageAI The installation of can be seen in the previous article ImageAI ( One )
Use version ImageAI-2.1.0
Object Detection
Well trained RetinaNet Model :RetinaNet
detection.py
from imageai.Detection import ObjectDetection
import os
import time
# timing
start = time.time()
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
# Load trained files
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
# Save the test result as a new picture
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image3.jpg"), output_image_path=os.path.join(execution_path , "image3new.jpg"))
# Closing time
end = time.time()
for eachObject in detections:
print(eachObject["name"] ," : " ,eachObject["percentage_probability"] , " : ", eachObject["box_points"] ) ## Predicted object name : Prediction probability : Two point coordinates of the object ( Top left , The lower right )
print("--------------------------------")
print ("\ncost time:",end-start)
Here is github Pictures provided
The result of my running
person : 76.33113265037537 : [463 139 512 221]
--------------------------------
person : 83.83048176765442 : [600 131 639 213]
--------------------------------
person : 96.08134031295776 : [534 102 579 225]
--------------------------------
person : 96.66982889175415 : [ 8 103 63 248]
--------------------------------
motorcycle : 71.07154726982117 : [273 180 346 306]
--------------------------------
dog : 94.58073377609253 : [398 314 444 433]
--------------------------------
car : 55.41401505470276 : [215 140 388 299]
--------------------------------
person : 86.19718551635742 : [154 145 255 393]
--------------------------------
cost time: 13.959604263305664
A picture I found casually
result
person : 61.145514249801636 : [175 580 202 650]
--------------------------------
person : 63.822001218795776 : [210 591 237 680]
--------------------------------
person : 93.19865703582764 : [314 592 345 688]
--------------------------------
person : 71.38553857803345 : [ 54 585 103 682]
--------------------------------
person : 80.48739433288574 : [272 594 309 692]
--------------------------------
motorcycle : 71.90784811973572 : [ 36 626 98 704]
--------------------------------
handbag : 55.64073324203491 : [818 654 876 764]
--------------------------------
person : 92.45884418487549 : [650 575 774 793]
--------------------------------
person : 83.37823152542114 : [787 597 880 796]
--------------------------------
person : 96.3289201259613 : [1096 586 1204 790]
--------------------------------
cost time: 15.170496702194214
complete !