Skip to content

Commit

Permalink
fix integrated system
Browse files Browse the repository at this point in the history
  • Loading branch information
Hironsan committed Sep 14, 2016
1 parent 709a297 commit 11b1e14
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 85 deletions.
12 changes: 6 additions & 6 deletions boss_input.py
Expand Up @@ -4,8 +4,10 @@
import numpy as np
import cv2

IMAGE_SIZE = 64

def resize_with_pad(image, height, width):

def resize_with_pad(image, height=IMAGE_SIZE, width=IMAGE_SIZE):

def get_padding_size(image):
h, w, _ = image.shape
Expand All @@ -24,7 +26,7 @@ def get_padding_size(image):
return top, bottom, left, right

top, bottom, left, right = get_padding_size(image)
BLACK = [0,0,0]
BLACK = [0, 0, 0]
constant = cv2.copyMakeBorder(image, top , bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)

resized_image = cv2.resize(constant, (height, width))
Expand All @@ -45,10 +47,8 @@ def traverse_dir(path):
image = read_image(abs_path)
images.append(image)
labels.append(path)
return images, labels


IMAGE_SIZE = 32
return images, labels


def read_image(file_path):
Expand All @@ -60,8 +60,8 @@ def read_image(file_path):

def extract_data(path):
images, labels = traverse_dir(path)
#images = np.array([np.reshape(image, -1) for image in images])
images = np.array(images)
dic = dict([(label, i) for i, label in enumerate(set(labels))])
labels = np.array([dic[label] for label in labels])

return images, labels
37 changes: 19 additions & 18 deletions boss_train.py
Expand Up @@ -3,7 +3,6 @@
import random

import numpy as np
import tensorflow as tf
from sklearn.cross_validation import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
Expand All @@ -14,7 +13,7 @@
from keras.models import load_model
from keras import backend as K

from boss_input import extract_data, resize_with_pad
from boss_input import extract_data, resize_with_pad, IMAGE_SIZE


class Dataset(object):
Expand All @@ -25,7 +24,7 @@ def __init__(self):
self.Y_train = None
self.Y_test = None

def read(self, img_rows=32, img_cols=32, img_channels=3, nb_classes=2):
def read(self, img_rows=IMAGE_SIZE, img_cols=IMAGE_SIZE, img_channels=3, nb_classes=2):
images, labels = extract_data('./data/')
labels = np.reshape(labels, [-1])
# numpy.reshape
Expand Down Expand Up @@ -141,11 +140,12 @@ def load(self, file_path=FILE_PATH):
self.model = load_model(file_path)

def predict(self, image):
#result = self.model.predict_proba(image)
if image.shape != (1, 3, 32, 32):
image = resize_with_pad(image, 32, 32)
image = image.reshape((1, 3, 32, 32))
if image.shape != (1, 3, IMAGE_SIZE, IMAGE_SIZE):
image = resize_with_pad(image)
image = image.reshape((1, 3, IMAGE_SIZE, IMAGE_SIZE))
result = self.model.predict_classes(image)
# result = self.model.predict_proba(image)

return result[0]

def evaluate(self, dataset):
Expand All @@ -154,21 +154,21 @@ def evaluate(self, dataset):

if __name__ == '__main__':

#dataset = Dataset()
#dataset.read()
"""
dataset = Dataset()
dataset.read()

model = Model()
model.build_model(dataset)
model.train(dataset, nb_epoch=10)
model.save()
"""

model = Model()
model.load()
# model.evaluate(dataset)
# for image, label in zip(dataset.X_test, dataset.Y_test):
# model.predict(image.reshape(1, 3, 32, 32))
# print(label)

model.evaluate(dataset)
for image, label in zip(dataset.X_test, dataset.Y_test):
model.predict(image.reshape(1, 3, IMAGE_SIZE, IMAGE_SIZE))
print(label)
"""
import cv2
import os
Expand All @@ -178,7 +178,8 @@ def evaluate(self, dataset):
if file_name.endswith('.jpg'):
print(file_name)
image = cv2.imread('./data/boss/' + file_name)
image = resize_with_pad(image, 32, 32)
image = image.reshape((1, 3, 32, 32))
image = resize_with_pad(image)
image = image.reshape((1, 3, IMAGE_SIZE, IMAGE_SIZE))
result = model.predict(image)
print(result)
"""
84 changes: 41 additions & 43 deletions capture.py → camera_reader.py
@@ -1,6 +1,4 @@
# -*- coding:utf-8 -*-
#webカメラの映像から顔を探し白の枠線をつけて保存するプログラム

import threading
from datetime import datetime
import cv2
Expand Down Expand Up @@ -68,46 +66,46 @@ def run(self):
cv2.destroyAllWindows()
"""

if __name__ == '__main__':
cap = cv2.VideoCapture(1)
cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"
model = Model()
model.load()
while True:
ret, frame = cap.read()
# グレースケール変換
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cap = cv2.VideoCapture(1)
cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"
model = Model()
model.load()
while True:
ret, frame = cap.read()
# グレースケール変換
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# カスケード分類器の特徴量を取得する
cascade = cv2.CascadeClassifier(cascade_path)

# 物体認識(顔認識)の実行
facerect = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))
if len(facerect) > 0:
print('face detected')
color = (255, 255, 255) # 白
for rect in facerect:
# 検出した顔を囲む矩形の作成
#cv2.rectangle(frame, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), color, thickness=2)

x, y = rect[0:2]
width, height = rect[2:4]
image = frame[y-50: y + height, x: x + width + 50]
cv2.imwrite('test.jpg', image)
result = model.predict(image)
print(result)
if result == 1: # boss
print('Boss is approaching')
show_image()
else:
print('Not boss')

#10msecキー入力待ち
k = cv2.waitKey(100)
#Escキーを押されたら終了
if k == 27:
break
# カスケード分類器の特徴量を取得する
cascade = cv2.CascadeClassifier(cascade_path)

#キャプチャを終了
cap.release()
cv2.destroyAllWindows()
# 物体認識(顔認識)の実行
facerect = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))
if len(facerect) > 0:
print('face detected')
color = (255, 255, 255) # 白
for rect in facerect:
# 検出した顔を囲む矩形の作成
#cv2.rectangle(frame, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), color, thickness=2)

x, y = rect[0:2]
width, height = rect[2:4]
image = frame[y-50: y + height, x: x + width + 50]
cv2.imwrite('test.jpg', image)
result = model.predict(image)
print(result)
if result == 1: # boss
print('Boss is approaching')
show_image()
else:
print('Not boss')

#10msecキー入力待ち
k = cv2.waitKey(100)
#Escキーを押されたら終了
if k == 27:
break

#キャプチャを終了
cap.release()
cv2.destroyAllWindows()
19 changes: 1 addition & 18 deletions image_show.py
@@ -1,27 +1,10 @@
# -*- coding: utf-8 -*-
import sys

import cv2
from PyQt4 import QtGui


def show_image(image_path='s_pycharm.jpg'):
"""
# 画像の読み込み
RGB = 1
img = cv2.imread(image_path, RGB)
# 画像の表示
cv2.imshow('img', img)
# キーが押させるまで画像を表示したままにする
# 第一引数:キーイベントを待つ時間 0: 無限, 0以上: 指定ミリ秒待つ
cv2.waitKey(0)
# 作成したウィンドウを全て破棄
cv2.destroyAllWindows()
"""

app = QtGui.QApplication(sys.argv)
pixmap = QtGui.QPixmap(image_path)
screen = QtGui.QLabel()
Expand All @@ -31,4 +14,4 @@ def show_image(image_path='s_pycharm.jpg'):


if __name__ == '__main__':
show_image()
show_image()

0 comments on commit 11b1e14

Please sign in to comment.