样本 特征 分类器 预测、检验
haar + adaboost -> face
haar(模板) + adaboost(分类器,三级级联:强分类器,弱分类器,node结点)
hog + svm -> 小狮子识别
#1.load 2.info 3.parse 4.imshow imwrite
import cv2
cap = cv2.VideoCapture("1.mp4") #获取一个视频打开cap 1.file name
isOpened = cap.isOpened #判断是否打开
print(isOpened)
fps = cap.get(cv2.CAP_PROP_FPS) #帧率
width = int (cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(fps,width,height)
i = 0
while(isOpened):
if i ==10:
break
else:
i = i+1
(flag,frame) = cap.read() #读取每一张 flag frame
fileName = 'image'+str(i)+'.jpg'
print(fileName)
if flag == True:
cv2.imwrite(fileName,frame,[cv2.IMWRITE_JPEG_QUALITY,100])
print('end!')
import cv2
img = cv2.imread('image1.jpg')
imgInfo = img.shape
size = (imgInfo[1],imgInfo[0])
print(size)
videoWrite = cv2.VideoWriter('2.mp4',-1,5,size) #写入对象 1.file name 2.编码器 3.帧率 4.size
for i in range(1,11):
fileName = 'image'+str(i)+'.jpg'
img = cv2.imread(fileName)
videoWrite.write(img) #写入方法 1.jpg data
print('end!')
1.特征 2.判决 3.得到判决
haar
公式推导 1 -2
特征 = 整个区域 * 权重1 + 黑色 * 权重2 = (黑+白)* 1+黑 * (-2)= 黑+白-2黑 = 白 - 黑
haar模板 上下 左右 image size 、模板 size
举例 1080720 step=2 1010
计算量 = 14个模板 * 20级缩放 * (1080/2*720/2) *(100点+ -)= 50 -100亿
实时处理:(50-100)*15=1000亿次 (每秒15帧)
A 1 B 1 2 C 1 2 3 D 1 2 3 4
4 = A-B-C+D = 1+1+2+3+4-1-2-1-3 = 4 (3次+ -)
haar + adaboost -> face recognition
苹果 苹果 苹果 香蕉
0.1 0.1 0.1 0.5
训练终止条件:1. for count 2. p
1.分类器的结构 2.adaboost 计算过程 3.xml 文件结构
haar > T1 and haar > T2 2个强分类器 15-20 特征
y1 弱分类器特征 adaboost 训练 SVM本质:寻求一个最优的超平面进行分类 什么是hog? 特征= 某个像素 某种运算 2.1 模块划分 2.2 梯度方向 模板 2.3 bin 2.4 每个模板hog 3780 hog svm line训练
node
3个haar -> 3个node
1node haar1 > nodeT1 z1 = a1
1node haar1 < nodeT1 z1 = a2
z = sum(z1,z2,z3)>T y1 = AA
z = sum(z1,z2,z3)
haar -> node z1 z2 z3 Z=sum(z1,z2,z3)
Z > T y1 y2 y3
x = sum(y1,y2,y3) > T1 obj
苹果 苹果 苹果 香蕉
0.1 0.1 0.1 0.1
minP t
0.2 0.2 0.2 0.7
训练终止条件:1. for count 2. p# 1.load xml 2.load jpg 3.haar gray 4.detect 5.draw
import cv2
import numpy as np
#load xml (file name)
face_xml = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_xml = cv2.CascadeClassifier('haarcascade_eye.xml')
#load jpg
img = cv2.imread('face.jpg')
cv2.imshow('src',img)
#haar gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#detect faces (1.data 2.缩放系数scale 3.人脸不小于5个像素)
faces = face_xml.detectMultiScale(gray,1.3,5)
print('face=',len(faces))
#draw
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_face = gray[y:y+h,x:x+w]
roi_color = img[y:y+h,x:x+w]
# 1 gray
eyes = eye_xml.detectMultiScale(roi_face)
print('eye=',len(eyes))
for (e_x,e_y,e_w,e_h) in eyes:
cv2.rectangle(roi_color,(e_x,e_y),(e_x+e_w,e_y+e_h),(0,255,0),2)
cv2.imshow('dst',img)
cv2.waitKey(0)
Hog_SVM小狮子识别
SVM
SVM核心:line
# 身高体重 训练 预测
import cv2
import numpy as np
import matplotlib.pyplot as plt
#1.准备data
rand1 = np.array([[155,48],[159,50],[164,53],[168,56],[172,60]])
rand2 = np.array([[172,64],[176,65],[177,66],[180,69],[183,65]])
#2.label
label = np.array([[0],[0],[0],[0],[0],[1],[1],[1],[1],[1]])
#3.引入data
data = np.vstack((rand1,rand2))
data = np.array(data,dtype='float32')
#SVM 所有的数据都要有label
#[155,48]——0 女生 [172,64]——1男生
#监督学习 0负样本 1正样本
#4.训练data
svm = cv2.ml.SVM_create() #ml:机器学习模板 SVM_create:创建支持向量机
#属性设置
svm.setType(cv2.ml.SVM_C_SVC) #svm type
svm.setKernel(cv2.ml.SVM_LINEAR) #line
svm.setC(0.01)
#训练
result = svm.train(data,cv2.ml.ROW_SAMPLE,label) #数据 类型 标签
#预测
pt_data = np.vstack([[167,55],[175,62]]) #0女生 1男生
pt_data = np.array(pt_data,dtype='float32')
print(pt_data)
(par1,par2) = svm.predict(pt_data)
print(par1,par2)
Hog特征
2.1. 模块划分
image(ppt) win(blue) block(red) cell(green)
size : image>win>block>cell
win step block step cell bin
win特征计算最顶层单元 ->obj
2.1.1 win size 64 * 128
2.1.2 1) block < win 2) win size w h /block (wh) 16 * 16
2.1.3 block step 如何win下滑动 88
2.1.4 计算 block cout = ((64-16)/8+1)((128-16)/8+1) = 105 block
2.1.5 cell size 8 * 8
2.1.6 block = ? cell 16 * 16=22=> 4cell cell1-cell4
2.1.7 cell bin 梯度:运算
像素 -> 梯度:大小f 方向angle
0-360° /40° = 9块 = 9bin
hog特征维度:
haar:值、hog:向量 (维度)-> 完全描述 一个obj info all
维度 = 105 * 4 * 9=3780
2.2 梯度方向 模板
每个像素都有一个梯度 win -> hog
特征模板 -> haar类似
[1 0 -1] 、 [ [1] [0] [-1] ]
a = p1 * 1+p2 * 0+p3 * (-1) = 相邻像素之差
b = 上下像素之差
f = 根号下(a^ 2+b^ 2)
angle = arctan(a/b)
2.3 bin 投影 梯度
bin: 0-360 9bin 0-40°
bin1 0-20 180-200
ij f
a=10 0-20 bin1; a=190 180-200 bin1 -> center -> 投影在f值上面
a=25 投影在bin1 bin2上面 -> f1 = ff(夹角) f2 = f*(1-f(夹角) ) f(夹角):0-1.0
+1 hog
2.4 每个模板hog
2.4.1整体计算hog cell复用
3780维 <- win ( block cell bin )
cell0 cell1 cell2 cell3 bin0-bin8
cell0 : bin0 bin1 … bin8
cell1 : bin0 bin1 … bin8
cell2 : bin0 bin1 … bin8
cell3 : bin0 bin1 … bin8
i j cell0 bin0 -> f0
i+1 j cell0 bin1 -> f1
…
sumbin0 (f0+f1+…) =bin0
权重累加
2.4.2 cell复用
block 4个cell
cell0 cell1 cell2 cell3
cell0 bin0-bin9
cellx0 cellx2 cellx4
cell0: ij -> bin bin+1
cellx2: ij -> cell2 cell3 ->bin bin+1 bin bin+1
hog *svm = 值
值 > Th 目标obj#1.样本 2.训练 3.test预测
#1.样本 (一个好的样本远胜过一个复杂的神经网络)
#1.1 pos正样本:包含所有检测目标
# neg负样本: 不包含obj
#1.2 正样本尽可能的多样(环境干扰)
#1.3 pos 820 neg 1930 通常1:2或1:3
#2.训练
#2.1 参数的设置 2.2 创建一个hog 2.3 svm 2.4 计算hog
#2.5 label 2.6 train 2.7 pred 2.8 draw
#2.1 参数的设置
import cv2
import numpy as np
import matplotlib.pyplot as plt
PosNum = 820
NegNum = 1931
winSize = (64,128)
blockSize = (16,16) #105
blockStride = (8,8) #4cell
cellSize = (8,8)
nBin = 9 #3780 = 105*4*9
#2.2 create hog
hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nBin)
#2.3 creat svm
svm = cv2.ml.SVM_create()
#2.4 computer hog ##2.5 label
featureNum = int(((128-16)/8+1)*((64-16)/8+1)*4*9) #3780
featureArray = np.zeros(((PosNum+NegNum),featureNum),np.float32)
labelArray = np.zeros(((PosNum+NegNum),1),np.int32)
#监督学习 SVM学习 image_hog
#遍历hog
for i in range(0,PosNum):
fileName = 'pos\\'+str(i+1)+'.jpg'
img = cv2.imread(fileName)
hist = hog.compute(img,(8,8)) #3780
for j in range(0,featureNum):
featureArray[i,j] = hist[j]
# hog特征 装入featureArray中 [1,:]->hog1; [2,:]->hog2
labelArray[i,0] = 1
# 正样本 label 1
for i in range(0,NegNum):
fileName = 'neg\\'+str(i+1)+'.jpg'
img = cv2.imread(fileName)
hist = hog.compute(img,(8,8)) #3780
for j in range(0,featureNum):
featureArray[i+PosNum,j] = hist[j]
labelArray[i+PosNum,0] = -1
# 负样本 label -1
#set svm属性
svm.setType(cv2.ml.SVM_C_SVC) #svm type
svm.setKernel(cv2.ml.SVM_LINEAR) #line
svm.setC(0.01)
#2.6 train
ret = svm.train(featureArray,cv2.ml.ROW_SAMPLE,labelArray)
#2.7 pred 步骤:create myHog (参数:myDetect)
# myDetect (resultArray\rho)
# 检测 myHog detectMultiScale
#2.7 pred 核心:create myHog -> myDetect -> array ->resultArray rho
#resultArray -> resultArray = -1*alphaArray*svmArray
#rho -> svm -> svm.train
alpha = np.zeros((1),np.float32)
rho = svm.getDecisionFunction(0,alpha)
print(alpha)
print(rho)
#create array
alphaArray = np.zeros((1,1),np.float32)
svmArray = np.zeros((1,featureNum),np.float32)
resultArray = np.zeros((1,featureNum),np.float32)
alphaArray[0,0] = alpha
resultArray = -1*alphaArray*svmArray
#detect (检测)
myDetect = np.zeros((3781),np.float32)
for i in range(0,3780):
myDetect[i] = resultArray[0,i] #放入特征
myDetect[3780] = rho[0]
#rho svm (判决时需要rho)
myHog = cv2.HOGDescriptor() #create hog
myHog.setSVMDetector(myDetect) #set characteristic
# load test_image
imageSrc = cv2.imread('Test2.jpg',1)#read test_image
objs = myHog.detectMultiScale(imageSrc,0,(8,8),(32,32),1.05,2) #detect image_目标
#xy wh 三维
x = int(objs[0][0][0])
y = int(objs[0][0][1])
w = int(objs[0][0][2])
h = int(objs[0][0][3])
#2.8 draw and show
cv2.rectangle(imageSrc,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('dst',imageSrc)
cv2.waitKey(0)