Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
takavarasha-desire committed Feb 26, 2024
0 parents commit f4d39b6
Show file tree
Hide file tree
Showing 27 changed files with 41,099 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

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

4 changes: 4 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

8 changes: 8 additions & 0 deletions .idea/realTime7.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/vcs.xml

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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# emotion_recognition
Binary file added __pycache__/cnn_model.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/cnn_models.cpython-39.pyc
Binary file not shown.
68 changes: 68 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import cv2
import time
from keras.models import model_from_json


emotion_labels = ['anger', 'disgust', 'fear', 'joy', 'neutral', 'sadness', 'surprise']
cascPath = "./haarcascade_frontalface_default.xml"

faceCascade = cv2.CascadeClassifier(cascPath)

# load json and create model arch
json_file = open('./models/fer2013_Model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)

# load weights into new model
model.load_weights('./models/fer2013_Model_weights.keras')


def predict_emotion(face_image_gray): # a single cropped face
resized_img = cv2.resize(face_image_gray, (48, 48), interpolation=cv2.INTER_AREA)
image = resized_img.reshape(1, 48, 48)
list_of_list = model.predict(image, batch_size=1, verbose=1)
anger, disgust, fear, joy, neutral, sadness, surprise = [prob for lst in list_of_list for prob in lst]
return [anger, disgust, fear, joy, neutral, sadness, surprise]


video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY, 1)

faces = faceCascade.detectMultiScale(
img_gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(48, 48),
flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
face_image_gray = img_gray[y:y+h, x:x+w]
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Predict emotion probabilities
emotion_probs = predict_emotion(face_image_gray)
predicted_emotion = emotion_labels[emotion_probs.index(max(emotion_probs))]

# Embed predicted emotion label on the frame
label_text = f'Emotion: {predicted_emotion}'
cv2.putText(frame, label_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

with open('emotion.txt', 'a') as f:
f.write('{},{},{},{},{},{},{}\n'.format(time.time(), *emotion_probs))

# Display the resulting frame
cv2.imshow('Video', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break


# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
37 changes: 37 additions & 0 deletions cleaning_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import glob
from PIL import Image
import PIL


# Function to get the class label from the filename
def get_class_label(filename):
class_names = ['anger', 'disgust', 'fear', 'joy', 'neutral', 'sadness', 'surprise']
for class_name in class_names:
if class_name in filename:
return class_name
return None


# Defining the source directories for train and test data
train_directory = 'C:/Users/takav/PycharmProjects/realTime/data_split/train'
test_directory = 'C:/Users/takav/PycharmProjects/realTime/data_split/test'


# Function to remove unidentified images in a directory
def remove_unidentified_images(directory):
for class_name in os.listdir(directory):
class_dir = os.path.join(directory, class_name)
if os.path.isdir(class_dir):
for image_file in glob.glob(os.path.join(class_dir, '*.png')):
try:
# Open the image using PIL to check if it is a valid image
img = Image.open(image_file)
except (IOError, OSError, PIL.UnidentifiedImageError):
print(f"Removed invalid image: {image_file}")
os.remove(image_file)


# Remove unidentified images from the train and test directories
remove_unidentified_images(train_directory)
remove_unidentified_images(test_directory)
Loading

0 comments on commit f4d39b6

Please sign in to comment.