微信公众号:小白图像与视觉
关于技术、关注
yysilence00
。有问题或建议,请公众号留言。
主题:使用YOLOv2和MobileNet_SSD检测算法与KCF对象跟踪器进行视频流的实时对象跟踪和检测
1、版本:opencv3.4.1 numpy imutils
2、载权重并将其放置在model_data /中
3、项目结构:
- media:存放视频文件
- misc:存放结构文件动图
- model_data:存放模型文件,这里使用coco和mobilenet数据集进行训练和测试,利用caffe框架进行网络参数训练获取模型
- out:存放结果
- src:存放源码
使用方法:先cd到项目文件中路径中,然后执行
python src/main.py -h
usage: main.py [-h] [--input INPUT] [--output OUTPUT] --model MODEL
[--config CONFIG] [--classes CLASSES] [--thr THR]
Object Detection and Tracking on Video Streams
optional arguments:
-h, --help show this help message and exit
--input INPUT Path to input image or video file. Skip this argument to
capture frames from a camera.
--output OUTPUT Path to save output as video file. Skip this argument if
you don't want the output to be saved.
--model MODEL Path to a binary file of model that contains trained weights.
It could be a file with extensions .caffemodel (Caffe) or
.weights (Darknet)
--config CONFIG Path to a text file of model that contains network
configuration. It could be a file with extensions
.prototxt (Caffe) or .cfg (Darknet)
--classes CLASSES Optional path to a text file with names of classes to
label detected objects.
--thr THR Confidence threshold for detection. Default: 0.35
然后在终端执行:
- yolov2
usge0:python src/main.py --model model_data/yolov2.weights --config model_data/yolov2.cfg --classes model_data/coco_classes.txt --input media/sample_video.mp4 --output out/sample_output.avi
-mobilenet_ssd
usge1:python src/main.py --model model_data/MobileNetSSD_deploy.caffemodel --config model_data/MobileNetSSD_deploy.prototxt --classes model_data/MobileNet_classes.txt --input media/sample_video.mp4 --output out/sample_output.avi
4、带有KCF跟踪器的YOLOv2
5、带有KCF跟踪器的MobileNet_SSD
6、完整项目代码
main.py
#usge0:python src/main.py --model model_data/yolov2.weights --config model_data/yolov2.cfg --classes model_data/coco_classes.txt --input media/sample_video.mp4 --output out/sample_output.avi
#usge1:python src/main.py --model model_data/MobileNetSSD_deploy.caffemodel --config model_data/MobileNetSSD_deploy.prototxt --classes model_data/MobileNet_classes.txt --input media/sample_video.mp4 --output out/sample_output.avi
"""
Author: Apoorva Vinod Gorur
Email: apoorva.v94@gmail.com
update Vesion:
Author: YanYong
Email: yanyong.email@gmail.com
"""
import cv2 as cv
import argparse
import sys
import numpy as np
import time
from copy import deepcopy
import imutils
from object_detection import object_detector
#绘制预测框图
def drawPred(frame, objects_detected):
objects_list = list(objects_detected.keys())
for object_, info in objects_detected.items():
box = info[0]
confidence = info[1]
label = '%s: %.2f' % (object_, confidence)
p1 = (int(box[0]), int(box[1]))
p2 = (int(box[0] + box[2]), int(box[1] + box[3]))
#p1=(x=box[0],y=box[1])是矩阵左上点的坐标,w=box[2],h=box[3]是矩阵的宽和高 p2 =(box[0] + box[2]), int(box[1] + box[3])为右下角坐标
cv.rectangle(frame, p1, p2, (<