Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error initializing FER detector: list indices must be integers or slices, not str #66

Open
osama-shehab opened this issue Aug 21, 2024 · 0 comments

Comments

@osama-shehab
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant