1. 程式人生 > >Opencv對視訊進行目標檢測

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,或者與老版本衝突,可以解除安裝老版本和相對應的版本依賴。

參考文獻