使用yoloV3与视像头实现实时的目标检测

第一部分:使用已有的模型实时监测目标

 第一步:根据官网安装darknet框架https://pjreddie.com/darknet/

第二步:

修改Makefie文件,由于我没有用到GPU,所以将GPU设置为0。按照我之前安装的opencv步骤安装opencv3.2.0,再将OPENCV设置为1,如果没有安装opencv打开摄像头的时候会报错。

GPU=0
CUDNN=0
OPENCV=1
OPENMP=0
DEBUG=0

按照自己电脑的配置修改完Makefile文件之后,重新编译

cd darknet

make 

 

运行程序:

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights 

使用yoloV3与视像头实现实时的目标检测_第1张图片

 

./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights -c 1

 使用yoloV3与视像头实现实时的目标检测_第2张图片

使用yoloV3与视像头实现实时的目标检测_第3张图片

 

 

第二部分:训练自己的图片集,可以参考网站:https://karbo.online/dl/yolo_starter/

第一:下载所需要的训练集,参考官网

在script/目录下有有一个voc_label.py文件,内容如下,将此文件拷贝到darknet目录下。

 

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join

sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]

classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]


def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

def convert_annotation(year, image_id):
    in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
    out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w')
    tree=ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult)==1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
        bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

wd = getcwd()

for year, image_set in sets:
    if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)):
        os.makedirs('VOCdevkit/VOC%s/labels/'%(year))
    image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
    list_file = open('%s_%s.txt'%(year, image_set), 'w')
    for image_id in image_ids:
        list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))
        convert_annotation(year, image_id)
    list_file.close()

os.system("cat 2007_train.txt 2007_val.txt 2012_train.txt 2012_val.txt > train.txt")
os.system("cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt > train.all.txt")

    image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
    list_file = open('%s_%s.txt'%(year, image_set), 'w')
    for image_id in image_ids:
        list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))
        convert_annotation(year, image_id)

读取Main下的txt文件内容

mage_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'

000012
000017
000023
000026
000032
000033
000034
000035
000036

 

将训练的图片的目录放在2007_train.txt文件中,list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'

/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000012.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000017.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000023.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000026.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000032.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000033.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000034.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000035.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000036.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000042.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000044.jpg
/home/utryjc/darknet/VOCdevkit/VOC2007/JPEGImages/000047.jpg

 

xml文件中记录了图片的标注信息,详细的标注的意义可以参见该文:https://arleyzhang.github.io/articles/1dc20586/

in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))


	VOC2007
	000012.jpg
	
		The VOC2007 Database
		PASCAL VOC2007
		flickr
		207539885
	
	
		KevBow
		?
	
	
		500
		333
		3
	
	0
	
		car
		Rear
		0
		0
		
			156
			97
			351
			270
		
	

 

out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w')  out_file文件中记录的数据如下

6 0.505 0.548048048048 0.39 0.51951951952

 

 第二步:准备权重文件

 wget https://pjreddie.com/media/files/darknet53.conv.74

 第三步:修改配置文件,文件目录:cfg/voc.data

 1 classes= 20
  2 train  = /train.txt
  3 valid  = 2007_test.txt
  4 names = data/voc.names
  5 backup = backup

 voc.names文件的内容如下所示:

aeroplane
bicycle
bird
boat
bottle
bus
car
cat
chair
cow
diningtable
dog
horse
motorbike
person
pottedplant
sheep
sofa
train
tvmonitor

第四步:训练模型 

下载预训练卷积权重:wget https://pjreddie.com/media/files/darknet53.conv.74

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74 

不知是什么原因,可能是我的笔记本没有带有GPU,所以这里进行的非常慢,我就没有等下去了!!! 

参考链接:https://blog.csdn.net/lilai619/article/details/79695109

                https://blog.csdn.net/phinoo/article/details/83022101

你可能感兴趣的:(计算机视觉)