SVM算法应用综合练习
一.SVM算法应用综合练习
1.将libsvm加入新建项目
2.在libsvm文件夹中打开svm-toy.exe
3.运行
4.line.txt 线性模型;poly.txt多项式模型;RBF.txt 高斯核模型
二.人脸识别
1.拍摄20张脸部图片
拍照
import cv2
import dlib
import os
import sys
import random
output_dir = 'D:/myworkspace/JupyterNotebook/People/person/631907060526'
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]
#image = []
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.特征点采集
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:/myworkspace/JupyterNotebook/People/person/"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("D:/shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1("D:/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 l