用ImageAI将普通视频转换为带目标检测的视频

之前我发表过一篇博客,是使用opencv将普通视频转为带人脸检测效果的视频

https://blog.****.net/luohualiushui1/article/details/86661501

现在选用一个开源项目ImageAI将普通视频转换为带目标检测的视频,目标检测包括人形检测和一些普通的物品检测。也就是说,我们会把一个普通的视频转换为一个里面的人形和物品画上框加上标注的视频。

首先大家了解一下ImageAI开源项目https://github.com/OlafenwaMoses/ImageAI

它的说明文档https://imageai-cn.readthedocs.io/zh_CN/latest/ImageAI_Object_Detection.html

该开源项目基于tensorflow,它里面使用了好几种目标检测的模型都是源于tensorflow提供的训练模型。

例如:RetinaNet, YOLOv3 和 TinyYOLOv3

现在开始正题

第一步在ubuntu上搭建相关环境,按照ImageAI项目的指引,给python安装依赖,如下:

pip3 install --upgrade tensorflow 
pip3 install numpy 
pip3 install scipy 
pip3 install opencv-python 
pip3 install pillow 
pip3 install matplotlib 
pip3 install h5py 
pip3 install keras 

第二步安装ImageAI

pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/imageai-2.0.2-py3-none-any.whl

第三步在PyCharm上新建工程引入ImageAI。

第四步ImageAI对视频处理封装的比较好,只需要如下处理即可:

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()

video_path = detector.detectObjectsFromVideo(input_file_path="/usr/local/video/229728.mp4", output_file_path="/usr/local/video/229728_p", frames_per_second=20, log_progress=True)
print(video_path)

运行起来相当花时间,资源消耗也不低,在长时间的运行之后才输出处理后的视频

效果如下图:

用ImageAI将普通视频转换为带目标检测的视频

用ImageAI将普通视频转换为带目标检测的视频

ok生成完毕,但是大家可以发现检测效果还好,但生成的视频色泽相当于原视频发生了变化,人脸部偏蓝。