Test2.jpg
准备材料 链接:https://pan.baidu.com/s/1tQUhHUcRB8gRPqOwmqsZ2A 提取码:0ka7
视频分解图片(可为了以后的样本采集准备)
# 视频分解图片
# 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))#w h
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 = 'img/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 Size要和图片尺寸保持一致
# 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
#记得释放空间,要不然直接打开生成的 2.mp4 会打不开 或者 暂停下运行的服务
videoWrite.release()
print('end!')
基于Haar+Adaboost人脸识别
# 1 load xml 2 load jpg 3 haar gray 4 detect 5 draw
import cv2
import numpy as np
# load xml 1 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('hua1',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('hua2',img)
cv2.waitKey(0)
利用支持向量机训练一组男女身高体重,并预测一个给出的身高体重数据判断男女(为监督学习过,设定男女标签,0为女生的 1 为男生的 )
# 1 思想 分类器
# 2 如何? 寻求一个最优的超平面 分类
# 3 核:line
# 4 数据:样本
# 5 训练 SVM_create train predict
# 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([[152,53],[156,55],[160,56],[172,64],[176,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 女生 [152,53] ---1 男生
# 监督学习 0 负样本 1 正样本
# 4 训练
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],[162,57]]) #0 女生 1男生
pt_data = np.array(pt_data,dtype='float32')
print(pt_data)
(par1,par2) = svm.predict(pt_data)
print(par2)
Hog+SVM小狮子识别
# 训练
# 1 参数 2hog 3 svm 4 computer hog 5 label 6 train 7 pred 8 draw
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 1 par
PosNum = 820
NegNum = 1931
winSize = (64,128)
blockSize = (16,16)# 105
blockStride = (8,8)#4 cell
cellSize = (8,8)
nBin = 9#9 bin 3780
# 2 hog create hog 1 win 2 block 3 blockStride 4 cell 5 bin
hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nBin)
# 3 svm
svm = cv2.ml.SVM_create()
# 4 computer hog
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 监督学习 样本 标签 svm -》image 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]
# featureArray hog [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
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setC(0.01)
# 6 train
ret = svm.train(featureArray,cv2.ml.ROW_SAMPLE,labelArray)
# 7 myHog :《-myDetect
# myDetect-《resultArray rho
# myHog-》detectMultiScale
# 7 检测 核心:create Hog -》 myDetect—》array-》
# resultArray-》resultArray = -1*alphaArray*supportVArray
# rho-》svm-〉svm.train
alpha = np.zeros((1),np.float32)
rho = svm.getDecisionFunction(0,alpha)
print(rho)
print(alpha)
alphaArray = np.zeros((1,1),np.float32)
supportVArray = np.zeros((1,featureNum),np.float32)
resultArray = np.zeros((1,featureNum),np.float32)
alphaArray[0,0] = alpha
resultArray = -1*alphaArray*supportVArray
# detect
myDetect = np.zeros((3781),np.float32)
for i in range(0,3780):
myDetect[i] = resultArray[0,i]
myDetect[3780] = rho[0]
# rho svm (判决)
myHog = cv2.HOGDescriptor()
myHog.setSVMDetector(myDetect)
# load
imageSrc = cv2.imread('Test2.jpg',1)
# (8,8) win
objs = myHog.detectMultiScale(imageSrc,0,(8,8),(32,32),1.05,2)
# 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])
# 绘制展示
cv2.rectangle(imageSrc,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('dst',imageSrc)
cv2.waitKey(0)
网友评论