资源简介
hog_svm采用Python实现训练,同时在训练的过程中提取hardexample进行进一步训练,获得了较为理想的效果,当然如果需要进一步优化还是有较大优化空间的。
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Tue Apr 17 20:57:22 2018
@author: kuangyongjian
“““
import cv2 as cv
import random
import glob
import os
#加载负样本
def get_neg_samples(foldernamesavePath):
count = 0
imgs = []
labels = []
f = open(‘neg.txt‘)
filenames = glob.iglob(os.path.join(foldername‘*‘))
for filename in filenames:
print(‘filename = ‘filename)
src = cv.imread(filename1)
if((src.cols >= 64) & (src.rows >= 128)):
x = random.uniform(0src.cols - 64)
y = random.uniform(0src.rows - 128)
imgRoi = src(cv.Rect(xy64128))
imgs.append(imgRoi)
saveName = savePath + ‘neg‘ + str(count) + ‘.jpg‘
cv.imwrite(saveNameimgRoi)
label = ‘neg‘ + str(count) + ‘.jpg‘
labels.append(label)
label = label + ‘\n‘
f.write(label)
count += 1
return imgslabels
#读取负样本
def read_neg_samples(foldername):
imgs = []
labels = []
neg_count = 0;
filenames = glob.iglob(os.path.join(foldername‘*‘))
for filename in filenames:
# print(‘filename = ‘filename)
src = cv.imread(filename1)
# cv.imshow(“src“src)
# cv.waitKey(5)
imgs.append(src)
labels.append(-1)
neg_count += 1
#print (‘neg_count = ‘neg_count)
return imgslabels
#加载正样本
def get_pos_samples(foldernamesavePath):
count = 0
imgs = []
labels = []
f = open(‘pos.txt‘)
filenames = glob.iglob(os.path.join(foldername‘*‘))
for filename in filenames:
print(‘filename = ‘filename)
src = cv.imread(filename)
imgRoi = src(cv.Rect(161664128))
imgs.append(imgRoi)
saveName = savePath + ‘neg‘ + str(count) + ‘.jpg‘
cv.imwrite(saveNameimgRoi)
label = ‘neg‘ + str(count) + ‘.jpg‘
labels.append(label)
f.write(label)
count += 1
return imgslabels
#读取正样本
def read_pos_samples(foldername):
imgs = []
labels = []
pos_count = 0
filenames = glob.iglob(os.path.join(foldername‘*‘))
for filename in filenames:
src = cv.imread(filename)
imgs.append(src)
labels.append(1)
pos_count += 1
return imgslabels
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-04-17 21:11 hog_svm_train_python\
文件 2682 2018-04-17 20:58 hog_svm_train_python\get_data.py
文件 3285 2018-04-17 21:02 hog_svm_train_python\hog_feature.py
文件 70603 2018-04-17 21:04 hog_svm_train_python\myHogDector.bin
文件 70505 2018-04-17 21:09 hog_svm_train_python\myHogDector1.bin
文件 1696 2018-04-17 20:59 hog_svm_train_python\nms.py
文件 504604 2004-11-23 22:03 hog_svm_train_python\person_236.png
文件 468287 2004-11-23 22:03 hog_svm_train_python\person_246.png
文件 433865 2004-11-23 22:03 hog_svm_train_python\person_247.png
文件 462680 2004-11-23 22:03 hog_svm_train_python\person_248.png
文件 915 2018-04-17 20:58 hog_svm_train_python\svm_train.py
文件 1067 2018-04-17 20:59 hog_svm_train_python\test.py
评论
共有 条评论