使用Object Dectection API识别视频

使用Object Dectection API识别视频

先放一段识别效果

主要参考:https://blog.csdn.net/xiaoxiao123jun/article/details/76605928

安装OpenCV2
下载
访问https://pypi.python.org/pypi/opencv-python,下方有个列表,根据自己的系统选择,下面是我的选择
这里写图片描述

上传
使用Object Dectection API识别视频_第1张图片
安装
使用Object Dectection API识别视频_第2张图片
检查
使用Object Dectection API识别视频_第3张图片
安装imageio、moviepy
用pip安装即可

视频识别相关代码
把以下每段代码加入notebook,最后notebook看起来像这样
使用Object Dectection API识别视频_第4张图片

最开始引入cv2
使用Object Dectection API识别视频_第5张图片



# Import everything needed to edit/save/watch video clips
import imageio
imageio.plugins.ffmpeg.download()

from moviepy.editor import VideoFileClip
from IPython.display import HTML

def detect_objects(image_np, sess, detection_graph):
    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=0)
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Each box represents a part of the image where a particular object was detected.
    boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    # Each score represent how level of confidence for each of the objects.
    # Score is shown on the result image, together with the class label.
    scores = detection_graph.get_tensor_by_name('detection_scores:0')
    classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    # Actual detection.
    (boxes, scores, classes, num_detections) = sess.run(
        [boxes, scores, classes, num_detections],
        feed_dict={image_tensor: image_np_expanded})

    # Visualization of the results of a detection.
    vis_util.visualize_boxes_and_labels_on_image_array(
        image_np,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8)
    return image_np

def process_image(image):
    # NOTE: The output you return should be a color image (3 channel) for processing video below
    # you should return the final output (image with lines are drawn on lanes)
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            image_process = detect_objects(image, sess, detection_graph)
            return image_process

white_output = 'video1_out.mp4'
clip1 = VideoFileClip("video1.mp4").subclip(20,50)
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!s
%time white_clip.write_videofile(white_output, audio=False)
from moviepy.editor import *
clip1 = VideoFileClip("video1_out.mp4")
clip1.write_gif("final.gif")

运行
把vedio1.mp4置于服务器的object_dectection目录下
在notebook中cell-run all
运行时间较长,一个是下载ffmpeg,这个下载可能比较慢,会失败
使用Object Dectection API识别视频_第6张图片
(我曾经自己下载,但放到服务器的位置不对),最后还是它自己下载成功,注意提示放置的位置。
这里写图片描述
另一个是处理视频(处理一个90秒的视频耗费将近2小时),页面上会有进度显示
使用Object Dectection API识别视频_第7张图片
结束后
使用Object Dectection API识别视频_第8张图片
服务器上会有输出
这里写图片描述

看看
使用Object Dectection API识别视频_第9张图片


后记
尝试生成30秒的gif,发现200M
使用Object Dectection API识别视频_第10张图片

你可能感兴趣的:(机器学习)