车牌识别之Cascade人脸识别训练

主题概要
人脸识别人脸识别训练的一个脚本
编辑时间
新建20170102
增加截图20170110
序号参考资料
1https://github.com/openalpr/openalpr
2http://docs.opencv.org/2.4/doc/user_guide/ug_traincascade.html
3https://github.com/openalpr/train-detector

脚本

前面做车牌识别总结的时候,提到了用人脸识别的方法进行定位。根据参考资料3,checkout出来,里面会有一些自带的正样本和负样本。还有一个python执行脚本,但是是在linux环境上执行的。我家里没有linux的环境,以前没有接触过python,但连蒙带猜,还是顺利把它改造成windows下能跑了。
其实主要的改动就是目录路径,然后每次执行cascade命令的时候都要os.chdir(OPENCV_DIR) 到openCV的执行目录。
目录结构是在参考资料3的基础上加一个cn文件夹,存放我们的中文车牌正样本。
这里写图片描述
主要要修改的配置有:
图片归一化的宽度和高度,大小是像素:

这里写图片描述

OpenCV的可执行目录:
这里写图片描述

训练时的工作基本目录:
这里写图片描述

下面是修改后的整个python脚本:

#!/usr/bin/python

import os
from PIL import Image
import uuid
import shutil

import sys

WIDTH=49
HEIGHT=13
COUNTRY='cn'

OPENCV_DIR= 'D:/Program Files (x86)/OpenCV2.4.9/opencv/build/x86/vc12/bin'
SAMPLE_CREATOR = OPENCV_DIR

BASE_DIR            = 'F:/Project/ComShao/LI-Openalpr-train-detector/Train-detector/'

OUTPUT_DIR          = BASE_DIR + "out/"
INPUT_NEGATIVE_DIR  = BASE_DIR + 'raw-neg/'
INPUT_POSITIVE_DIR  = BASE_DIR + COUNTRY + '/'
OUTPUT_NEGATIVE_DIR  = BASE_DIR + 'negative/'
OUTPUT_POSITIVE_DIR  = BASE_DIR + 'positive/'


POSITIVE_INFO_FILE  = OUTPUT_POSITIVE_DIR + 'positive.txt'
NEGATIVE_INFO_FILE  = OUTPUT_NEGATIVE_DIR + 'negative.txt'
VEC_FILE            = OUTPUT_POSITIVE_DIR + 'vecfile.vec'



vector_arg = '-vec %s' % (VEC_FILE)
width_height_arg = '-w %d -h %d' % (WIDTH, HEIGHT)


def print_usage():
    print "Usage: prep.py [Operation]"
    print "   -- Operations --"
    print "  neg        -- Prepares the negative samples list"
    print "  pos        -- Copies all the raw positive files to a opencv vector"
    print "  showpos    -- Shows the positive samples that were created"
    print "  train      -- Outputs the command for the Cascade Training algorithm"

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1



command=""

if command != "":
    pass
elif len(sys.argv) != 2:
    print_usage()
    exit()
else:
    command = sys.argv[1]


if command == "neg":
    print "Neg"

    # Get rid of any spaces
    for neg_file in os.listdir(INPUT_NEGATIVE_DIR):
        if " " in neg_file:
            fileName, fileExtension = os.path.splitext(neg_file)

            newfilename =  str(uuid.uuid4()) + fileExtension
            #print "renaming: " + files + " to "+ root_dir + "/" + str(uuid.uuid4()) + fileExtension
            os.rename(INPUT_NEGATIVE_DIR + neg_file, INPUT_POSITIVE_DIR + newfilename)


    f = open(NEGATIVE_INFO_FILE,'w')
    ## Write a list of all the negative files
    for neg_file in os.listdir(INPUT_NEGATIVE_DIR):
        if os.path.isdir(INPUT_NEGATIVE_DIR + neg_file):
            continue

        shutil.copy2(INPUT_NEGATIVE_DIR + neg_file, OUTPUT_NEGATIVE_DIR + neg_file )

        #f.write(neg_file + "\r\n")
        f.write(neg_file + "\n")


    f.close()

elif command == "pos":
    print "Pos"
    info_arg = '-info %s' % (POSITIVE_INFO_FILE)

    # Copy all files in the raw directory and build an info file

    ## Remove all files in the output positive directory
    for old_file in os.listdir(OUTPUT_POSITIVE_DIR):
        os.unlink(OUTPUT_POSITIVE_DIR + old_file)

    ## First, prep the sample filenames (make sure they have no spaces)
    for files in os.listdir(INPUT_POSITIVE_DIR):
        if os.path.isdir(INPUT_POSITIVE_DIR + files):
            continue

        # Rename the file if it has a space in it
        newfilename = files
        if " " in files:
            fileName, fileExtension = os.path.splitext(files)

            newfilename =  str(uuid.uuid4()) + fileExtension
            #print "renaming: " + files + " to "+ root_dir + "/" + str(uuid.uuid4()) + fileExtension
            os.rename(INPUT_POSITIVE_DIR + files, INPUT_POSITIVE_DIR + newfilename)

        # Copy from the raw directory to the positive directory
        shutil.copy2(INPUT_POSITIVE_DIR + newfilename, OUTPUT_POSITIVE_DIR + newfilename )


    total_pics = 0
    ## Create the positive.txt input file
    f = open(POSITIVE_INFO_FILE,'w')
    for filename in os.listdir(OUTPUT_POSITIVE_DIR):
        if os.path.isdir(OUTPUT_POSITIVE_DIR + filename):
            continue

        if filename.endswith(".txt"):
            continue
    try:
        img = Image.open(OUTPUT_POSITIVE_DIR + filename)

        # get the image's width and height in pixels
        width, height = img.size
        f.write(filename + " 1 0 0 " + str(width) + " " + str(height) + '\n')
        #f.write(filename + " 1 0 0 " + str(width) + " " + str(height) + '/n')

        total_pics = total_pics + 1
    except IOError:
        print "Exception reading image file: " + filename

    f.close()




    # Collapse the samples into a vector file

    os.chdir(OPENCV_DIR)
    #execStr = '%s/opencv_createsamples %s %s %s -num %d' % (OPENCV_DIR, vector_arg, width_height_arg, info_arg, total_pics )
    execStr = 'opencv_createsamples %s %s %s -num %d' % (vector_arg, width_height_arg, info_arg, total_pics )
    print execStr

    os.system(execStr)
    #opencv_createsamples -info ./positive.txt -vec ../positive/vecfile.vec -w 120 -h 60 -bg ../negative/PentagonCityParkingGarage21.jpg -num 100


elif command == "showpos":
    print "SHOW"
    os.chdir(OPENCV_DIR)
    #execStr = '%s/opencv_createsamples -vec %s -w %d -h %d' % (OPENCV_DIR, VEC_FILE, WIDTH, HEIGHT )
    execStr = 'opencv_createsamples -vec %s -w %d -h %d' % (VEC_FILE, WIDTH, HEIGHT )
    print execStr
    os.system(execStr)
    #opencv_createsamples -vec ../positive/vecfile.vec -w 120 -h 60
elif command == "train":
    print "TRAIN"

    #data_arg = '-data %s/' % (OUTPUT_DIR)
    data_arg = '-data %s' % (OUTPUT_DIR)
    bg_arg = '-bg %s' % (NEGATIVE_INFO_FILE)
    #bg_arg = '-bg %s' %  "negative.txt"
    try:
    num_pos_samples = file_len(POSITIVE_INFO_FILE)
    except:
    num_pos_samples = -1
    num_pos_samples*=0.8
    num_neg_samples = file_len(NEGATIVE_INFO_FILE)
    num_neg_samples*=0.8

    os.chdir(OPENCV_DIR)
    #execStr = '%s/opencv_traincascade %s %s %s %s -numPos %d -numNeg %d -maxFalseAlarmRate 0.45 -featureType LBP -numStages 13' % (OPENCV_DIR, data_arg, vector_arg, bg_arg, width_height_arg, num_pos_samples, num_neg_samples )
    execStr = 'opencv_traincascade %s %s %s %s -precalcIdxBufSize %d -numPos %d -numNeg %d  -maxFalseAlarmRate 0.45 -featureType LBP -numStages 13' % (data_arg, vector_arg, bg_arg, width_height_arg,6000,num_pos_samples, num_neg_samples )
    #execStr = 'opencv_traincascade %s %s %s %s -numPos %d -numNeg %d -maxFalseAlarmRate 0.45 -featureType LBP -numStages 13' % (data_arg, vector_arg, bg_arg, width_height_arg,num_pos_samples, num_neg_samples )
    print "Execute the following command to start training:"
    print execStr
    os.system(execStr)
    #opencv_traincascade -data ./out/ -vec ./positive/vecfile.vec -bg ./negative/negative.txt -w 120 -h 60 -numPos 99 -numNeg 5  -featureType LBP -numStages 8
    #opencv_traincascade -data ./out/ -vec ./positive/vecfile.vec -bg ./negative/negative.txt -w 120 -h 60 -numPos 99 -numNeg 5  -featureType LBP -numStages 20
elif command == "SDFLSDFSDFSDF":

    root_dir = '/home/mhill/projects/anpr/AlprPlus/samples/svm/raw-pos'
    outputfilename = "positive.txt"



else:
    print_usage()
    exit()

脚本的内容本身很简单,不外乎创建正样本、创建负样本、调用opencv_createsamples、opencv_traincascade两个命令等。
整个人脸识别的原理、通过怎么样提取特征、用的什么算法,这些东西其实才是精华,但毕竟时间有限,也不是专门研究这个的,现在只能做到根据参考资料2,对各个参数有个几分的理解。比如,这里面的参数 -precalcIdxBufSize 是我自己根据openvCV的说明文档添加进去的,大大提高了训练速度。
可能训练最难办的到不是这个脚本的理解,而是这正样本是怎么来的?我这里有12000多张定位好的车牌,其实是应用easyPR里面的方法,自己写了个批量定位的函数,由sobel定位、颜色定位和文字定位而来的。

使用

安装好python2.几,注意不要用3以上版本,会导致print 语法不支持。
切换到工作目录后,依次执行:
prep.py neg
prep.py pos
prep.py train

这里写图片描述

这里写图片描述

如果提示下面的语句报错:
这里写图片描述

可以进入python shell中,执行:pip install image
或者:python -m pip install Pillow

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值