Last active
January 24, 2018 02:09
-
-
Save PonDad/c128fbd3bcc94bafa1de5627898a2794 to your computer and use it in GitHub Desktop.
Raspberry Pi 深層学習でリアルタイム顔認識(Keras・Open CV)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import face_keras as face | |
import sys, os | |
from keras.preprocessing.image import load_img, img_to_array | |
import numpy as np | |
import cv2 | |
import time | |
cascade_path = "haarcascade_frontalface_alt.xml" | |
cascade = cv2.CascadeClassifier(cascade_path) | |
cam = cv2.VideoCapture(0) | |
color = (255, 255, 255) | |
image_size = 32 | |
categories = ["Samuel", "Travolta", "PonDad", "Madsen", "Pam", "Mikami"] | |
def main(): | |
while(True): | |
ret, frame = cam.read() | |
facerect = cascade.detectMultiScale(frame, scaleFactor=1.2, minNeighbors=2, minSize=(10, 10)) | |
cv2.imwrite("frontalface.png", frame) | |
img = cv2.imread("frontalface.png") | |
for rect in facerect: | |
cv2.rectangle(frame, tuple(rect[0:2]),tuple(rect[0:2] + rect[2:4]), color, thickness=2) | |
x = rect[0] | |
y = rect[1] | |
width = rect[2] | |
height = rect[3] | |
dst = img[y:y+height, x:x+width] | |
cv2.imwrite("output.png", dst) | |
cv2.imread("output.png") | |
X = [] | |
img = load_img("./output.png", target_size=(image_size,image_size)) | |
in_data = img_to_array(img) | |
X.append(in_data) | |
X = np.array(X) | |
X = X.astype("float") / 256 | |
model = face.build_model(X.shape[1:]) | |
model.load_weights("./image/face-model.h5") | |
pre = model.predict(X) | |
print(pre) | |
if pre[0][0] > 0.9: | |
print(categories[0]) | |
text = categories[0] + str(pre[0][0]*100) + "%" | |
font = cv2.FONT_HERSHEY_PLAIN | |
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) | |
elif pre[0][1] > 0.9: | |
print(categories[1]) | |
text = categories[1] + str(pre[0][1]*100) + "%" | |
font = cv2.FONT_HERSHEY_PLAIN | |
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) | |
elif pre[0][2] > 0.9: | |
print(categories[2]) | |
text = categories[2] + str(pre[0][2]*100) + "%" | |
font = cv2.FONT_HERSHEY_PLAIN | |
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) | |
cv2.imshow("Show FLAME Image", frame) | |
time.sleep(0.4) | |
k = cv2.waitKey(1) | |
if k == ord('q'): | |
break | |
cam.release() | |
cv2.destroyAllWindows() | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Activation, Flatten | |
from keras.layers import Convolution2D, MaxPooling2D | |
from keras.utils import np_utils | |
import numpy as np | |
root_dir = "./image/" | |
categories = ["samuel", "travolta", "pon_dad", "madsen", "pam", "mikami"] | |
nb_classes = len(categories) | |
image_size = 32 | |
def main(): | |
X_train, X_test, y_train, y_test = np.load("./image/face.npy") | |
X_train = X_train.astype("float") / 256 | |
X_test = X_test.astype("float") / 256 | |
y_train = np_utils.to_categorical(y_train, nb_classes) | |
y_test = np_utils.to_categorical(y_test, nb_classes) | |
model = model_train(X_train, y_train) | |
model_eval(model, X_test, y_test) | |
def build_model(in_shape): | |
model = Sequential() | |
model.add(Convolution2D(32, 3, 3, | |
border_mode='same', | |
input_shape=in_shape)) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Convolution2D(64, 3, 3, border_mode='same')) | |
model.add(Activation('relu')) | |
model.add(Convolution2D(64, 3, 3)) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(512)) | |
model.add(Activation('relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(nb_classes)) | |
model.add(Activation('softmax')) | |
model.compile(loss='binary_crossentropy', | |
optimizer='rmsprop', | |
metrics=['accuracy']) | |
return model | |
def model_train(X, y): | |
model = build_model(X.shape[1:]) | |
history = model.fit(X, y, batch_size=32, nb_epoch=30, validation_split=0.1) | |
hdf5_file = "./image/face-model.h5" | |
model.save_weights(hdf5_file) | |
return model | |
def model_eval(model, X, y): | |
score = model.evaluate(X, y) | |
print('loss=', score[0]) | |
print('accuracy=', score[1]) | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn import cross_validation | |
from keras.preprocessing.image import load_img, img_to_array | |
import os, glob | |
import numpy as np | |
root_dir = "./image/" | |
categories = ["samuel", "travolta", "pon_dad", "madsen", "pam", "mikami"] | |
nb_classes = len(categories) | |
image_size = 32 | |
X = [] | |
Y = [] | |
for idx, cat in enumerate(categories): | |
files = glob.glob(root_dir + "/" + cat + "/*") | |
print("---", cat, "を処理中") | |
for i, f in enumerate(files): | |
img = load_img(f, target_size=(image_size,image_size)) | |
data = img_to_array(img) | |
X.append(data) | |
Y.append(idx) | |
X = np.array(X) | |
Y = np.array(Y) | |
X_train, X_test, y_train, y_test = \ | |
cross_validation.train_test_split(X, Y) | |
xy = (X_train, X_test, y_train, y_test) | |
np.save("./image/face.npy", xy) | |
print("ok,", len(Y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment