1、训练并写出决策函数的数学公式
1.1、下载安装libsvm
????1、进入libsvm官网下载libsvm的包
????2、然后下载libsvm的whl文件,然后再使用pip命令安装libsvm 下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#libsvm
1.2、训练制作的数据集
????1、手工制作一个 两个特征的二分类的Iris数据集,首先进入windows文件夹,打开svmtoy.exe文件 ????2、在打开的程序里面使用鼠标随机点击形成数据集 注意:生成数据集的时候使用两种颜色,否则后面不好处理
????3、然后点击保存将数据集保存为文本文档格式,冒号前面的1,2指的是特征
????4、导入包并读取数据,svm_read_problem函数的作用是读取刚刚生成的文件并返回合适的格式便于训练 生成的文件
多项式核的
from libsvm.svmutil import *
from libsvm.svm import *
import scipy.spatial
label,data= svm_read_problem('D:\\word\\iris.txt')
p_label,p_data=svm_read_problem('D:\\word\\predict.txt')
????高斯核的
from libsvm.svmutil import *
from libsvm.svm import *
import scipy.spatial
label,data= svm_read_problem('E:\\libsvm\\iris.txt')
p_label,p_data=svm_read_problem('E:\libsvm\\predict.txt')
para ='-t 2 -c 4 -b 1'
'''
-t
0为线性核
1为多项式核
2为高斯核(默认)
'''
model=svm_train(label,data,para)
svm_save_model('E:\libsvm\\gaosi.txt',model)
acc=svm_predict(p_label,p_data,model)
????5、决策函数:
公式:f(x)=SV*x+rho SV:所有的支持向量 rho:决策函数中的常数项的相反数 SV的值在生成的txt文件内。
2、建立人脸识别数据集
2.1、采集人脸数据集
????1、使用电脑自带的摄像头采集人脸数据
"""
Spyder Editor
This is a temporary script file.
"""
import random
from cv2 import cv2 as cv2
import os
import dlib
from skimage import io
import csv
import numpy as np
output_dir = 'D:/631907060432/face'
size = 64
if not os.path.exists(output_dir):
os.makedirs(output_dir)
def relight(img, light=1, bias=0):
w = img.shape[1]
h = img.shape[0]
for i in range(0,w):
for j in range(0,h):
for c in range(3):
tmp = int(img[j,i,c]*light + bias)
if tmp > 255:
tmp = 255
elif tmp < 0:
tmp = 0
img[j,i,c] = tmp
return img
detector = dlib.get_frontal_face_detector()
camera = cv2.VideoCapture(0)
index = 1
while True:
if (index <= 20):
print('Being processed picture %s' % index)
success, img = camera.read()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray_img, 1)
for i, d in enumerate(dets):
x1 = d.top() if d.top() > 0 else 0
y1 = d.bottom() if d.bottom() > 0 else 0
x2 = d.left() if d.left() > 0 else 0
y2 = d.right() if d.right() > 0 else 0
face = img[x1:y1,x2:y2]
face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))
face = cv2.resize(face, (size,size))
cv2.imshow('image', face)
cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face)
index += 1
key = cv2.waitKey(30) & 0xff
if key == 27:
break
else:
print('Finished!')
camera.release()
cv2.destroyAllWindows()
break
2.2、建立特征数据集
????1、获取特征数据并保存
from cv2 import cv2 as cv2
import os
import dlib
from skimage import io
import csv
import numpy as np
path_images_from_camera = "D:/631907060432/"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
face_rec = dlib.face_recognition_model_v1('dlib_face_recognition_resnet_model_v1.dat')
def return_128d_features(path_img):
img_rd = io.imread(path_img)
img_gray = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)
faces = detector(img_gray, 1)
print("%-40s %-20s" % ("检测到人脸的图像 / image with faces detected:", path_img), '\n')
if len(faces) != 0:
shape = predictor(img_gray, faces[0])
face_descriptor = face_rec.compute_face_descriptor(img_gray, shape)
else:
face_descriptor = 0
print("no face")
return face_descriptor
def return_features_mean_personX(path_faces_personX):
features_list_personX = []
photos_list = os.listdir(path_faces_personX)
if photos_list:
for i in range(len(photos_list)):
print("%-40s %-20s" % ("正在读的人脸图像 / image to read:", path_faces_personX + "/" + photos_list[i]))
features_128d = return_128d_features(path_faces_personX + "/" + photos_list[i])
if features_128d == 0:
i += 1
else:
features_list_personX.append(features_128d)
i1=str(i+1)
add="D:/631907060432/"+i1+".csv"
print(add)
with open(add,"w",newline="") as csvfile:
writer1 = csv.writer(csvfile)
writer1.writerow(features_128d)
else:
print("文件夹内图像文件为空 / Warning: No images in " + path_faces_personX + '/', '\n')
if features_list_personX:
features_mean_personX = np.array(features_list_personX).mean(axis=0)
else:
features_mean_personX = '0'
return features_mean_personX
people = os.listdir(path_images_from_camera)
people.sort()
with open("D:/test.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
for person in people:
print("##### " + person + " #####")
features_mean_personX = return_features_mean_personX(path_images_from_camera + person)
writer.writerow(features_mean_personX)
print("特征均值 / The mean of features:", list(features_mean_personX))
print('\n')
print("所有录入人脸数据存入 / Save all the features of faces registered into: D:/Face/get_features_all.csv")
????1、采集数据 ????2、采集到的数据
????3、其中的一组数据值
????4、平均特征数组
|