在上一篇文章Caffe代码解读1--从一段命令行开始,我们说了如何训练一个神经网络,并以SSD网络为例,对用到的python脚本进行了分析。在这篇文章中,我们开始分析,如何使用神经网络进行目标检测,依旧是以SSD网络为例进行讲解。
打开控制台,输入两行命令:
cd /home/**(您服务器的名字)/caffe
python examples/ssd/ssd_VOC0712/ssd_VOC0712_detect.py
我们打开这份python文件看一下
'''
Author: Mr.K
You can use this script detect one image, a group of images, a video file or the video strame from a camera.
If you choose to detect the image,
After running this script, you will get images which you have choosen to detect and a detection result file.
As for images, they are annotated with blue bounding box to show the objects that your network detected.
As for the result file, its format is as followed:
file_name: the/image/path/***.jpg
para_name: xmin ymin xmax ymax label_id confidence label_name
object: **** **** **** **** ******** ********** **********
. . . . . . . .
. . . . . . . .
. . . . . . . .
object: **** **** **** **** ******** ********** **********
If you choose to detect a video or the video strame from a camera,
After running this script, you will get a video file that has annotated with blue bounding box
compute speed without storing result,using VOC2007 dataset:
video: 58fps(forward), 18fps(loop)
one image: 36fps(forward), 19fps(loop)
images: 73fps(forward), 43fps(loop)
'''
import os
import os.path
import sys
import cv2
import time
import argparse
import numpy as np
# Make sure that caffe is on the python path:
caffe_root = './'
os.chdir(caffe_root)
sys.path.insert(0, os.path.join(caffe_root, 'python'))
import caffe
from google.protobuf import text_format
from caffe.proto import caffe_pb2
from caffe.model_libs import *
####### you are supposed to run this script at the CAFFE ROOT ########################
network_name = "ssd" #your neural net name, need to config
dataset_name = "VOC0712"#your dataset name, need to config
model_name = "{}_{}".format(network_name,dataset_name)
job_dir = "examples/{}/{}".format(network_name, model_name)
prototxt_dir = "{}/prototxt".format(job_dir)
trainLog_dir = "{}/log".format(job_dir)
trainData_dir = "{}/data".format(job_dir)
trainModel_dir = "{}/model".format(job_dir)
img_save_dir = "{}/result/detect".format(job_dir)
# the caffemodel you want to use
model_weights = "{}/ssd_VOC0712_iter_120000.caffemodel".format(trainModel_dir)#need to config
deploy_net_file = "{}/deploy.prototxt".format(prototxt_dir)
# used to show label name
labelmap_file = "{}/labelmap_voc.prototxt".format(trainData_dir)
# image_resize has to be matched to the input size of your network
image_resize = 300 #need to config
# source_file can be an image file, a directory, a video file or the camera id
#sour