ImageAI:视频对象检测和跟踪(预览版)

2020-07-22  本文已影响0人  程序员Ameng

ImageAI 提供方便,灵活和强大的方法来对视频进行对象检测和跟踪。目前仅支持当前最先进的 RetinaNet 算法进行对象检测和跟踪,后续版本会加入对其他算法的支持。虽然这只是预览版本,但提供了很多令人难以置信的选项。在开始视频对象检测和跟踪任务前,您必须通过以下链接下载 RetinaNet 模型文件:

由于视频对象检测是非常消耗硬件资源的任务,所以我们建议您使用安装了 NVIDIA GPU 和 GPU 版 Tensorflow 的计算机来完成此实验。使用CPU进行视频对象检测将比使用 NVIDIA GPU 驱动的计算机慢。您也可以使用 Google Colab 进行此实验,因为它具有可用的 NVIDIA K80 GPU。下载 RetinaNet 模型文件后,应将模型文件复制到.py文件所在的项目文件夹中。然后创建一个python文件并为其命名; 例如 FirstVideoObjectDetection.py 。然后将下面的代码写入python文件中:

from imageai.Detection import VideoObjectDetection
import os
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel(detection_speed="fastest")
custom_objects = detector.CustomObjects(person=True, car=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, frame_detection_interval=5, minimum_percentage_probability=50)
print(video_path)

输入的视频截图


输出的视频截图


文档

imageai.Detection.VideoObjectDetection class
在任何的Python程序中通过实例化VideoObjectDetection类并调用下面的函数即可进行视频对象检测:

上一篇下一篇

猜你喜欢

热点阅读