Opencv对视频进行目标检测

在Opencv3.3版本中集成了deeplearning功能。其实现了对caffe和tensorflow两个框架的推理,但不支持训练。本文使用caffe训练的文件对目标进行检测。整个思路是首先读取视频文件,然后加载模型文件,最后读取到视频的每一帧对其进行检测。

  1. 系统: ubuntu16.04
  2. python:2.7
  3. 模型文件:MobileNet-SSD
  4. 任意视频文件

1.安装 openCV

如果安装好了openCV可以跳过这一小节,这一小节只是把一些命令复制出来,实际安装可能会存在错误。
install opencv3.3

Step #1: Install OpenCV dependencies on Ubuntu 16.04

Ubuntu 16.04:How to install OpenCV
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential cmake pkg-config
$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install python2.7-dev python3.5-dev

Step #2: Download the OpenCV source

$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
$ unzip opencv.zip

Step #3: Setup your Python environment — Python 2.7 or Python 3

$ cd ~
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

Step #4: Configuring and compiling OpenCV on Ubuntu 16.04

$ cd ~/opencv-3.3.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
    -D BUILD_EXAMPLES=ON ..
$ make -j4
$ sudo make install
$ sudo ldconfig

Step #6: Testing your OpenCV install

$ cd ~
$ workon cv
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.0'
>>>

进行检测

需要修改代码中的几个地方,change the path “–prototxt”,”–model, videoPath”
这里写图片描述

#import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import time
import cv2

videoPath = "/home/user/Desktop/test.mp4"
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--prototxt", default='/home/user/Desktop/MobileNetSSD_deploy.prototxt',
    help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", default="/home/user/Desktop/MobileNetSSD_deploy.caffemodel",
    help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.2,
    help="minimum probability to filter weak detections")
args = vars(ap.parse_args())

# detect, then generate a set of bounding box colors for each class
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
    "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
    "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
    "sofa", "train", "tvmonitor"]

COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

# initialize the video stream, allow the cammera sensor to warmup,
# and initialize the FPS counter
print('[INFO] starting video stream...')
vs = cv2.VideoCapture(videoPath)
time.sleep(2.0)
fps = FPS().start()
print('open ',vs.isOpened())

#loop over the frames from the video stream
while True:

    #grap the frame from the threaded video stream and resize it
    #to have a maximum width of 400 pixels

    ret,frame = vs.read()
    print('shape : ',type(frame))
    if frame is None:
        break
    frame = imutils.resize(frame,width=400)

    #grab the frame dimensions and convert it to a blob
    (h,w) = frame.shape[:2]
    bolb = cv2.dnn.blobFromImage(frame,0.007843,(300,300),127.5)

    #pass the blob through the network and obtain the detections and predictions
    net.setInput(bolb)
    detections = net.forward()
    for i in np.arange(0, detections.shape[2]):
        # extract the confidence (i.e., probability) associated with the
        # prediction
        confidence = detections[0, 0, i, 2]

        # filter out weak detections by ensuring the `confidence` is
        # greater than the minimum confidence
        if confidence > args["confidence"]:
            # extract the index of the class label from the `detections`,
            # then compute the (x, y)-coordinates of the bounding box for
            # the object
            idx = int(detections[0, 0, i, 1])
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            # display the prediction
            label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
            print("[INFO] {}".format(label))
            cv2.rectangle(frame, (startX, startY), (endX, endY),
                          COLORS[idx], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(frame, label, (startX, y),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
    #show the output frame
    cv2.imshow("Frame",frame)
    key = cv2.waitKey(1)& 0xff

    if key == ord('q'):
        break
    fps.update()
fps.stop()

vs.release()
cv2.destroyAllWindows()

如果openCV加载不到视频文件,有可能是缺少相对应的ffmpeg,或者与老版本冲突,可以卸载老版本和相对应的版本依赖。

参考文献

  1. https://www.pyimagesearch.com/2017/09/11/object-detection-with-deep-learning-and-opencv/
  2. https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/
  3. https://github.com/chuanqi305/MobileNet-SSD
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值