faster RCNN视频目标检测

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

代码模块详细加载参考本人博客:https://blog.csdn.net/LearnToPain/article/details/104809107
代码:

#-*-coding:utf-8-*-
import tensorflow as tf
import cv2
import  numpy as np
import matplotlib.pyplot as plt
import label_map_util
import visualization_utils as vis_util
import os
from moviepy.editor import VideoFileClip
video_path=r'./video/叶问4x.mp4'
path=r'./mscoco_label_map.pbtxt'
label_map = label_map_util.load_labelmap(path)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

with tf.gfile.FastGFile(r'frozen_inference_graph.pb',mode='rb') as f:
    graph_def=tf.GraphDef() #图定义类对象
    graph_def.ParseFromString(f.read()) #字符串转对象
    tf.import_graph_def(graph_def, name='')  # 对象图导入sess
    def proc(image):
        image=np.array(image)
        image1=image[None]
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            grapha = sess.graph
            image_tensor = grapha.get_tensor_by_name('image_tensor:0')
            boxes = grapha.get_tensor_by_name('detection_boxes:0')
            scores = grapha.get_tensor_by_name('detection_scores:0')
            classes = grapha.get_tensor_by_name('detection_classes:0')
            num_detections = grapha.get_tensor_by_name('num_detections:0')
            (boxes, scores, classes, num_detections) = sess.run(
                [boxes, scores, classes, num_detections],
                feed_dict={image_tensor: image1})
            vis_util.visualize_boxes_and_labels_on_image_array(image,
                                                      np.squeeze(boxes),
                                                      np.squeeze(classes).astype(np.int32),
                                                      np.squeeze(scores),
                                                      category_index,
                                                      use_normalized_coordinates=True)#对图片的处理标识出目标
            return image


    clip1 = VideoFileClip(video_path).subclip(28, 30)
    white_clip=clip1.fl_image(proc)
    white_clip.write_videofile(video_path,audio=False)

效果,视频长跑很久,于是弄了2秒的视频
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值