Last active
April 30, 2019 05:32
-
-
Save PonDad/e47ebc44f84cb6aae46e63e0028c437b 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 apple_keras as apple | |
import sys, os | |
import numpy as np | |
import subprocess | |
import cv2 | |
from keras.preprocessing.image import load_img, img_to_array | |
cam = cv2.VideoCapture(0) | |
image_size = 32 | |
categories = ["赤りんご", "青りんご"] | |
def main(): | |
def jtalk(t): | |
open_jtalk=['open_jtalk'] | |
mech=['-x','/var/lib/mecab/dic/open-jtalk/naist-jdic'] | |
htsvoice=['-m','/usr/share/hts-voice/mei/mei_happy.htsvoice'] | |
speed=['-r','1.0'] | |
outwav=['-ow','open_jtalk.wav'] | |
cmd=open_jtalk+mech+htsvoice+speed+outwav | |
c = subprocess.Popen(cmd,stdin=subprocess.PIPE) | |
c.stdin.write(t) | |
c.stdin.close() | |
c.wait() | |
aplay = ['aplay','-q','open_jtalk.wav'] | |
wr = subprocess.Popen(aplay) | |
while(True): | |
ret, frame = cam.read() | |
cv2.imshow("Show FLAME Image", frame) | |
k = cv2.waitKey(1) | |
if k == ord('s'): | |
cv2.imwrite("output.png", frame) | |
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 = apple.build_model(X.shape[1:]) | |
model.load_weights("./image/apple-model.h5") | |
pre = model.predict(X) | |
print(pre) | |
if pre[0][0] > 0.9: | |
print(categories[0]) | |
text = 'これは' + categories[0]+ 'だよ' | |
text = text.encode('utf-8') | |
jtalk(text) | |
elif pre[0][1] > 0.9: | |
print(categories[1]) | |
text = 'これは' + categories[1]+ 'だよ' | |
text = text.encode('utf-8') | |
jtalk(text) | |
elif 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
# https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py | |
import numpy as np | |
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 | |
root_dir = "./image/" | |
categories = ["red_apple", "green_apple"] | |
nb_classes = len(categories) | |
image_size = 32 | |
def main(): | |
X_train, X_test, y_train, y_test = np.load("./image/apple.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=10, validation_split=0.1) | |
hdf5_file = "./image/apple-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
import os, glob | |
import numpy as np | |
from sklearn import cross_validation | |
from keras.preprocessing.image import load_img, img_to_array | |
root_dir = "./image/" | |
categories = ["red_apple", "green_apple"] | |
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/apple.npy", xy) | |
print("ok,", len(Y)) |
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
The MIT License (MIT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment