很久没有写CV方面的文章了,最近笔者接触了一段时间的目标检测的工具:darknet,因此希望能写篇文章记录下,同时希望自己能在目标检测有更深一步的研究。
  目标检测是计算机视觉和数字图像处理的一个热门方向,广泛应用于机器人导航、智能视频监控、工业检测、航空航天等诸多领域,通过计算机视觉减少对人力资本的消耗,具有重要的现实意义。目标检测即找出图像中所有感兴趣的物体,包含物体定位和物体分类两个子任务,同时确定物体的类别和位置。图像分类的任务我们已经熟悉了,而目标检测与图片分类的不同之处在于,目标检测需要检测出图片中我们感兴趣的物体,同时检测出其边界并给出类别。
  目前主流的目标检测算法主要是基于深度学习模型,大概可以分成两大类别:

  1. One-Stage目标检测算法,这类检测算法不需要产生候选区域(Region Proposal)阶段,可以通过一个Stage直接产生物体的类别概率和位置坐标值,比较典型的算法有YOLO、SSD和CornerNet;
  2. Two-Stage目标检测算法,这类检测算法将检测问题划分为两个阶段,第一个阶段首先产生候选区域,包含目标大概的位置信息,然后第二个阶段对候选区域进行分类和位置精修,这类算法的典型代表有R-CNN,Fast R-CNN,Faster R-CNN等。

目标检测模型的主要性能指标是检测准确度和速度,其中准确度主要考虑物体的定位以及分类准确度。一般情况下,Two-Stage算法在准确度上有优势,而One-Stage算法在速度上有优势。
  darknet是一个开源的深度学习框架,它实现了YOLO算法,官方网站地址为:https://pjreddie.com/darknet/yolo/。基于它,我们能够很多有意思的事情。本文的后续部分将会给出darknet在图片和视频中进行目标检测的例子。

图像中的目标检测

  这部分将会给出darknet在图片中进行目标检测的例子。
  首先我们先把darknet的源码下载到本地:

git clone https://github.com/pjreddie/darknet.git

  然后我们下载已经预先训练好的YOLO3算法的模型yolov3,下载网址为:https://pjreddie.com/media/files/yolov3.weights,下载文件放在backup文件夹下。注意,该模型是在COCO数据集上训练的结果,COCO数据集包含80个类别,比如人、狗、车、猫等。
  接着我们需要输入make命令进行编译,这会生成darnet可执行文件。
  接着我们在网上找一张图片,对这张图片(位于data文件夹下的dog_person.jpeg)进行目标检测,输入命令如下:

./darknet detect cfg/yolov3.cfg backup/yolov3.weights data/dog_person.jpeg

在本地的本地环境(CPU)中,输出结果如下:

Loading weights from backup/yolov3.weights...Done!
data/dog_person.jpeg: Predicted in 19.373125 seconds.
person: 96%
person: 91%
person: 87%
person: 100%
person: 100%
person: 99%
person: 99%
dog: 100%

同时,也是生成一张预测图片predictions.jpg,内容如下:

目标检测精度提升技巧 目标检测有什么用_目标检测


虽然漏了其中的一条狗,但丝毫不影响该模型出色的目标检测的能力!这个结果对于初次接触目标检测的我是如此地让人兴奋!

视频中的目标检测

  接着是更为令人兴奋的部分。我们将在视频中进行目标检测。
  由于笔者初次接触darknet,对很多内容还不是很熟悉,因此,借鉴别人已经写好的代码进行视频中的目标检测。
  Python代码(detect_video.py)如下:

# import the necessary packages
import numpy as np
import argparse
import imutils
import time
import cv2
import os

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True,
    help="path to input video")
ap.add_argument("-o", "--output", required=True,
    help="path to output video")
ap.add_argument("-y", "--yolo", required=True,
    help="base path to YOLO directory")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
    help="minimum probability to filter weak detections")
ap.add_argument("-t", "--threshold", type=float, default=0.3,
    help="threshold when applyong non-maxima suppression")
args = vars(ap.parse_args())

# load the COCO class labels our YOLO model was trained on
labelsPath = os.path.sep.join([args["yolo"], "coco.names"])
LABELS = open(labelsPath).read().strip().split("\n")
# initialize a list of colors to represent each possible class label
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8")
# derive the paths to the YOLO weights and model configuration
weightsPath = os.path.sep.join([args["yolo"], "yolov3.weights"])
configPath = os.path.sep.join([args["yolo"], "yolov3.cfg"])
# load our YOLO object detector trained on COCO dataset (80 classes)
# and determine only the *output* layer names that we need from YOLO
print("[INFO] loading YOLO from disk...")
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]

# initialize the video stream, pointer to output video file, and
# frame dimensions
vs = cv2.VideoCapture(args["input"])
writer = None
(W, H) = (None, None)
# try to determine the total number of frames in the video file
try:
    prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() \
        else cv2.CAP_PROP_FRAME_COUNT
    total = int(vs.get(prop))
    print("[INFO] {} total frames in video".format(total))
# an error occurred while trying to determine the total
# number of frames in the video file
except Exception:
    print("[INFO] could not determine # of frames in video")
    print("[INFO] no approx. completion time can be provided")
    total = -1

# loop over frames from the video file stream
while True:
    # read the next frame from the file
    (grabbed, frame) = vs.read()
    # if the frame was not grabbed, then we have reached the end
    # of the stream
    if not grabbed:
        break
    # if the frame dimensions are empty, grab them
    if W is None or H is None:
        (H, W) = frame.shape[:2]

    # construct a blob from the input frame and then perform a forward
    # pass of the YOLO object detector, giving us our bounding boxes
    # and associated probabilities
    blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
    net.setInput(blob)
    start = time.time()
    layerOutputs = net.forward(ln)
    end = time.time()
    # initialize our lists of detected bounding boxes, confidences,
    # and class IDs, respectively
    boxes = []
    confidences = []
    classIDs = []

    # loop over each of the layer outputs
    for output in layerOutputs:
        # loop over each of the detections
        for detection in output:
            # extract the class ID and confidence (i.e., probability)
            # of the current object detection
            scores = detection[5:]
            classID = np.argmax(scores)
            confidence = scores[classID]
            # filter out weak predictions by ensuring the detected
            # probability is greater than the minimum probability
            if confidence > args["confidence"]:
                # scale the bounding box coordinates back relative to
                # the size of the image, keeping in mind that YOLO
                # actually returns the center (x, y)-coordinates of
                # the bounding box followed by the boxes' width and
                # height
                box = detection[0:4] * np.array([W, H, W, H])
                (centerX, centerY, width, height) = box.astype("int")
                # use the center (x, y)-coordinates to derive the top
                # and and left corner of the bounding box
                x = int(centerX - (width / 2))
                y = int(centerY - (height / 2))
                # update our list of bounding box coordinates,
                # confidences, and class IDs
                boxes.append([x, y, int(width), int(height)])
                confidences.append(float(confidence))
                classIDs.append(classID)

    # apply non-maxima suppression to suppress weak, overlapping
    # bounding boxes
    idxs = cv2.dnn.NMSBoxes(boxes, confidences, args["confidence"], args["threshold"])
    # ensure at least one detection exists
    if len(idxs) > 0:
        # loop over the indexes we are keeping
        for i in idxs.flatten():
            # extract the bounding box coordinates
            (x, y) = (boxes[i][0], boxes[i][1])
            (w, h) = (boxes[i][2], boxes[i][3])
            # draw a bounding box rectangle and label on the frame
            color = [int(c) for c in COLORS[classIDs[i]]]
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
            cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

    # check if the video writer is None
    if writer is None:
        # initialize our video writer
        fourcc = cv2.VideoWriter_fourcc(*"MJPG")
        writer = cv2.VideoWriter(args["output"], fourcc, 30, (frame.shape[1], frame.shape[0]), True)
        # some information on processing single frame
        if total > 0:
            elap = (end - start)
            print("[INFO] single frame took {:.4f} seconds".format(elap))
            print("[INFO] estimated total time to finish: {:.4f}".format(elap * total))
    # write the output frame to disk
    writer.write(frame)
# release the file pointers
print("[INFO] cleaning up...")
writer.release()
vs.release()

我们把coco.names和yolov3.cfg文件都放在backup文件夹下。然后,我们再输入下面的命令(注意:jishengchong.mp4为输入视频文件,位于data文件夹下,jishengchong.avi为输出文件,里面包含目标检测的结果):

python3 detect_video.py --input ../data/jishengchong.mp4 --output ../data/jishengchong.avi --yolo ../backup

由于视频检测太费时间,因此,我在这里只给出该视频的前10秒的检测结果,如下:

可以参考微信公众号上的文章:https://mp.weixin.qq.com/s?__biz=MzU2NTYyMDk5MQ==&mid=2247484606&idx=1&sn=52862414c7689b53a58941651f7f49a0&chksm=fcb9bd2ecbce34385b92d32625c819ce28f0e41af2eaf4a6df38cfb71651bdb293e4c4cfeb5f&token=679045967&lang=zh_CN#rd

总结

  本次作为笔者入门目标检测的第一篇文章,希望以后也能在这个方向又一定深度的研究。