今回はGMM(Gaussian mixture model)=混合ガウスモデルを用いてあいみんとぴょんさんの顔認識をしてみました。
ソースコードも載せてますので、これを元に色々な分類を行ってみてください!
目的
GMMで顔認識!
使用したもの
・pyhton2.7
(printをせばpython3でも動きます!)
・画像(50*50)
学習用
愛美
テスト用
愛美
では、実験していきます!
# coding: utf-8
import cv2
import matplotlib.pyplot as plt
import sys
import glob
from PIL import Image
from sklearn import mixture
from sklearn.externals import joblib
import numpy as np
def resize(filename, w, h):
imageName = filename
img = Image.open(imageName)
img_resize_lanczos = img.resize((w, h), Image.LANCZOS)
savepath = filename
img_resize_lanczos.save(savepath)
img_resize = cv2.imread(savepath)
return img_resize
if __name__=="__main__":
n_mixtures = 50
max_iterations = 75
tarain_data_folder = glob.glob('train_data/*')
test_data_folder = glob.glob('test_data/*')
for person in tarain_data_folder:
all_image_data = []
speaker_name = person.split("/")[-1]
for imgPath in glob.glob(person+'/*.jpg'):
print imgPath
img_resize = resize(imgPath, 50, 50)
img_gray = cv2.cvtColor(img_resize, cv2.COLOR_BGR2GRAY)
if all_image_data ==[]:
all_image_data=img_gray
else:
all_image_data = np.concatenate([all_image_data,img_gray],axis=0)
all_image_data = np.array(all_image_data)
print all_image_data.shape
gmm = mixture.GaussianMixture(n_components=n_mixtures, covariance_type='diag' , max_iter = max_iterations ).fit(all_image_data)
joblib.dump(gmm,'train_models/'+speaker_name+'.pkl')
print "Training Completed"
# test
for person in test_data_folder:
all_image_data = []
speaker_name = person.split("/")[-1]
for imgPath in glob.glob(person+'/*.jpg'):
print imgPath
img_resize = resize(imgPath, 50, 50)
img_gray = cv2.cvtColor(img_resize, cv2.COLOR_BGR2GRAY)
max_score=-9999999
max_person=person
for modelfile in sorted(glob.glob('train_models/*.pkl')):
gmm = joblib.load(modelfile)
score=gmm.score(img_gray)
if score>max_score:
max_score,max_speaker=score,modelfile.replace('train_models/','').replace('.pkl','')
print person+" -> "+max_person+(" Y" if person==max_person else " N")
出力結果
train_data/pyon/pyon3.jpg
train_data/pyon/pyon2.jpg
train_data/pyon/pyon.jpg
(150, 50)
train_data/aimi/aimi.jpg
train_data/aimi/aimi2.jpg
train_data/aimi/aimi3.jpg
(150, 50)
Training Completed
=============================================================
# test
test_data/pyon/pyon5.jpg
test_data/pyon -> test_data/pyon Y
test_data/pyon/pyon4.jpg
test_data/pyon -> test_data/pyon Y
test_data/pyon/pyon6.jpg
test_data/pyon -> test_data/pyon Y
test_data/aimi/aimi6.jpg
test_data/aimi -> test_data/aimi Y
test_data/aimi/aimi4.jpg
test_data/aimi -> test_data/aimi Y
test_data/aimi/aimi5.jpg
test_data/aimi -> test_data/aimi Y
3つ*2人のデータで学習を行い、3つ*2人のデータでテストを行った。
結果、識別率100%となった。
識別するデータが2人だったため高い性能になっています。
次はまち吉あいみーなの4人の顔認識をやりたいと思います!
ありがとうございました!