1. 程式人生 > >ImageAI (三) 使用Python快速簡單實現視訊中物體檢測 Video Object Detection and Tracking

ImageAI (三) 使用Python快速簡單實現視訊中物體檢測 Video Object Detection and Tracking

前兩篇已經講解了ImageAI實現圖片預測以及圖片物體檢測的方法,現在再來講解一下ImageAI的第三個功能視訊中的物體檢測。
準備工作以及ImageAI的安裝可以詳見上一篇 Image Prediction: ImageAI (一)
Object Detection:ImageAI (二)

Video Object Detection and Tracking

這裡它提供了三種不同的模型供我們選擇:(ImageAI至少需要更新到2.0.2 後兩個模型是新加的
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)

程式碼如下 很簡單

from imageai.Detection import VideoObjectDetection
import os
import time
#計時
start = time.time()

 #當前檔案目錄
execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsTinyYOLOv3() #設定需要使用的模型 detector.setModelPath( os.path.join(execution_path, "yolo-tiny.h5")) #載入已經訓練好的模型資料 detector.loadModel() #設定輸入視訊地址 輸出地址 每秒幀數等 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) #結束計時 end = time.time() print ("\ncost time:",end-start)

最終可以得到檢測好的視訊檔案traffic_detected.mp4

截圖如下:
1

2

3

Custom Video Object Detection 自定義視訊物件檢測
ImageAI 的模型(RetinaNet),可以檢測 80 種不同型別的物件。包括:

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.

部分程式碼如下:

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)

可以看到多了一條custom_objects = detector.CustomObjects(person=True, bicycle=True, motorcycle=True)
這裡定義了需要檢測的物體,person,bicycle以及motorcycle
然後在detector.detectCustomObjectsFromVideo中添加了一個引數custom_objects=custom_objects

通過這樣生成的視訊中就只會檢測自定義的那些物體了!
截圖如下:
4
可以看到上圖已經不檢測汽車這些物體了 只檢測除了person以及motorcycle

Video Detection Speed 視訊檢測速度

在load的時候新增引數detection_speed

detector.loadModel(detection_speed="fast")

以下是官方提供的資料:
視訊長度= 1分鐘24秒,檢測速度=”normal”,最小百分比概率= 50(預設值),檢測時間= 29分鐘3秒
視訊長度= 1分鐘24秒,檢測速度=”fast”,最小百分比概率= 40,檢測時間= 11分鐘6秒
視訊長度= 1分鐘24秒,檢測速度=”faster”,最小百分比概率= 30,檢測時間= 7分47秒
視訊長度= 1分鐘24秒,檢測速度=”fastest”,最小百分比概率= 20,檢測時間= 6分鐘20秒
視訊長度= 1分鐘24秒,檢測速度=”flash”,最小百分比概率= 10,檢測時間= 3分55