Skip to content

Commit

Permalink
All_Inclusive
Browse files Browse the repository at this point in the history
This contains the training files, models and implementation with device cameras. Also included is an attempt to convert to an app.
  • Loading branch information
2abet committed Jan 16, 2024
1 parent 03a8c39 commit 3658a8c
Show file tree
Hide file tree
Showing 38,224 changed files with 1,658,067 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Auto detect text files and perform LF normalization
* text=auto
*.pkg filter=lfs diff=lfs merge=lfs -text
*.exe filter=lfs diff=lfs merge=lfs -text
60 changes: 60 additions & 0 deletions Camera_tflite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.camera import Camera
from kivy.uix.label import Label
from kivy.graphics.texture import Texture
import tensorflow as tf
import tensorflow_hub as hub
from kivy.clock import Clock
import cv2
import numpy as np

class MyCameraApp(App):
def build(self):
self.layout = BoxLayout(orientation='vertical')
self.camera = Camera(play=True, resolution=(500, 500))
self.label = Label(text='Emotion will be displayed here')
self.layout.add_widget(self.camera)
self.layout.add_widget(self.label)
return self.layout

# def on_start(self):
# # Load the TensorFlow Hub module
# module_handle = 'https://www.kaggle.com/models/google/mobilenet-v2/frameworks/TensorFlow2/variations/035-224-feature-vector/versions/2'
# module = hub.load(module_handle)
# with hub.custom_object_scope({'KerasLayer': hub.KerasLayer(module)}):
# self.model = tf.keras.models.load_model('Face (1).h5', custom_objects=custom_objects)
# Clock.schedule_interval(self.update, 1.0 / 30.0) # Update at 30 FPS

def on_start(self):
# Load the TensorFlow Hub module
module_handle = 'https://www.kaggle.com/models/google/mobilenet-v2/frameworks/TensorFlow2/variations/035-224-feature-vector/versions/2'
self.model = tf.keras.models.load_model('Face.h5', custom_objects={'KerasLayer': hub.KerasLayer})
Clock.schedule_interval(self.update, 1.0 / 30.0) # Update at 30 FPS


def update(self, *args):
# Capture image from camera
camera_texture = self.camera.texture
camera_image_array = self.texture_to_array(camera_texture)
emotion = self.predict_emotion(camera_image_array)
self.label.text = f'Emotion: {emotion}'

def texture_to_array(self, texture):
buffer = texture.pixels
image_array = np.frombuffer(buffer, dtype=np.uint8)
image_array = image_array.reshape(texture.height, texture.width, 4)
image_array = cv2.cvtColor(image_array, cv2.COLOR_RGBA2RGB)
image_array = cv2.resize(image_array, (224, 224)) # Resize as per model input
image_array = image_array / 255.0 # Normalize
return image_array

def predict_emotion(self, image_array):
image_array = np.expand_dims(image_array, axis=0)
prediction = self.model.predict(image_array)
predicted_class = np.argmax(prediction, axis=1)
emotions = ["angry", "disgust", "fear", "happy", "neutral", "sad", "surprise"]
return emotions[predicted_class[0]]

if __name__ == '__main__':
MyCameraApp().run()
18 changes: 18 additions & 0 deletions Convert_Keras_tflite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import tensorflow as tf
import tensorflow_hub as hub

# Specify the custom object
custom_objects = {'KerasLayer': hub.KerasLayer}

# Load your model with the custom object
model = tf.keras.models.load_model('Face.keras', custom_objects=custom_objects)

# Convert the model to TFLite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# Enable TF Select Ops
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

# Save the TFLite model to a file
with open('D:/Facial/archive (2)/Face.tflite', 'wb') as f:
f.write(tflite_model)
71 changes: 71 additions & 0 deletions FED.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import tensorflow as tf
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
import random
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense
from tensorflow.keras.models import Model

'''
img_array=cv2.imread("train/angry/Training_3908.jpg")
plt.imshow(img_array)
'''
training = "D:/Facial/archive (2)/train" # The training directory

classes = ["angry", "disgust", "fear", "happy", "neutral", "sad", "surprise"]

for category in classes:
path = os.path.join(training, category)
for img in os.listdir(path):
img_array= cv2.imread(os.path.join(path,img))


img_resize= 224
resized_array= cv2.resize(img_array, (img_resize,img_resize))
plt.imshow(cv2.cvtColor(resized_array, cv2.COLOR_BGR2RGB))
plt.show()

training_data = []

def create_training_data():
for category in classes:
path = os.path.join(training, category)
class_num = classes.index(category)
for img in os.listdir(path):
try:
img_array= cv2.imread(os.path.join(path,img))
resized_array = cv2.resize(img_array, (img_resize,img_resize))
training_data.append([resized_array, class_num])
except Exception as e:
pass
create_training_data()

random.shuffle(training_data)

X=[]
y=[]

for features, label in training_data:
X.append(features)
y.append(label)

X= np.array(X).reshape(-1, img_resize, img_resize, 3)

X=X/255.0

base_model = tf.keras.applications.MobileNetV2()

base_input = base_model.layers[0].input
base_output = base_model.layers[-2].output

final_output= layers.Dense(128)(base_output)
final_output = layers.Activation ('relu')(final_output)
final_output = layers.Dense(64)(final_output)
final_output = layers.Activation ('relu')(final_output)
final_output = layers.Dense(7, activation = 'softmax')(final_output)

final_model = keras.Model(inputs = base_input, outputs=final_output)
Binary file added Face (1).h5
Binary file not shown.
Binary file added Face.h5
Binary file not shown.
Binary file added Face.keras
Binary file not shown.
Binary file added Face.tflite
Binary file not shown.
Loading

0 comments on commit 3658a8c

Please sign in to comment.