logu29 commited on
Commit
f148efd
·
verified ·
1 Parent(s): a7b3ad1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py CHANGED
@@ -27,6 +27,36 @@ def analyze_video(video_file):
27
  cap = cv2.VideoCapture(temp_path)
28
  success, frame = cap.read()
29
  cap.release()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  if not success:
32
  return "Could not read video"
 
27
  cap = cv2.VideoCapture(temp_path)
28
  success, frame = cap.read()
29
  cap.release()
30
+
31
+ def analyze_video_emotion(video_file):
32
+ # Save the uploaded video to a temp file
33
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
34
+ tmp.write(video_file.read())
35
+ tmp_path = tmp.name
36
+
37
+ cap = cv2.VideoCapture(tmp_path)
38
+ emotions = []
39
+ frame_count = 0
40
+
41
+ while cap.isOpened():
42
+ ret, frame = cap.read()
43
+ if not ret or frame_count > 60: # Limit to 60 frames max
44
+ break
45
+ try:
46
+ result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
47
+ emotions.append(result[0]['dominant_emotion'])
48
+ except:
49
+ pass
50
+ frame_count += 1
51
+
52
+ cap.release()
53
+
54
+ if emotions:
55
+ # Return most common emotion
56
+ return max(set(emotions), key=emotions.count)
57
+ else:
58
+ return "No face detected"
59
+
60
 
61
  if not success:
62
  return "Could not read video"