viswanani commited on
Commit
8895c04
Β·
verified Β·
1 Parent(s): 4f97933

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -27
app.py CHANGED
@@ -1,54 +1,55 @@
1
  import gradio as gr
2
  from deepface import DeepFace
 
3
  import os
4
 
5
- def analyze_video(video_file):
6
- if video_file is None:
7
- return "❌ Please upload a video file."
8
-
9
- # Gradio provides a tempfile.NamedTemporaryFile, use .name to get the actual path
10
- video_path = video_file.name
11
 
12
  try:
 
 
 
 
 
 
13
  result = DeepFace.analyze(
14
  video_path,
15
  actions=["emotion"],
16
- enforce_detection=False,
17
- detector_backend="retinaface"
18
  )
19
 
 
20
  emotion_counts = {}
21
- for frame_result in result:
22
- dominant_emotion = frame_result.get("dominant_emotion")
23
- if dominant_emotion:
24
- emotion_counts[dominant_emotion] = emotion_counts.get(dominant_emotion, 0) + 1
25
 
26
  total_frames = sum(emotion_counts.values())
27
- if total_frames == 0:
28
- return "⚠️ No faces or emotions detected in the video."
29
-
30
- output = "πŸ“Š **Detected Emotions Summary:**\n"
31
  for emotion, count in sorted(emotion_counts.items(), key=lambda x: x[1], reverse=True):
32
- percentage = (count / total_frames) * 100
33
- output += f"- {emotion}: {count} frames ({percentage:.2f}%)\n"
34
 
35
- if emotion_counts.get("fear", 0) / total_frames > 0.2 or emotion_counts.get("surprise", 0) / total_frames > 0.2:
36
- output += "\nπŸ”΄ **Potential signs of lying detected!**"
 
37
  else:
38
- output += "\n🟒 **No strong signs of deception detected.**"
39
 
40
  return output
41
 
42
  except Exception as e:
43
- return f"❌ Error analyzing video: {str(e)}"
44
 
45
  with gr.Blocks() as demo:
46
- gr.Markdown("# πŸ•΅οΈ Lie Detection from Video\nUpload a video to detect emotional signals related to deception.")
47
- with gr.Row():
48
- video_input = gr.Video(label="πŸŽ₯ Upload a Video File (MP4 preferred)")
49
- result_output = gr.Textbox(label="πŸ“ˆ Analysis Results", lines=12)
50
  analyze_btn = gr.Button("πŸ” Analyze Video")
51
 
52
- analyze_btn.click(analyze_video, inputs=video_input, outputs=result_output)
53
 
54
  demo.launch()
 
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()