ImageAI 提供方便,灵活和强大的方法来对视频进行对象检测和跟踪。目前仅支持当前最先进的 RetinaNet 算法进行对象检测和跟踪,后续版本会加入对其他算法的支持。虽然这只是预览版本,但提供了很多令人难以置信的选项。在开始视频对象检测和跟踪任务前,您必须通过以下链接下载 RetinaNet 模型文件:
- RetinaNet (文件大小=145MB)
由于视频对象检测是非常消耗硬件资源的任务,所以我们建议您使用安装了 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类并调用下面的函数即可进行视频对象检测:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。