MohamedMotaz commited on
Commit
9dfecdd
1 Parent(s): f278c61

final deployment

Browse files
Files changed (2) hide show
  1. face_emotion_pipeline.py +4 -139
  2. requirements.txt +0 -2
face_emotion_pipeline.py CHANGED
@@ -4,8 +4,9 @@ import numpy as np
4
  from moviepy.editor import VideoFileClip
5
  from retinaface import RetinaFace
6
  from hsemotion.facial_emotions import HSEmotionRecognizer
7
- from torchvision.transforms import ToTensor
8
- device='cpu'
 
9
  recognizer = HSEmotionRecognizer(model_name='enet_b0_8_best_vgaf', device='cpu')
10
 
11
  # Face Detection Function
@@ -37,8 +38,6 @@ def annotate_frame(frame, faces):
37
  # Emotion Classification Function
38
  def classify_emotions(face_image):
39
  """ Classify emotions for the given face image using global recognizer """
40
- face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
41
- face_image = ToTensor()(face_image).unsqueeze(0).to(device)
42
  results = recognizer.predict_emotions(face_image)
43
  if results:
44
  emotion = results[0] # Get the most likely emotion
@@ -63,7 +62,7 @@ def process_video_frames(video_path, temp_output_path, frame_skip=5):
63
  frame = np.copy(frame) # Create a writable copy of the frame
64
  faces = detect_faces(frame)
65
  annotate_frame(frame, faces)
66
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert RGB to BGR for OpenCV
67
  out.write(frame)
68
  frame_count += 1
69
 
@@ -128,140 +127,6 @@ def process_image(input_path, output_path):
128
  cv2.imwrite(combined_output_path, combined_image)
129
 
130
 
131
-
132
-
133
-
134
-
135
-
136
- # import cv2
137
- # import os
138
- # import numpy as np
139
- # from moviepy.editor import VideoFileClip
140
- # from retinaface import RetinaFace
141
- # from hsemotion.facial_emotions import HSEmotionRecognizer
142
-
143
-
144
- # # Initialize recognizer
145
- # recognizer = HSEmotionRecognizer(model_name='enet_b0_8_best_vgaf', device='cpu')
146
-
147
- # # Face Detection Function
148
- # def detect_faces(frame):
149
- # """ Detect faces in the frame using RetinaFace """
150
- # faces = RetinaFace.detect_faces(frame)
151
- # if isinstance(faces, dict):
152
- # face_list = []
153
- # for key in faces.keys():
154
- # face = faces[key]
155
- # facial_area = face['facial_area']
156
- # face_dict = {
157
- # 'box': (facial_area[0], facial_area[1], facial_area[2] - facial_area[0], facial_area[3] - facial_area[1])
158
- # }
159
- # face_list.append(face_dict)
160
- # return face_list
161
- # return []
162
-
163
- # # Annotation Function
164
- # def annotate_frame(frame, faces):
165
- # """ Annotate the frame with recognized emotions using global recognizer """
166
- # for face in faces:
167
- # x, y, w, h = face['box']
168
- # face_image = frame[y:y+h, x:x+w] # Extract face region from frame
169
- # emotion = classify_emotions(face_image)
170
- # cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
171
- # cv2.putText(frame, emotion, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
172
-
173
- # # Emotion Classification Function
174
- # def classify_emotions(face_image):
175
- # """ Classify emotions for the given face image using global recognizer """
176
- # results = recognizer.predict_emotions(face_image)
177
- # if results:
178
- # emotion = results[0] # Get the most likely emotion
179
- # print("=====>",emotion)
180
- # else:
181
- # emotion = 'Unknown'
182
- # return emotion
183
-
184
- # # Process Video Frames
185
- # def process_video_frames(video_path, temp_output_path, frame_skip=5):
186
- # # Load the video
187
- # video_clip = VideoFileClip(video_path)
188
- # fps = video_clip.fps
189
-
190
- # # Initialize output video writer
191
- # out = cv2.VideoWriter(temp_output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (int(video_clip.size[0]), int(video_clip.size[1])))
192
-
193
- # # Iterate through frames, detect faces, and annotate emotions
194
- # frame_count = 0
195
- # for frame in video_clip.iter_frames():
196
- # if frame_count % frame_skip == 0: # Process every nth frame
197
- # frame = np.copy(frame) # Create a writable copy of the frame
198
- # faces = detect_faces(frame)
199
- # annotate_frame(frame, faces)
200
- # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert RGB to BGR for OpenCV RGB2BGR
201
- # out.write(frame)
202
- # frame_count += 1
203
-
204
- # # Release resources and cleanup
205
- # out.release()
206
- # cv2.destroyAllWindows()
207
- # video_clip.close()
208
-
209
- # # Add Audio to Processed Video
210
- # def add_audio_to_video(original_video_path, processed_video_path, output_path):
211
- # try:
212
- # original_clip = VideoFileClip(original_video_path)
213
- # processed_clip = VideoFileClip(processed_video_path)
214
- # final_clip = processed_clip.set_audio(original_clip.audio)
215
- # final_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')
216
- # except Exception as e:
217
- # print(f"Error while combining with audio: {e}")
218
- # finally:
219
- # original_clip.close()
220
- # processed_clip.close()
221
-
222
- # # Process Video
223
- # def process_video(video_path, output_path , skip = 1 , add_audio = True):
224
- # temp_output_path = 'temp_output_video.mp4'
225
-
226
- # # Process video frames and save to a temporary file
227
- # process_video_frames(video_path, temp_output_path, frame_skip=skip) # Adjust frame_skip as needed
228
-
229
- # # Add audio to the processed video
230
- # if add_audio:
231
- # add_audio_to_video(video_path, temp_output_path, output_path)
232
- # else:
233
- # os.rename(temp_output_path, output_path) # Rename the temporary file if audio is not needed
234
-
235
- # # Process Image
236
- # def process_image(input_path, output_path):
237
- # # Ensure output path has a valid extension
238
- # if not output_path.lower().endswith(('.jpg', '.jpeg', '.png','.heic')):
239
- # output_path += '.jpg' # Default to .jpg if no valid extension is found
240
-
241
- # # Step 1: Read input image
242
- # image = cv2.imread(input_path)
243
- # if image is None:
244
- # print(f"Error: Unable to read image at '{input_path}'")
245
- # return
246
-
247
- # # Step 2: Detect faces and annotate emotions
248
- # faces = detect_faces(image)
249
- # annotate_frame(image, faces)
250
-
251
- # # Step 3: Write annotated image to output path
252
- # cv2.imwrite(output_path, image)
253
-
254
- # # Step 4: Combine input and output images horizontally
255
- # input_image = cv2.imread(input_path)
256
- # combined_image = cv2.hconcat([input_image, image])
257
- # combined_image = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB)
258
-
259
- # # Step 5: Save the combined image
260
- # combined_output_path = os.path.splitext(output_path)[0] + '_combined.jpg'
261
-
262
- # cv2.imwrite(combined_output_path, combined_image)
263
-
264
-
265
  ###########################
266
 
267
  # recognizer = HSEmotionRecognizer(model_name='enet_b0_8_best_vgaf', device='cpu')
 
4
  from moviepy.editor import VideoFileClip
5
  from retinaface import RetinaFace
6
  from hsemotion.facial_emotions import HSEmotionRecognizer
7
+
8
+
9
+ # Initialize recognizer
10
  recognizer = HSEmotionRecognizer(model_name='enet_b0_8_best_vgaf', device='cpu')
11
 
12
  # Face Detection Function
 
38
  # Emotion Classification Function
39
  def classify_emotions(face_image):
40
  """ Classify emotions for the given face image using global recognizer """
 
 
41
  results = recognizer.predict_emotions(face_image)
42
  if results:
43
  emotion = results[0] # Get the most likely emotion
 
62
  frame = np.copy(frame) # Create a writable copy of the frame
63
  faces = detect_faces(frame)
64
  annotate_frame(frame, faces)
65
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert RGB to BGR for OpenCV RGB2BGR
66
  out.write(frame)
67
  frame_count += 1
68
 
 
127
  cv2.imwrite(combined_output_path, combined_image)
128
 
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  ###########################
131
 
132
  # recognizer = HSEmotionRecognizer(model_name='enet_b0_8_best_vgaf', device='cpu')
requirements.txt CHANGED
@@ -3,5 +3,3 @@ moviepy==1.0.3
3
  hsemotion==0.3.0
4
  retina-face==0.0.17
5
  tf_keras==2.15.1
6
- torch==2.2.0
7
- torchvision==0.17.0
 
3
  hsemotion==0.3.0
4
  retina-face==0.0.17
5
  tf_keras==2.15.1