viswanani commited on
Commit
180d000
Β·
verified Β·
1 Parent(s): a808a28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -46
app.py CHANGED
@@ -1,55 +1,39 @@
1
- import gradio as gr
2
  from deepface import DeepFace
3
- import tempfile
4
  import os
5
 
6
- def analyze_video(video):
7
- if video is None:
8
- return "❌ No video file uploaded."
9
-
10
- try:
11
- # Save video to a temporary path
12
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
13
- tmp.write(video.read())
14
- video_path = tmp.name
15
-
16
- # Analyze video for emotion (can add other actions like age, gender, race)
17
- result = DeepFace.analyze(
18
- video_path,
19
- actions=["emotion"],
20
- detector_backend="retinaface",
21
- enforce_detection=False
22
- )
23
 
24
- # Summarize emotion frequency
25
- emotion_counts = {}
26
- for frame in result:
27
- emotion = frame.get("dominant_emotion")
28
- if emotion:
29
- emotion_counts[emotion] = emotion_counts.get(emotion, 0) + 1
30
 
31
- total_frames = sum(emotion_counts.values())
32
- output = "πŸ“Š **Emotion Analysis:**\n"
33
- for emotion, count in sorted(emotion_counts.items(), key=lambda x: x[1], reverse=True):
34
- output += f"- {emotion}: {count} frames ({(count/total_frames)*100:.1f}%)\n"
35
 
36
- # Basic lie detection logic (can be customized)
37
- if emotion_counts.get("fear", 0)/total_frames > 0.2 or emotion_counts.get("surprise", 0)/total_frames > 0.2:
38
- output += "\nπŸ”΄ **Lie Detected: High fear/surprise levels**"
39
- else:
40
- output += "\n🟒 **No lie patterns detected**"
41
-
42
- return output
43
 
 
 
 
 
44
  except Exception as e:
45
  return f"❌ Error during analysis: {str(e)}"
46
-
47
- with gr.Blocks() as demo:
48
- gr.Markdown("## πŸ•΅οΈ Lie Detection from Video\nUpload a short face-visible video to detect emotional indicators.")
49
- video_input = gr.Video(label="πŸŽ₯ Upload MP4 Video")
50
- output_box = gr.Textbox(label="πŸ“ˆ Results", lines=10)
51
- analyze_btn = gr.Button("πŸ” Analyze Video")
52
-
53
- analyze_btn.click(fn=analyze_video, inputs=video_input, outputs=output_box)
54
-
55
- demo.launch()
 
 
 
1
+ import cv2
2
  from deepface import DeepFace
3
+ import gradio as gr
4
  import os
5
 
6
+ def analyze_emotion_from_video(video_path):
7
+ if not video_path or not os.path.exists(video_path):
8
+ return "❌ Error: No video file found or path is invalid."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ cap = cv2.VideoCapture(video_path)
11
+ if not cap.isOpened():
12
+ return "❌ Error: Could not open the video file."
 
 
 
13
 
14
+ frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
15
+ middle_frame_index = frame_count // 2
16
+ cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_index)
17
+ success, frame = cap.read()
18
 
19
+ if not success or frame is None:
20
+ return "❌ Error: Could not read a valid frame from the video."
 
 
 
 
 
21
 
22
+ try:
23
+ analysis = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
24
+ dominant_emotion = analysis[0]['dominant_emotion']
25
+ return f"βœ… Dominant Emotion: {dominant_emotion}"
26
  except Exception as e:
27
  return f"❌ Error during analysis: {str(e)}"
28
+ finally:
29
+ cap.release()
30
+
31
+ interface = gr.Interface(
32
+ fn=analyze_emotion_from_video,
33
+ inputs=gr.Video(label="Upload a Video File"),
34
+ outputs=gr.Textbox(label="Emotion Analysis Result"),
35
+ title="🎭 Video Emotion Detection",
36
+ description="Upload a short video with a visible face to detect the dominant emotion using DeepFace."
37
+ )
38
+
39
+ interface.launch()