-
Notifications
You must be signed in to change notification settings - Fork 89
Open
Description
def detect_emotion(image_path):
"""Detect emotion from the given image using FER."""
print("Image Path: ", image_path)
try:
# Convert the image path to a NumPy ndarray
frame = cv2.imread(image_path)
if frame is None:
raise ValueError(f"Image at path {image_path} could not be loaded.")
print("Frame read successfully")
print("Type of frame:", type(frame))
# Initialize the FER detector with the MTCNN detector with error handling
try:
fer_detector = FER(mtcnn=True)
print("FER detector initialized successfully.")
except Exception as fer_error:
print(f"Error initializing FER detector: {fer_error}")
return None
# Analyze emotions using FER
try:
emotion_results = fer_detector.detect_emotions(frame)
print("Emotion Results: ", emotion_results) # Debugging output
except Exception as detect_error:
print(f"Error detecting emotions: {detect_error}")
return None
if not emotion_results:
print("No emotions detected.")
return None
# Ensure the result is a list of dictionaries
if isinstance(emotion_results, list) and len(emotion_results) > 0:
first_result = emotion_results[0]
if isinstance(first_result, dict) and 'emotions' in first_result:
emotions = first_result['emotions']
if isinstance(emotions, dict):
dominant_emotion = max(emotions, key=emotions.get)
return dominant_emotion
else:
print("Emotions data is not a dictionary.")
else:
print("No 'emotions' key found in the first result.")
else:
print("Emotion results are not in the expected format.")
return None
except Exception as e:
print(f"Error in FER emotion detection: {e}")
return None
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels