当前位置:首页 >> 脚本专栏

python+opencv3.4.0 实现HOG+SVM行人检测的示例代码

参照opencv官网例程写了一个基于python的行人检测程序,实现了和自带检测器基本一致的检测效果。

网址 :https://docs.opencv.org/3.4.0/d5/d77/train_HOG_8cpp-example.html

opencv版本:3.4.0

训练集和opencv官方用了同一个,可以从http://pascal.inrialpes.fr/data/human/下载,在网页的最下方“here(970MB处)”,用迅雷下载比较快(500kB/s)。训练集文件比较乱,需要仔细阅读下载首页的文字介绍。注意pos文件夹下的png图片属性,它们用opencv无法直接打开,linux系统下也无法显示,需要用matlab读取图片->保存才行,很奇怪的操作。

代码如下,尽可能与opencv官方例程保持一致,但省略了很多不是很关键的东西。训练一次大概需要十几分钟

import cv2
import numpy as np
import random
 
 
def load_images(dirname, amout = 9999):
 img_list = []
 file = open(dirname)
 img_name = file.readline()
 while img_name != '': # 文件尾
  img_name = dirname.rsplit(r'/', 1)[0] + r'/' + img_name.split('/', 1)[1].strip('\n')
  img_list.append(cv2.imread(img_name))
  img_name = file.readline()
  amout -= 1
  if amout <= 0: # 控制读取图片的数量
   break
 return img_list
 
 
# 从每一张没有人的原始图片中随机裁出10张64*128的图片作为负样本
def sample_neg(full_neg_lst, neg_list, size):
 random.seed(1)
 width, height = size[1], size[0]
 for i in range(len(full_neg_lst)):
  for j in range(10):
   y = int(random.random() * (len(full_neg_lst[i]) - height))
   x = int(random.random() * (len(full_neg_lst[i][0]) - width))
   neg_list.append(full_neg_lst[i][y:y + height, x:x + width])
 return neg_list
 
 
# wsize: 处理图片大小,通常64*128; 输入图片尺寸>= wsize
def computeHOGs(img_lst, gradient_lst, wsize=(128, 64)):
 hog = cv2.HOGDescriptor()
 # hog.winSize = wsize
 for i in range(len(img_lst)):
  if img_lst[i].shape[1] >= wsize[1] and img_lst[i].shape[0] >= wsize[0]:
   roi = img_lst[i][(img_lst[i].shape[0] - wsize[0]) // 2: (img_lst[i].shape[0] - wsize[0]) // 2 + wsize[0],      (img_lst[i].shape[1] - wsize[1]) // 2: (img_lst[i].shape[1] - wsize[1]) // 2 + wsize[1]]
   gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
   gradient_lst.append(hog.compute(gray))
 # return gradient_lst
 
 
def get_svm_detector(svm):
 sv = svm.getSupportVectors()
 rho, _, _ = svm.getDecisionFunction(0)
 sv = np.transpose(sv)
 return np.append(sv, [[-rho]], 0)
 
 
# 主程序
# 第一步:计算HOG特征
neg_list = []
pos_list = []
gradient_lst = []
labels = []
hard_neg_list = []
svm = cv2.ml.SVM_create()
pos_list = load_images(r'G:/python_project/INRIAPerson/96X160H96/Train/pos.lst')
full_neg_lst = load_images(r'G:/python_project/INRIAPerson/train_64x128_H96/neg.lst')
sample_neg(full_neg_lst, neg_list, [128, 64])
print(len(neg_list))
computeHOGs(pos_list, gradient_lst)
[labels.append(+1) for _ in range(len(pos_list))]
computeHOGs(neg_list, gradient_lst)
[labels.append(-1) for _ in range(len(neg_list))]
 
# 第二步:训练SVM
svm.setCoef0(0)
svm.setCoef0(0.0)
svm.setDegree(3)
criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 1000, 1e-3)
svm.setTermCriteria(criteria)
svm.setGamma(0)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setNu(0.5)
svm.setP(0.1) # for EPSILON_SVR, epsilon in loss function"htmlcode">
import cv2
import numpy as np
 
hog = cv2.HOGDescriptor()
hog.load('myHogDector.bin')
cap = cv2.VideoCapture(0)
while True:
 ok, img = cap.read()
 rects, wei = hog.detectMultiScale(img, winStride=(4, 4),padding=(8, 8), scale=1.05)
 for (x, y, w, h) in rects:
  cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
 cv2.imshow('a', img)
 if cv2.waitKey(1)&0xff == 27: # esc键
  break
cv2.destroyAllWindows()