美文网首页
ImageAI:视频对象检测和跟踪(预览版)

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

作者: 程序员Ameng | 来源:发表于2020-07-22 16:21 被阅读0次

    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类并调用下面的函数即可进行视频对象检测:

    • setModelTypeAsRetinaNet() 如果您选择使用RetinaNet 模型文件来进行对象检测,你只需调用一次该函数。

    • setModelPath() 该函数用于设定模型文件的路径。模型文件必须与您设置的模型类型相对应。loadModel() 该函数用于载入模型。该函数接收一个prediction_speed参数。该参数用于指定对象检测的速度模式,当速度模式设置为’fastest’时预测时间可缩短60%左右,具体取决于图像的质量。
      detection_speed(可选); 可接受的值是”normal”, “fast”, “faster” and “fastest”

    • detectObjectsFromVideo() 此函数用于通过接收以下参数来进行视频对象检测:
      input_file_path,该参数用于指定输入视频的文件路径output_file_path,该参数用于指定输出视频的文件路径
      frames_per_second 该参数用于指定输出视频中的每秒帧数fpsframe_detection_interval(可选,默认为1)),该参数用于指定视频检测的帧间隔,即间隔多少帧检测一次。
      minimum_percentage_probability(可选,默认为50),用于设定预测概率的阈值,只有当百分比概率大于等于该值时才会返回检测到的对象。
      log_progress(可选),该参数用于指定是否将检测进度输出到控制台

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/zhltlktx.html