caffe-ssd版本的代码中 /examples下有ssd_detect.ipynd的图像检测实例
/examples/ssd/下有ssd_pascal_video.py的视频检测实例,但没有保存视频接口

博主将图像检测的修改为为视频接口并保存,代码如下:
github地址:https://github.com/huangzicheng/caffe-ssd-video-detect

import numpy as np
import matplotlib.pyplot as plt
import cv2
#%matplotlib inline
#from __future__ import *
#plt.rcParams['figure.figsize'] = (10, 10)
#plt.rcParams['image.interpolation'] = 'nearest'
#plt.rcParams['image.cmap'] = 'gray'# Make sure that caffe is on the python path:
caffe_root = '/home/xxx/caffe-ssd'  # this file is expected to be in {caffe_root}/examples
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, 'python')import caffe
caffe.set_device(0)
caffe.set_mode_gpu()from google.protobuf import text_format
from caffe.proto import caffe_pb2# load PASCAL VOC labels
labelmap_file = '/home/xxx/caffe-ssd/VOCdevkit/labelmap_voc.prototxt'
file = open(labelmap_file, 'r')
labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), labelmap)def get_labelname(labelmap, labels):num_labels = len(labelmap.item)labelnames = []if type(labels) is not list:labels = [labels]for label in labels:found = Falsefor i in xrange(0, num_labels):if label == labelmap.item[i].label:found = Truelabelnames.append(labelmap.item[i].display_name)breakassert found == Truereturn labelnamesmodel_def = 'models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
model_weights = 'models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_31292.caffemodel'net = caffe.Net(model_def,      # defines the structure of the modelmodel_weights,  # contains the trained weightscaffe.TEST)     # use test mode (e.g., don't perform dropout)# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', np.array([104,117,123])) # mean pixel
transformer.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB# set net to batch size of 1
image_resize = 300
net.blobs['data'].reshape(1,3,image_resize,image_resize)outputFile='/home/xxx/pic_test/out1.avi'#cap = cv2.VideoCapture("Minions_banana.mp4")
import math
cap = cv2.VideoCapture('/xxx/pic_test/cars.mp4')
vid_writer = cv2.VideoWriter(outputFile, cv2.VideoWriter_fourcc('M','J','P','G'), 28, (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))while cap.isOpened():ret, image1 = cap.read()if ret == True:#cv2.imshow('img',image)#image = caffe.io.load_image('/home/huanglong/002795.jpg')#plt.imshow(image)# image1=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)img = image1/ 255.0transformed_image = transformer.preprocess('data', img)print transformed_image.shapenet.blobs['data'].data[...] = transformed_image# Forward pass.detections = net.forward()['detection_out']# Parse the outputs.()['detection_out']det_label = detections[0,0,:,1]det_conf = detections[0,0,:,2]det_xmin = detections[0,0,:,3]det_ymin = detections[0,0,:,4]det_xmax = detections[0,0,:,5]det_ymax = detections[0,0,:,6]# Get detections with confidence higher than 0.6.top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]top_conf = det_conf[top_indices]top_label_indices = det_label[top_indices].tolist()top_labels = get_labelname(labelmap, top_label_indices)top_xmin = det_xmin[top_indices]top_ymin = det_ymin[top_indices]top_xmax = det_xmax[top_indices]top_ymax = det_ymax[top_indices]#colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()colors=[(0,0,0),(128,0,0),(0,128,0)]#fig=plt.figure()#currentAxis = plt.gca()for i in xrange(top_conf.shape[0]):xmin = int(round(top_xmin[i] * image1.shape[1]))ymin = int(round(top_ymin[i] * image1.shape[0]))xmax = int(round(top_xmax[i] * image1.shape[1]))ymax = int(round(top_ymax[i] * image1.shape[0]))print xmin, ymin, xmax, ymaxscore = top_conf[i]label = int(top_label_indices[i])label_name = top_labels[i]display_txt = '%s: %.2f'%(label_name, score)coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1print coords,color = colors[label]cv2.rectangle(image1, (xmin,ymin), (xmax,ymax), color, 1)#currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))#currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5}) font = cv2.FONT_HERSHEY_COMPLEXcv2.putText(image1, display_txt, (xmin,ymin), font, 2, color, 2)#cv2.imshow('img',image1) vid_writer.write(image1)if cv2.waitKey(1000) & 0xFF == ord('q'):break#
cap.release()
cv2.destroyAllWindows()

caffe-ssd 检测视频并保存相关推荐

  1. caffe ssd 测试demo,检测单张图片

    原 SSD: Single Shot MultiBox Detector 检测单张图片 2016年10月29日 16:39:05 阅读数:19930 标签: python ssd ssd-detect ...

  2. yolov3运行及保存检测视频(包括摄像头)

    新代码:可用,如果不想在老代码上折腾更改,可以直接根据下面的新地址拉取代码.  (注意看Readme) --------------------更新20180703----------------- ...

  3. 边缘设备上的实时AI人员检测:检测视频中的人员

    下载数据-19.3 MB 下载模型-43.5 MB 下载结果-36.66 MB 从本系列的前几篇文章中,我们获得了使用SSD DNN模型检测图像中人物的Python代码.而且我们已经展示了该代码可以在 ...

  4. Caffe SSD Ubuntu16 04 训练自己的数据集

    总的来说,Caffe 是一个比较难上手的框架.这次尝试训练 Caffe 框架下 SSD 模型的训练是我第一次使用 Caffe 框架.下面就说一说我踩过的几个坑,希望能够帮助到大家. 1 编译 Caff ...

  5. matlab ssd检测,基于SSD神经网络的违章停车车辆实时检测方法与流程

    本发明属于图像识别和计算机视觉技术领域,尤其涉及一种停车车辆的检测方法,可用于城市环境中对违章停车车辆的检测. 背景技术: 随着现代社会经济的快速发展和城市化的普及,汽车作为一种重要的交通工具,其数量 ...

  6. 【OpenCV 4开发详解】图像与视频的保存

    本文首发于"小白学视觉"微信公众号,欢迎关注公众号 本文作者为小白,版权归人民邮电出版社发行所有,禁止转载,侵权必究! 经过几个月的努力,小白终于完成了市面上第一本OpenCV 4 ...

  7. 一步一步带你训练自己的SSD检测算法

    目录 一.前言 二.实现细节 1.前提条件 2.数据标注 2.1 Labelme 2.1.1 工具特点简介 2.1.2 工具安装 2.1.3 工具使用简介 2.2 LabelImg 2.2.1 工具安 ...

  8. OpenCV4每日一练day7:视频加载、摄像头调用、视频文件保存

    一.视频加载(读取)/摄像头调用使用的是同一个函数:VideoCapture( ) get函数使用,如video.get(CAP_PROP_FOURCC) 视频实时显示与播放:video>> ...

  9. 基于kinect + EmguCV 的监控小应用(视频录制保存)

    kinectMonitor 源码位置 家庭监控小应用–https://gitee.com/ellecommander/kinecMonitor 介绍 基于kinect的家庭监控系统,主要是为了监控我爸 ...

最新文章

  1. 组原-OS-政治截图
  2. 四、Android学习第四天——JAVA基础回顾(转)
  3. Xshell实现Windows上传文件到Linux主机
  4. java中接口的定义与实现
  5. (二)线程同步_3---在类中使用相互独立的属性同步
  6. 【直播预告】阿里云服务网格 ASM 产品易用性改善实践与思考
  7. 关于 java 实现 语音朗读
  8. 决策树ID3算法[分类算法]
  9. libcare Hello World测试例
  10. 编译原理:用lex/flex做词法分析
  11. HDU-4313-Matrix(离线并查集)
  12. Ubuntu安装Go语言环境
  13. 利用matlab使用窗函数,Matlab中窗函数的简单使用
  14. 指付通盗刷信用卡维权连载--9月5日维权纪实
  15. vmware运行虚拟机ubuntu,电脑就重启
  16. Pr 入门教程:如何调整音频轨道音量?
  17. 视频 TS 内容详细讲解
  18. LeetCode 27 合并两个排序的链表
  19. 计算机网络要点归纳-(谢希仁版本)
  20. mysql 加密狗_IA300加密狗使用 | 学步园

热门文章

  1. 只用1个div,你能用CSS常规属性绘制:正3、4、5、6、7、8边形吗?
  2. 前端解决微信公众号图片引用错误
  3. 微信小程序发起用户授权
  4. python epub解析_python 解析电子书的信息
  5. HTTP请求方式GET/POST
  6. electron 实现截图功能
  7. RFID犯人手环点名定位系统管理方案
  8. 安全评价师如何刷题学习软件考题宝 JavaScript Switch 语句
  9. selenium和webdriver区别
  10. 关于java.util.LinkedHashMap cannot be cast to 实体类......问题的出现以及解决办法