Rivalcoder commited on
Commit
3d07c4d
·
1 Parent(s): e59fdf7
Files changed (1) hide show
  1. app.py +41 -16
app.py CHANGED
@@ -278,28 +278,45 @@ def process_video(video_path: str) -> Dict[str, Any]:
278
  return {
279
  "success": True,
280
  "message": "No faces detected in video",
281
- "results": [],
 
 
 
 
 
 
 
 
282
  "error": None
283
  }
284
 
285
- # Calculate summary statistics
286
- emotions_count = {}
287
  for detection in largest_face_detections:
288
  emotion = detection['emotion']
289
- emotions_count[emotion] = emotions_count.get(emotion, 0) + 1
 
290
 
291
- # Get dominant emotion
292
- dominant_emotion = max(emotions_count.items(), key=lambda x: x[1])[0]
 
 
 
 
 
 
293
 
294
  return {
295
  "success": True,
296
  "message": "Video processed successfully",
297
  "results": {
298
- "detections": largest_face_detections,
 
 
299
  "summary": {
300
  "total_frames": total_frames,
301
  "total_detections": len(largest_face_detections),
302
- "emotions_count": emotions_count,
303
  "dominant_emotion": dominant_emotion
304
  }
305
  },
@@ -317,14 +334,22 @@ def gradio_analyze_video(video_path: str):
317
  summary = result["results"]["summary"]
318
  detections = result["results"]["detections"]
319
 
 
 
 
 
 
 
 
 
 
 
 
320
  output = {
321
- "summary": {
322
- "total_frames": summary["total_frames"],
323
- "faces_detected": summary["total_detections"],
324
- "dominant_emotion": summary["dominant_emotion"],
325
- "emotion_distribution": summary["emotions_count"]
326
- },
327
- "sample_detections": detections[:5] # Show first 5 detections
328
  }
329
  return output
330
 
@@ -404,7 +429,7 @@ async def root():
404
  """
405
 
406
  # Mount Gradio app to FastAPI
407
- app = gr.mount_gradio_app(app, demo, path="/gradio")
408
 
409
  if __name__ == "__main__":
410
  import uvicorn
 
278
  return {
279
  "success": True,
280
  "message": "No faces detected in video",
281
+ "results": {
282
+ "average_emotions": {},
283
+ "dominant_emotion": None,
284
+ "detections": [],
285
+ "summary": {
286
+ "total_frames": total_frames,
287
+ "total_detections": 0
288
+ }
289
+ },
290
  "error": None
291
  }
292
 
293
+ emotion_scores = {e: [] for e in emotions} # Initialize with all emotion types
294
+
295
  for detection in largest_face_detections:
296
  emotion = detection['emotion']
297
+ confidence = detection['confidence']
298
+ emotion_scores[emotion].append(confidence)
299
 
300
+ # Calculate summary statistics
301
+ average_emotions = {
302
+ e: sum(scores)/len(scores) if scores else 0
303
+ for e, scores in emotion_scores.items()
304
+ }
305
+
306
+ # Get dominant emotion based on average confidence
307
+ dominant_emotion = max(average_emotions.items(), key=lambda x: x[1])[0]
308
 
309
  return {
310
  "success": True,
311
  "message": "Video processed successfully",
312
  "results": {
313
+ "average_emotions": average_emotions,
314
+ "dominant_emotion": dominant_emotion,
315
+ "detections": largest_face_detections, # Optional: include all detections
316
  "summary": {
317
  "total_frames": total_frames,
318
  "total_detections": len(largest_face_detections),
319
+ "emotions_count": {e: len(s) for e, s in emotion_scores.items()},
320
  "dominant_emotion": dominant_emotion
321
  }
322
  },
 
334
  summary = result["results"]["summary"]
335
  detections = result["results"]["detections"]
336
 
337
+ # output = {
338
+ # "summary": {
339
+ # "total_frames": summary["total_frames"],
340
+ # "faces_detected": summary["total_detections"],
341
+ # "dominant_emotion": summary["dominant_emotion"],
342
+ # "emotion_distribution": summary["emotions_count"]
343
+ # },
344
+ # "sample_detections": detections[:5] # Show first 5 detections
345
+ # }
346
+ # return output
347
+
348
  output = {
349
+ "average_emotions": result["results"]["average_emotions"],
350
+ "dominant_emotion": result["results"]["dominant_emotion"],
351
+ "frames_analyzed": result["results"]["summary"]["total_frames"],
352
+ "faces_detected": result["results"]["summary"]["total_detections"]
 
 
 
353
  }
354
  return output
355
 
 
429
  """
430
 
431
  # Mount Gradio app to FastAPI
432
+ app = gr.mount_gradio_app(app, demo, path="/")
433
 
434
  if __name__ == "__main__":
435
  import uvicorn