Skip to content

Commit

Permalink
optimized imports
Browse files Browse the repository at this point in the history
  • Loading branch information
daltamur committed Nov 3, 2022
1 parent ba25ab9 commit 1feb25a
Show file tree
Hide file tree
Showing 18 changed files with 460 additions and 455 deletions.
16 changes: 8 additions & 8 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions .idea/openCVFacialRecognition.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 41 additions & 45 deletions cnnModel/model.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
from os.path import exists

import numpy as np
import tensorflow as tf
import os
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

class cnnModel:
def __init__(self):
self.model = None
cnn = input_data(shape=[150, 150, 3])

#32 filters and stride is 5, the filter will move 5 pixels at a time
cnn = conv_2d(cnn, 32, filter_size=5, activation='relu')
cnn = max_pool_2d(cnn, 5)
cnn = conv_2d(cnn, 64, 5, activation='relu')
cnn = max_pool_2d(cnn, 5)
cnn = conv_2d(cnn, 128, 5, activation='relu')
cnn = max_pool_2d(cnn, 5)
cnn = conv_2d(cnn, 256, 5, activation='relu')

#input layer
cnn = fully_connected(cnn, 1024, activation='relu')
self.cnn = dropout(cnn, 0.5)
# input layer
cnn = fully_connected(cnn, 256, activation='relu')
self.cnn = dropout(cnn, 0.5)

def set_output_layer(self, labels):
# output layer
self.labels = labels
self.cnn = fully_connected(self.cnn, len(labels.keys()), activation='softmax')
self.cnn = regression(self.cnn, optimizer='adam', learning_rate=0.001, loss='categorical_crossentropy')

# final model initializer
self.model = tflearn.DNN(self.cnn, tensorboard_verbose=1)

def train(self, x_train, y_train, x_test, y_test):
self.model.fit(x_train, y_train, n_epoch=5, validation_set=(x_test, y_test), show_metric=True)
self.model.save('savedModels/cnn.tfl')

def predict(self, current_face):
import numpy as np
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

class cnnModel:
def __init__(self):
self.model = None
cnn = input_data(shape=[150, 150, 3])

#32 filters and stride is 5, the filter will move 5 pixels at a time
cnn = conv_2d(cnn, 32, filter_size=5, activation='relu')
cnn = max_pool_2d(cnn, 5)
cnn = conv_2d(cnn, 64, 5, activation='relu')
cnn = max_pool_2d(cnn, 5)
cnn = conv_2d(cnn, 128, 5, activation='relu')
cnn = max_pool_2d(cnn, 5)
cnn = conv_2d(cnn, 256, 5, activation='relu')

#input layer
cnn = fully_connected(cnn, 1024, activation='relu')
self.cnn = dropout(cnn, 0.5)
# input layer
cnn = fully_connected(cnn, 256, activation='relu')
self.cnn = dropout(cnn, 0.5)

def set_output_layer(self, labels):
# output layer
self.labels = labels
self.cnn = fully_connected(self.cnn, len(labels.keys()), activation='softmax')
self.cnn = regression(self.cnn, optimizer='adam', learning_rate=0.001, loss='categorical_crossentropy')

# final model initializer
self.model = tflearn.DNN(self.cnn, tensorboard_verbose=1)

def train(self, x_train, y_train, x_test, y_test):
self.model.fit(x_train, y_train, n_epoch=5, validation_set=(x_test, y_test), show_metric=True)
self.model.save('savedModels/cnn.tfl')

def predict(self, current_face):
return self.model.predict(np.array(current_face).reshape(-1, 150, 150, 3))
106 changes: 53 additions & 53 deletions engine.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
from cnnModel import model
from getWebCamImages import take_pictures
from lbphModel import lbph
from trainModels import train
from runDetector import detectFace

lbph_model = lbph.lbphModel()
cnn_model = model.cnnModel()


def run():
cur_command = input("Enter Command (type 'help' to see list of commands): ")

while cur_command != 'exit':
if cur_command == 'take pictures':
take_pictures.takeImages()
print("Pictures added to database!")

elif cur_command == 'train lbph':
train.train_lbph(lbph_model)
print("LBPH model trained successfully!")

elif cur_command == 'train cnn':
train.train_cnn(cnn_model)
print("CNN model trained successfully!")

elif cur_command == 'run predictor lbph':
detectFace.runDetector(lbph_model.name_dict, lbph_model, 'lbph')
print()
print("Predictor closed successfully")

elif cur_command == 'run predictor cnn':
detectFace.runDetector(cnn_model.labels, cnn_model, 'cnn')
print()
print("Predictor closed successfully")

elif cur_command == 'help':
print()
print("Commands:")
print("take pictures: take pictures used for training models")
print('train lbph: train the local binary pattern histogram model')
print('train cnn: train convolutional neural network model (Will take a lot longer, if a model has already been trained previously, you '
'will be given the option to load that model)')
print('run predictor: run the real-time prediction algorithm')
print()

else:
print("No such command!")

cur_command = input("Enter Command (type 'help' to see list of commands): ")


run()
from cnnModel import model
from getWebCamImages import take_pictures
from lbphModel import lbph
from trainModels import train
from runDetector import detectFace

lbph_model = lbph.lbphModel()
cnn_model = model.cnnModel()


def run():
cur_command = input("Enter Command (type 'help' to see list of commands): ")

while cur_command != 'exit':
if cur_command == 'take pictures':
take_pictures.takeImages()
print("Pictures added to database!")

elif cur_command == 'train lbph':
train.train_lbph(lbph_model)
print("LBPH model trained successfully!")

elif cur_command == 'train cnn':
train.train_cnn(cnn_model)
print("CNN model trained successfully!")

elif cur_command == 'run predictor lbph':
detectFace.runDetector(lbph_model.name_dict, lbph_model, 'lbph')
print()
print("Predictor closed successfully")

elif cur_command == 'run predictor cnn':
detectFace.runDetector(cnn_model.labels, cnn_model, 'cnn')
print()
print("Predictor closed successfully")

elif cur_command == 'help':
print()
print("Commands:")
print("take pictures: take pictures used for training models")
print('train lbph: train the local binary pattern histogram model')
print('train cnn: train convolutional neural network model (Will take a lot longer, if a model has already been trained previously, you '
'will be given the option to load that model)')
print('run predictor: run the real-time prediction algorithm')
print()

else:
print("No such command!")

cur_command = input("Enter Command (type 'help' to see list of commands): ")


run()
Loading

0 comments on commit 1feb25a

Please sign in to comment.