车牌识别之Cascade人脸识别训练
来源:互联网 发布:网络直播 文化部 编辑:程序博客网 时间:2024/06/02 11:39
脚本
前面做车牌识别总结的时候,提到了用人脸识别的方法进行定位。根据参考资料3,checkout出来,里面会有一些自带的正样本和负样本。还有一个python执行脚本,但是是在linux环境上执行的。我家里没有linux的环境,以前没有接触过python,但连蒙带猜,还是顺利把它改造成windows下能跑了。
其实主要的改动就是目录路径,然后每次执行cascade命令的时候都要os.chdir(OPENCV_DIR) 到openCV的执行目录。
目录结构是在参考资料3的基础上加一个cn文件夹,存放我们的中文车牌正样本。
主要要修改的配置有:
图片归一化的宽度和高度,大小是像素:
OpenCV的可执行目录:
训练时的工作基本目录:
下面是修改后的整个python脚本:
#!/usr/bin/pythonimport osfrom PIL import Imageimport uuidimport shutilimport sysWIDTH=49HEIGHT=13COUNTRY='cn'OPENCV_DIR= 'D:/Program Files (x86)/OpenCV2.4.9/opencv/build/x86/vc12/bin'SAMPLE_CREATOR = OPENCV_DIRBASE_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 + 1command=""if command != "": passelif 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 100elif 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 60elif 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 20elif 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
- 车牌识别之Cascade人脸识别训练
- 车牌识别之车牌定位
- 车牌识别之车牌定位
- 车牌识别之车牌定位
- HyperLPR车牌识别技术算法之车牌粗定位与训练
- 车牌识别学习之车牌定位---1
- 车牌识别之车牌定位(一)
- opencv实现车牌识别之字符识别
- 基于mobilenet-ssd训练车牌识别模型
- 车牌识别 之 字符分割
- opencv 学习之 车牌识别
- 车牌识别之颜色选取
- 车牌识别之字符分割
- 项目总结之车牌识别
- 车牌识别
- 车牌识别
- 车牌识别
- 车牌识别
- 我的2016都在升值地卖命和卖命地升值
- j2me 音乐播放器,音频文件大于剩余内存时,出现了问题。
- j2me编译 CLR模式
- 十步完全理解SQL(转)
- tcp
- 车牌识别之Cascade人脸识别训练
- Solr的简单部署
- 急急急,请大家帮忙看看如何在三点菜单中实现不同Activity的跳转
- 流水灯实例,点亮发光管LED并闪烁(查表操作)
- svn 备份和还原项目
- 指定整数数组中输出和为一整数的所有组合
- 漫步数学分析五——闭集
- 67. Add Binary
- SurvivalShooter学习笔记(二.玩家移动旋转)