windows8.1 Anaconda(python2.7)
I wrote the code to decide from the image whether it is Maki Horikita. Collect face image folders, digitize them and train them with scikit-learn.
Folder structure
|---face_category
|---horikita_face(200 face image folders of Maki Horikita)
|---joyu_face(200 face image folders other than Maki Horikita)
|---horikita_siken(20 image folders of Maki Horikita for testing)
|---face_rec2.py(horikita_Extract face from siken image)
|---LinearSVC.py(Main processing)
|---haarcascade_frontalface_alt.xml(Cascade file)
face_rec2.py
#-*- coding:utf-8 -*-
def picture_face(before_image, num):
import cv2
import os
import sys
cascade_path = "haarcascade_frontalface_alt.xml"
#File reading
image_file = cv2.imread('C:/Users/nobu/Desktop/my_programs/face_category/horikita_siken/%s' % before_image)
#Grayscale conversion
image_gray = cv2.cvtColor(image_file, cv2.COLOR_BGR2GRAY)
#Acquire the features of the cascade classifier
cascade = cv2.CascadeClassifier(cascade_path)
#Execution of object recognition (face recognition)
facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.2, minNeighbors=2, minSize=(10, 10))
if facerect == None:
sys.exit()
for rect in facerect:
#Cut out only the face and save
x = rect[0]
y = rect[1]
width = rect[2]
height = rect[3]
dst = image_file[y:y+height, x:x+width]
resize_image = cv2.resize(dst,(256,256))
new_image_path = 'C:/Users/nobu/Desktop/my_programs/face_category/' + str(num) + '_face.jpg'
print new_image_path
cv2.imwrite(new_image_path, resize_image)
LinearSVC.py
# -*- coding:utf-8 -*-
from sklearn.svm import LinearSVC
from PIL import Image
import os
import glob
import numpy as np
import face_rec2
current_dir = os.getcwd()
#Training data
#Convert Maki Horikita's image to numerical value
horikita_list = glob.glob(current_dir + "\\horikita_face\\*")
horikita_dim2 = []
for image in horikita_list:
#Exclude system files
if image == current_dir + "\\horikita_face\\Thumbs.db":
continue
else:
x = np.array([])
horikita_dim3 = np.array(Image.open(image).convert('L'))
#scikit-It seems that learn cannot handle more than 3 dimensions, so convert to 2 dimensions
for i in xrange(256):
x = np.r_[x,horikita_dim3[i]]
horikita_dim2.append(x)
data_training1 = horikita_dim2
#Convert images other than Maki Horikita to numerical values
joyu_list = glob.glob(current_dir + "\\joyu_face\\*")
joyu_dim2 = []
for image in joyu_list:
#Exclude system files
if image == current_dir + "\\joyu_face\\Thumbs.db":
continue
else:
x = np.array([])
joyu_dim3 = np.array(Image.open(image).convert('L'))
#scikit-It seems that learn cannot handle more than 3 dimensions, so convert to 2 dimensions
for i in xrange(256):
x = np.r_[x,joyu_dim3[i]]
joyu_dim2.append(x)
data_training2 = joyu_dim2
data_training = np.r_[data_training1, data_training2]
#Label setting
label_training = []
for i in xrange(400):
if i < 200:
label_training.append(1)
else:
label_training.append(0)
#Learning
estimator = LinearSVC(C=0.5)
estimator.fit(data_training, label_training)
j = 0
#Test data
for i in xrange(1,21):
face_rec2.picture_face(str(i) + '.jpg', i)
horikita_siken = np.array([])
try:
horikita = np.array(Image.open('C:/Users/nobu/Desktop/my_programs/face_category/' + str(i) + '_face.jpg').convert('L'))
for j in xrange(256):
horikita_siken = np.r_[horikita_siken,horikita[j]]
data_test = horikita_siken
#I'll predict
label_prediction = estimator.predict(data_test)
if label_prediction == 1:
print "----------------------------------------------"
print str(i) + ".jpg is Maki Horikita"
print "----------------------------------------------"
else:
print "----------------------------------------------"
print str(i) + ".jpg is not Maki Horikita"
print "----------------------------------------------"
except:
print "----------------------------------------------"
print str(i) + ".jpg could not be processed"
print "----------------------------------------------"
continue
Failure ...: weary: I tested with 20 face image folders of Maki Horikita for testing, but for some reason Some output says "It's not Maki Horikita". I would like to know if there are any solutions or improvements.
SVM to try machine learning with scikit-learn Perform face recognition with OpenCV, trim and save only the face part [Python]
Recommended Posts