LovnishVerma commited on
Commit
e8a2db8
·
verified ·
1 Parent(s): cd66699

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -61,6 +61,8 @@ def process_frame(frame):
61
  gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
62
  faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
63
 
 
 
64
  for (x, y, w, h) in faces:
65
  roi_gray = gray_frame[y:y+h, x:x+w]
66
  roi_color = frame[y:y+h, x:x+w]
@@ -79,26 +81,33 @@ def process_frame(frame):
79
  if confidence < 100:
80
  name = known_names[label]
81
 
 
 
 
82
  # Draw bounding box and label on the frame
83
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
84
- cv2.putText(frame, f"{name} - {emotion}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
85
 
86
- return frame
87
 
88
  # Video feed
89
  def video_feed(video_source):
90
  frame_placeholder = st.empty() # This placeholder will be used to replace frames in-place
 
91
 
92
  while True:
93
  ret, frame = video_source.read()
94
  if not ret:
95
  break
96
 
97
- frame = process_frame(frame)
98
 
99
  # Display the frame in the placeholder
100
  frame_placeholder.image(frame, channels="BGR", use_column_width=True)
101
 
 
 
 
102
  # Sidebar for video or image upload
103
  upload_choice = st.sidebar.radio("Choose input source", ["Upload Video", "Upload Image", "Camera"])
104
 
@@ -121,7 +130,8 @@ elif upload_choice == "Upload Image":
121
  if uploaded_image:
122
  image = Image.open(uploaded_image)
123
  frame = np.array(image)
124
- frame = process_frame(frame)
125
  st.image(frame, caption='Processed Image', use_column_width=True)
 
126
 
127
  st.sidebar.write("Emotion Labels: Angry, Fear, Happy, Neutral, Sad, Surprise")
 
61
  gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
62
  faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
63
 
64
+ result_text = "" # Initialize the result text for display
65
+
66
  for (x, y, w, h) in faces:
67
  roi_gray = gray_frame[y:y+h, x:x+w]
68
  roi_color = frame[y:y+h, x:x+w]
 
81
  if confidence < 100:
82
  name = known_names[label]
83
 
84
+ # Format the result text as "Name is feeling Emotion"
85
+ result_text = f"{name} is feeling {emotion}"
86
+
87
  # Draw bounding box and label on the frame
88
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
89
+ cv2.putText(frame, result_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
90
 
91
+ return frame, result_text
92
 
93
  # Video feed
94
  def video_feed(video_source):
95
  frame_placeholder = st.empty() # This placeholder will be used to replace frames in-place
96
+ text_placeholder = st.empty() # This placeholder will display the result text
97
 
98
  while True:
99
  ret, frame = video_source.read()
100
  if not ret:
101
  break
102
 
103
+ frame, result_text = process_frame(frame)
104
 
105
  # Display the frame in the placeholder
106
  frame_placeholder.image(frame, channels="BGR", use_column_width=True)
107
 
108
+ # Display the result text in the text placeholder
109
+ text_placeholder.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
110
+
111
  # Sidebar for video or image upload
112
  upload_choice = st.sidebar.radio("Choose input source", ["Upload Video", "Upload Image", "Camera"])
113
 
 
130
  if uploaded_image:
131
  image = Image.open(uploaded_image)
132
  frame = np.array(image)
133
+ frame, result_text = process_frame(frame)
134
  st.image(frame, caption='Processed Image', use_column_width=True)
135
+ st.markdown(f"<h3 style='text-align: center;'>{result_text}</h3>", unsafe_allow_html=True)
136
 
137
  st.sidebar.write("Emotion Labels: Angry, Fear, Happy, Neutral, Sad, Surprise")