程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Imageai (III) use Python to quickly and easily realize video object detection and tracking

編輯:Python

The first two articles have explained ImageAI Methods to realize image prediction and image object detection , Now let's talk about ImageAI The third function of video object detection .
Preparatory work and ImageAI The installation of can be seen in the previous article Image Prediction: ImageAI ( One )
Object Detection:ImageAI ( Two )

This article github Official address :https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Detection/VIDEO.md


Video Object Detection and Tracking

Here it provides three different models for us to choose :(ImageAI At least update to 2.0.2 The last two models are new
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)

The code is as follows It's simple

from imageai.Detection import VideoObjectDetection
import os
import time
# timing
start = time.time()
# Current file directory
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsTinyYOLOv3() # Set the model to be used
detector.setModelPath( os.path.join(execution_path, "yolo-tiny.h5")) # Load the trained model data
detector.loadModel()
# Set the input video address Output address Frames per second, etc
video_path = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "traffic.mp4"), output_file_path=os.path.join(execution_path, "traffic_detected"), frames_per_second=20, log_progress=True)
print(video_path)
# Closing time
end = time.time()
print ("\ncost time:",end-start)

Finally, we can get the detected video file traffic_detected.mp4

The screenshot is as follows :


Custom Video Object Detection Custom video object detection
ImageAI Model of (RetinaNet), It can detect 80 Different types of objects . Include :

person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, fire hydrant, stop_sign, parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports ball, kite, baseball bat, baseball glove, skateboard, surfboard, tennis racket, bottle, wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot dog, pizza, donot, cake, chair, couch, potted plant, bed, dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear, hair dryer, toothbrush.

Part of the code is as follows :

execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
custom_objects = detector.CustomObjects(person=True, bicycle=True, motorcycle=True)
video_path = detector.detectCustomObjectsFromVideo(custom_objects=custom_objects, input_file_path=os.path.join(execution_path, "traffic.mp4"), output_file_path=os.path.join(execution_path, "traffic_custom_detected"), frames_per_second=20, log_progress=True)
print(video_path)

You can see one more custom_objects = detector.CustomObjects(person=True, bicycle=True, motorcycle=True)
Here we define the objects to be detected ,person,bicycle as well as motorcycle
And then in detector.detectCustomObjectsFromVideo Added a parameter to custom_objects=custom_objects

Only those customized objects will be detected in the video generated in this way !
The screenshot is as follows :

It can be seen that the above figure does not detect objects such as cars Only detect except person as well as motorcycle


Video Detection Speed Video detection speed

stay load When adding parameters detection_speed

detector.loadModel(detection_speed="fast")

The following is the official data :
The length of the video = 1 minute 24 second , Detection speed =”normal”, Minimum percentage probability = 50( The default value is ), Test time = 29 minute 3 second
The length of the video = 1 minute 24 second , Detection speed =”fast”, Minimum percentage probability = 40, Test time = 11 minute 6 second
The length of the video = 1 minute 24 second , Detection speed =”faster”, Minimum percentage probability = 30, Test time = 7 branch 47 second
The length of the video = 1 minute 24 second , Detection speed =”fastest”, Minimum percentage probability = 20, Test time = 6 minute 20 second
The length of the video = 1 minute 24 second , Detection speed =”flash”, Minimum percentage probability = 10, Test time = 3 branch 55

The code and model files are placed on the network disk , If necessary, you can download it yourself
link : https://pan.baidu.com/s/1kgvu_95U7f6AB1rJkpm9Cg Extraction code : r3n6


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved