LovnishVerma commited on
Commit
17a8870
·
verified ·
1 Parent(s): c7b74ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -51
app.py CHANGED
@@ -2,96 +2,135 @@ import streamlit as st
2
  import cv2
3
  import numpy as np
4
  import time
5
- # Larger title
6
- st.markdown("<h1 style='text-align: center;'>Emotion Detection</h1>", unsafe_allow_html=True)
7
-
8
- # Smaller subtitle
9
- st.markdown("<h3 style='text-align: center;'>angry, fear, happy, neutral, sad, surprise</h3>", unsafe_allow_html=True)
10
- start = time.time()
11
  from keras.models import load_model
12
- import tempfile
13
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
14
 
 
15
  @st.cache_resource
16
  def load_emotion_model():
17
  model = load_model('CNN_Model_acc_75.h5')
18
  return model
19
 
20
- # Load the model
21
  model = load_emotion_model()
22
- print("time taken to load model : " , time.time() - start)
23
- img_shape = 48
 
24
  emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
 
25
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
26
 
27
-
28
  def process_frame(frame):
 
29
  gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
30
  faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
31
-
32
  for (x, y, w, h) in faces:
33
  roi_gray = gray_frame[y:y+h, x:x+w]
34
  roi_color = frame[y:y+h, x:x+w]
35
-
36
  face_roi = cv2.resize(roi_color, (img_shape, img_shape))
37
  face_roi = np.expand_dims(face_roi, axis=0)
38
  face_roi = face_roi / float(img_shape)
39
  predictions = model.predict(face_roi)
40
  emotion = emotion_labels[np.argmax(predictions[0])]
41
 
 
42
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
43
- cv2.putText(frame, emotion, (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
44
-
45
  return frame
46
 
47
- # def video_feed(video_source):
48
- # # Read and process video frames
49
- # while True:
50
- # ret, frame = video_source.read()
51
- # if not ret:
52
- # break
53
- # frame = process_frame(frame)
54
- # st.image(frame, channels="BGR")
55
-
56
- def video_feed(video_source):
57
- # Create a placeholder to display the frames
58
- frame_placeholder = st.empty() # This placeholder will be used to replace frames in-place
59
-
60
- while True:
61
- ret, frame = video_source.read()
62
- if not ret:
63
- break
64
-
65
- frame = process_frame(frame)
66
-
67
- # Display the frame in the placeholder
68
- frame_placeholder.image(frame, channels="BGR", use_column_width=True)
69
-
70
-
71
-
72
- # Sidebar for video or image upload
73
- upload_choice = st.sidebar.radio("Choose input source", [ "Upload Video", "Upload Image" ,"Camera"])
74
 
75
  if upload_choice == "Camera":
76
- # Access camera
77
- video_source = cv2.VideoCapture(0)
78
- video_feed(video_source)
 
 
 
 
 
79
 
80
  elif upload_choice == "Upload Video":
81
  uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi", "mkv", "webm"])
82
  if uploaded_video:
83
- # Temporarily save the video to disk
84
  with tempfile.NamedTemporaryFile(delete=False) as tfile:
85
  tfile.write(uploaded_video.read())
86
  video_source = cv2.VideoCapture(tfile.name)
87
- video_feed(video_source)
 
 
 
 
 
 
 
88
 
89
  elif upload_choice == "Upload Image":
90
- uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg", "gif"])
91
  if uploaded_image:
92
  image = Image.open(uploaded_image)
93
  frame = np.array(image)
94
  frame = process_frame(frame)
95
- st.image(frame, caption='Processed Image', use_column_width=True)
96
-
97
- st.sidebar.write("Emotion Labels: Angry, Fear, Happy, Neutral, Sad, Surprise")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import cv2
3
  import numpy as np
4
  import time
 
 
 
 
 
 
5
  from keras.models import load_model
 
6
  from PIL import Image
7
+ from huggingface_hub import HfApi, Repository
8
+ import os
9
+ import tempfile
10
+
11
+ # Page configuration
12
+ st.set_page_config(page_title="Emotion Detection", layout="centered")
13
+
14
+ # Title and Subtitle
15
+ st.markdown("<h1 style='text-align: center;'>Emotion Detection</h1>", unsafe_allow_html=True)
16
+ st.markdown("<h3 style='text-align: center;'>angry, fear, happy, neutral, sad, surprise</h3>", unsafe_allow_html=True)
17
 
18
+ # Load Model
19
  @st.cache_resource
20
  def load_emotion_model():
21
  model = load_model('CNN_Model_acc_75.h5')
22
  return model
23
 
24
+ start_time = time.time()
25
  model = load_emotion_model()
26
+ st.write(f"Model loaded in {time.time() - start_time:.2f} seconds.")
27
+
28
+ # Emotion labels and constants
29
  emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
30
+ img_shape = 48
31
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
32
 
 
33
  def process_frame(frame):
34
+ """Detect faces and predict emotions."""
35
  gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
36
  faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
 
37
  for (x, y, w, h) in faces:
38
  roi_gray = gray_frame[y:y+h, x:x+w]
39
  roi_color = frame[y:y+h, x:x+w]
 
40
  face_roi = cv2.resize(roi_color, (img_shape, img_shape))
41
  face_roi = np.expand_dims(face_roi, axis=0)
42
  face_roi = face_roi / float(img_shape)
43
  predictions = model.predict(face_roi)
44
  emotion = emotion_labels[np.argmax(predictions[0])]
45
 
46
+ # Draw rectangle and emotion label
47
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
48
+ cv2.putText(frame, emotion, (x, y + h + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
 
49
  return frame
50
 
51
+ # Sidebar for input selection
52
+ st.sidebar.title("Choose Input Source")
53
+ upload_choice = st.sidebar.radio("Select:", ["Camera", "Upload Video", "Upload Image", "Upload to Hugging Face"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  if upload_choice == "Camera":
56
+ # Use Streamlit's camera input widget
57
+ st.sidebar.info("Click a picture to analyze emotion.")
58
+ picture = st.camera_input("Take a picture")
59
+ if picture:
60
+ image = Image.open(picture)
61
+ frame = np.array(image)
62
+ frame = process_frame(frame)
63
+ st.image(frame, caption="Processed Image", use_column_width=True)
64
 
65
  elif upload_choice == "Upload Video":
66
  uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi", "mkv", "webm"])
67
  if uploaded_video:
 
68
  with tempfile.NamedTemporaryFile(delete=False) as tfile:
69
  tfile.write(uploaded_video.read())
70
  video_source = cv2.VideoCapture(tfile.name)
71
+ frame_placeholder = st.empty()
72
+ while video_source.isOpened():
73
+ ret, frame = video_source.read()
74
+ if not ret:
75
+ break
76
+ frame = process_frame(frame)
77
+ frame_placeholder.image(frame, channels="BGR", use_column_width=True)
78
+ video_source.release()
79
 
80
  elif upload_choice == "Upload Image":
81
+ uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
82
  if uploaded_image:
83
  image = Image.open(uploaded_image)
84
  frame = np.array(image)
85
  frame = process_frame(frame)
86
+ st.image(frame, caption="Processed Image", use_column_width=True)
87
+
88
+ elif upload_choice == "Upload to Hugging Face":
89
+ st.sidebar.info("Upload images to the 'known_faces' directory in the Hugging Face repository.")
90
+
91
+ # Configure Hugging Face Repository
92
+ REPO_NAME = "face_emotion_detection2"
93
+ REPO_ID = "LovnishVerma/" + REPO_NAME
94
+ hf_token = os.getenv("uploadphoto1") # Set your Hugging Face token as an environment variable
95
+
96
+ if not hf_token:
97
+ st.error("Hugging Face token not found. Please set it as an environment variable named 'HF_TOKEN'.")
98
+ st.stop()
99
+
100
+ # Initialize Hugging Face API
101
+ api = HfApi()
102
+
103
+ def create_hugging_face_repo():
104
+ """Create or verify the Hugging Face repository."""
105
+ try:
106
+ api.create_repo(repo_id=REPO_ID, repo_type="dataset", token=hf_token, exist_ok=True)
107
+ st.success(f"Repository '{REPO_NAME}' is ready on Hugging Face!")
108
+ except Exception as e:
109
+ st.error(f"Error creating Hugging Face repository: {e}")
110
+
111
+ def upload_to_hugging_face(file):
112
+ """Upload a file to the Hugging Face repository."""
113
+ try:
114
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
115
+ temp_file.write(file.read())
116
+ temp_file_path = temp_file.name
117
+
118
+ api.upload_file(
119
+ path_or_fileobj=temp_file_path,
120
+ path_in_repo=f"known_faces/{os.path.basename(temp_file_path)}",
121
+ repo_id=REPO_ID,
122
+ token=hf_token,
123
+ )
124
+ st.success("File uploaded successfully to Hugging Face!")
125
+ except Exception as e:
126
+ st.error(f"Error uploading file to Hugging Face: {e}")
127
+
128
+ # Create the repository if it doesn't exist
129
+ create_hugging_face_repo()
130
+
131
+ # Upload image file
132
+ hf_uploaded_image = st.file_uploader("Upload Image to Hugging Face", type=["png", "jpg", "jpeg"])
133
+ if hf_uploaded_image:
134
+ upload_to_hugging_face(hf_uploaded_image)
135
+
136
+ st.sidebar.write("Emotion Labels: Angry, Fear, Happy, Neutral, Sad, Surprise")