LovnishVerma commited on
Commit
ae4e42c
·
verified ·
1 Parent(s): 2bce874

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -15,6 +15,9 @@ EMOTION_MODEL_FILE = "CNN_Model_acc_75.h5"
15
  HUGGING_FACE_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
16
  HUGGING_FACE_REPO = "username/repo_name"
17
 
 
 
 
18
  # Create directories
19
  os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
20
 
@@ -80,7 +83,7 @@ def detect_faces_and_emotions(image):
80
  reshaped_face = np.reshape(normalized_face, (1, 48, 48, 1))
81
  emotion_prediction = emotion_model.predict(reshaped_face)
82
  emotion_label = np.argmax(emotion_prediction)
83
- return emotion_label
84
 
85
  return None
86
 
@@ -134,17 +137,16 @@ elif choice == "Face Recognition and Emotion Detection":
134
  st.error(f"Error: {e}")
135
 
136
  elif action == "Use Webcam":
137
- cap = cv2.VideoCapture(0)
138
- if st.button("Start Webcam"):
139
- while True:
140
- ret, frame = cap.read()
141
- if not ret:
142
- break
143
- emotion_label = detect_faces_and_emotions(frame)
144
  if emotion_label is not None:
145
  st.success(f"Emotion Detected: {emotion_label}")
146
- cv2.imshow("Webcam", frame)
147
- if cv2.waitKey(1) & 0xFF == ord('q'):
148
- break
149
- cap.release()
150
- cv2.destroyAllWindows()
 
15
  HUGGING_FACE_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
16
  HUGGING_FACE_REPO = "username/repo_name"
17
 
18
+ # Emotion Labels
19
+ EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
20
+
21
  # Create directories
22
  os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
23
 
 
83
  reshaped_face = np.reshape(normalized_face, (1, 48, 48, 1))
84
  emotion_prediction = emotion_model.predict(reshaped_face)
85
  emotion_label = np.argmax(emotion_prediction)
86
+ return EMOTION_LABELS[emotion_label]
87
 
88
  return None
89
 
 
137
  st.error(f"Error: {e}")
138
 
139
  elif action == "Use Webcam":
140
+ st.info("Use the camera input widget to capture an image.")
141
+ camera_image = st.camera_input("Take a picture")
142
+ if camera_image:
143
+ try:
144
+ img = Image.open(camera_image)
145
+ img_array = np.array(img)
146
+ emotion_label = detect_faces_and_emotions(img_array)
147
  if emotion_label is not None:
148
  st.success(f"Emotion Detected: {emotion_label}")
149
+ else:
150
+ st.warning("No face detected.")
151
+ except Exception as e:
152
+ st.error(f"Error: {e}")