Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| # from transformers import pipeline | |
| from deepface import DeepFace | |
| import numpy as np | |
| # import custom helper functions | |
| from backend import check_image_rotation | |
| # pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog") | |
| st.title("Your Emotions? Or Nah?") | |
| # st.title("Hot Dog? Or Not?") | |
| file_name = st.file_uploader("Upload a photo of your face") | |
| # file_name = st.file_uploader("Upload a hot dog candidate image") | |
| if file_name is not None: | |
| # make two columns | |
| col1, col2 = st.columns(2) | |
| # capture image with intended rotation | |
| image = check_image_rotation(file_name) | |
| # display image in left column | |
| col1.image(image, use_column_width=True) | |
| # capture image data for face analysis | |
| image_data = np.array(image) | |
| # define a list of backends in case face cannot be detected | |
| backends = ['opencv', 'mtcnn', 'retinaface', 'mediapipe', 'ssd'] | |
| # attempt tracker | |
| attempt = 0 | |
| # retry loop | |
| while True: | |
| try: | |
| # capture predictions from deepface emotion model | |
| predictions = DeepFace.analyze(image_data, actions=['emotion'], detector_backend=backends[attempt]) | |
| # ensure only the main prediction object is processed, | |
| if len(predictions) > 1: | |
| # when more than one face is detected by the backend, | |
| faces = [(face, face['region']['w'] * face['region']['h']) for face in predictions] | |
| # by using the predictions connected to the largest bounding box | |
| new_predictions = sorted(faces, key=lambda x: x[1], reverse=True)[0][0] | |
| emotion_dict = new_predictions['emotion'] | |
| else: | |
| emotion_dict = predictions['emotion'] | |
| # capture desired prediction data | |
| emotions = list(emotion_dict.keys()) | |
| probabilities = list(emotion_dict.values()) | |
| # display in the right column... | |
| col2.header("Emotion Probabilities") | |
| # ...each emotion category and its probability | |
| for i in range(len(emotions)): | |
| col2.subheader(f"{emotions[i]}: {probabilities[i]:.2f}%") | |
| break | |
| except Exception as e: | |
| # if the analysis fails to detect a face, try a different backend | |
| attempt += 1 | |
| if attempt < len(backends): | |
| print(f"Retrying with backend `{backends[attempt]}` due to error: {str(e)}") | |
| else: | |
| print(f"Failed to analyze image after attempting all detector backends available. Please upload a new image.") | |