使用Object Dectection API识别视频

使用Object Dectection API识别视频

先放一段识别效果
这里写图片描述

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

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

上传
这里写图片描述
安装
这里写图片描述
检查
这里写图片描述
安装imageio、moviepy
用pip安装即可

视频识别相关代码
把以下每段代码加入notebook,最后notebook看起来像这样
这里写图片描述

最开始引入cv2
这里写图片描述



# 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,这个下载可能比较慢,会失败
这里写图片描述
(我曾经自己下载,但放到服务器的位置不对),最后还是它自己下载成功,注意提示放置的位置。
这里写图片描述
另一个是处理视频(处理一个90秒的视频耗费将近2小时),页面上会有进度显示
这里写图片描述
结束后
这里写图片描述
服务器上会有输出
这里写图片描述

看看
这里写图片描述


后记
尝试生成30秒的gif,发现200M
这里写图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值