[PYTHON] [Failure] Find Maki Horikita by machine learning

【environment】

windows8.1  Anaconda(python2.7)

【Overview】

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

【result】

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.

[Reference site]

SVM to try machine learning with scikit-learn Perform face recognition with OpenCV, trim and save only the face part [Python]

Recommended Posts

[Failure] Find Maki Horikita by machine learning
4 [/] Four Arithmetic by Machine Learning
Machine learning summary by Python beginners
Machine learning
Making Sandwichman's Tale by Machine Learning ver4
Four arithmetic operations by machine learning 6 [Commercial]
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Judgment of igneous rock by machine learning ②
Classification of guitar images by machine learning Part 1
Try to forecast power demand by machine learning
Classify machine learning related information by topic model
Analysis of shared space usage by machine learning
Stock price forecast by machine learning Numerai Signals
[Translation] scikit-learn 0.18 Tutorial Introduction of machine learning by scikit-learn
Reasonable price estimation of Mercari by machine learning
[Memo] Machine learning
Classification of guitar images by machine learning Part 2
Machine learning classification
A story about data analysis by machine learning
Machine Learning sample
Python learning memo for machine learning by Chainer Chapters 1 and 2
Time series data prediction by AutoML (automatic machine learning)
Machine learning tutorial summary
About machine learning overfitting
Machine learning ⑤ AdaBoost Summary
Machine Learning: Supervised --AdaBoost
Machine learning logistic regression
Machine learning support vector machine
Machine Sommelier by Keras-
Studying Machine Learning ~ matplotlib ~
Machine learning linear regression
Machine learning course memo
Machine learning library dlib
Machine learning (TensorFlow) + Lotto 6
Somehow learn machine learning
Machine learning library Shogun
Machine learning rabbit challenge
Introduction to machine learning
Machine Learning: k-Nearest Neighbors
What is machine learning?
Collect machine learning data by scraping from bio-based public databases
Python learning memo for machine learning by Chainer Chapter 7 Regression analysis
Distinguish t + pazolite songs by machine learning (NNC challenge development)
Stock price forecast by machine learning Let's get started Numerai
I was frustrated by Kaggle, so I tried to find a good rental property by scraping & machine learning