-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_generator.py
More file actions
796 lines (682 loc) · 34.4 KB
/
metrics_generator.py
File metadata and controls
796 lines (682 loc) · 34.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
import json
import os
import math
import numpy
from collections import defaultdict
import matplotlib.pyplot as plt
from datetime import datetime
import glob
from io import StringIO
import sys
from pymongo import MongoClient
class GazeObjectAnalyzer:
def __init__(self, proximity_threshold=0.1):
"""
Initialize the analyzer.
"""
self.proximity_threshold = proximity_threshold
self.video_dimensions = {}
self.object_data = {}
self.gaze_data = {}
self.emotion_data = {}
self.demographic_data = {}
def load_object_detection_data(self, file_path):
"""
Load object detection data.
"""
try:
with open(file_path, 'r') as f:
data = json.load(f)
video_id = data.get("_id", os.path.basename(file_path).split('.')[0])
self.video_dimensions[video_id] = {
"width": data.get("video_width", 1920),
"height": data.get("video_height", 1080)
}
self.object_data[video_id] = {
"frames": data.get("extracted_objects", []),
"ad_duration": data.get("ad_duration", 0)
}
print(f"Loaded object detection data for video ID: {video_id}")
return video_id
except Exception as e:
print(f"Error loading object detection data: {e}")
return None
def load_gaze_emotion_data(self, file_path):
"""
Load gaze and emotion data.
"""
try:
with open(file_path, 'r') as f:
data = json.load(f)
ad_id = data.get("ad_id", os.path.basename(file_path).split('.')[0])
self.gaze_data[ad_id] = data.get("gaze_data", [])
self.emotion_data[ad_id] = data.get("emotion_data", [])
self.demographic_data[ad_id] = data.get("demographic_data", {})
print(f"Loaded gaze and emotion data for ad ID: {ad_id}")
return ad_id
except Exception as e:
print(f"Error loading gaze and emotion data: {e}")
return None
def match_video_with_gaze_data(self, video_id, ad_id):
"""
Match object detection data with gaze data (if IDs differ).
"""
if video_id != ad_id:
self.gaze_data[video_id] = self.gaze_data.get(ad_id, [])
self.emotion_data[video_id] = self.emotion_data.get(ad_id, [])
self.demographic_data[video_id] = self.demographic_data.get(ad_id, {})
def calculate_distance(self, gaze_x, gaze_y, box, video_width, video_height):
"""
Calculate normalized distance between gaze and box center.
Handles both percentage (0-100) and absolute pixel gaze coordinates.
"""
# Check if gaze coordinates seem like absolute pixels (e.g., > 100)
# or percentages (<= 100). This assumes percentages are capped at 100.
if gaze_x > 100 or gaze_y > 100 or gaze_x < 0 or gaze_y < 0:
# Assume absolute pixel coordinates if outside typical 0-100 range
abs_gaze_x = gaze_x
abs_gaze_y = gaze_y
else:
# Assume percentage coordinates (0-100) and convert
abs_gaze_x = gaze_x * video_width / 100
abs_gaze_y = gaze_y * video_height / 100
box_center_x = (box["x1"] + box["x2"]) / 2
box_center_y = (box["y1"] + box["y2"]) / 2
# Ensure box coordinates are valid numbers before comparison
try:
x1, y1, x2, y2 = float(box["x1"]), float(box["y1"]), float(box["x2"]), float(box["y2"])
is_inside = (x1 <= abs_gaze_x <= x2 and
y1 <= abs_gaze_y <= y2)
except (TypeError, ValueError):
# Handle cases where box coordinates might not be valid numbers
is_inside = False
print(f"Warning: Invalid box coordinates encountered: {box}")
dx = abs_gaze_x - box_center_x
dy = abs_gaze_y - box_center_y
# Avoid division by zero if width or height is 0
if video_width > 0 and video_height > 0:
diagonal = math.sqrt(video_width**2 + video_height**2)
distance = math.sqrt(dx**2 + dy**2) / diagonal if diagonal > 0 else 0
else:
distance = 0 # Or handle as an error case
return distance, is_inside
def get_frame_index_from_time(self, video_time, fps=30):
"""
Convert video time to frame index.
"""
return int(video_time * fps)
def analyze_gaze_on_objects(self, video_id):
"""
Analyze gaze data in relation to detected objects.
"""
if video_id not in self.object_data or video_id not in self.gaze_data:
print(f"Missing data for video ID: {video_id}")
return None
video_width = self.video_dimensions[video_id]["width"]
video_height = self.video_dimensions[video_id]["height"]
frames = self.object_data[video_id]["frames"]
gaze_points = self.gaze_data[video_id]
emotion_records = self.emotion_data.get(video_id, [])
# Debug emotion data
if emotion_records:
emotion_counts = {}
for e in emotion_records:
emotion = e.get("emotion", "unknown")
emotion_counts[emotion] = emotion_counts.get(emotion, 0) + 1
print(f"Available emotions for analysis: {emotion_counts}")
results = {
"video_id": video_id,
"objects": defaultdict(lambda: {
"total_gaze_time": 0,
"direct_gaze_time": 0,
"proximity_gaze_time": 0,
"attention_percentage": 0,
"emotions": defaultdict(float),
"instances": []
}),
"total_view_time": 0,
"off_object_time": 0,
"demographics": self.demographic_data.get(video_id, {}),
"frames_with_objects": 0,
"frames_without_objects": 0,
"emotion_totals": defaultdict(float), # Track overall emotion time
"non_object_emotions": defaultdict(float) # For emotions when not looking at objects
}
for frame in frames:
if frame:
results["frames_with_objects"] += 1
else:
results["frames_without_objects"] += 1
if not gaze_points:
print("No gaze points, skipping...")
return results
# Build emotion timestamp lookup for fast access
emotion_by_timestamp = {}
for emotion_entry in emotion_records:
emotion_by_timestamp[emotion_entry["timestamp"]] = emotion_entry["emotion"]
for i in range(1, len(gaze_points)):
curr_gaze = gaze_points[i]
prev_gaze = gaze_points[i - 1]
if not curr_gaze.get("isWithinVideo", True) or not curr_gaze.get("isVideoPlaying", True):
continue
time_interval = (curr_gaze["videoTime"] - prev_gaze["videoTime"])
if time_interval <= 0: # Only check for non-positive intervals
continue
results["total_view_time"] += time_interval
frame_idx = self.get_frame_index_from_time(curr_gaze["videoTime"])
# Handle cases where frame_idx might be out of bounds
if frame_idx >= len(frames):
continue # Or some other appropriate action
frame_objects = frames[frame_idx] if frame_idx < len(frames) else []
# Find current emotion - use improved matching algorithm
current_emotion = "unknown"
closest_emotion_time = float('inf')
curr_timestamp = curr_gaze["timestamp"]
# Look for the closest emotion timestamp
for emotion_entry in emotion_records:
time_diff = abs(emotion_entry["timestamp"] - curr_timestamp)
if time_diff < closest_emotion_time and time_diff < 1000: # Within 1 second
closest_emotion_time = time_diff
current_emotion = emotion_entry["emotion"]
# Track all emotions regardless of objects
results["emotion_totals"][current_emotion] += time_interval
if not frame_objects:
results["off_object_time"] += time_interval
results["non_object_emotions"][current_emotion] += time_interval
continue
gaze_on_object = False
for obj in frame_objects:
obj_name = obj["name"]
box = obj["box"]
distance, is_inside = self.calculate_distance(
curr_gaze["gazeX"], curr_gaze["gazeY"], box, video_width, video_height
)
if is_inside:
results["objects"][obj_name]["direct_gaze_time"] += time_interval
results["objects"][obj_name]["total_gaze_time"] += time_interval
results["objects"][obj_name]["emotions"][current_emotion] += time_interval
gaze_on_object = True
results["objects"][obj_name]["instances"].append({
"time": curr_gaze["videoTime"],
"duration": time_interval,
"is_direct": True,
"distance": 0,
"emotion": current_emotion
})
elif distance < self.proximity_threshold:
results["objects"][obj_name]["proximity_gaze_time"] += time_interval
results["objects"][obj_name]["total_gaze_time"] += time_interval
results["objects"][obj_name]["emotions"][current_emotion] += time_interval
gaze_on_object = True
results["objects"][obj_name]["instances"].append({
"time": curr_gaze["videoTime"],
"duration": time_interval,
"is_direct": False,
"distance": distance,
"emotion": current_emotion
})
if not gaze_on_object:
results["off_object_time"] += time_interval
results["non_object_emotions"][current_emotion] += time_interval
for obj_name, obj_data in results["objects"].items():
if results["total_view_time"] > 0:
obj_data["attention_percentage"] = (obj_data["total_gaze_time"] / results["total_view_time"]) * 100
return results
def compare_multiple_viewers(self, results_list):
"""
Compare results from multiple viewers.
"""
if not results_list:
return None
comparison = {
"video_id": results_list[0]["video_id"],
"total_viewers": len(results_list),
"demographics": {
"gender_distribution": defaultdict(int),
"age_groups": defaultdict(int)
},
"objects": defaultdict(lambda: {
"avg_attention_percentage": 0,
"attention_variance": 0,
"median_attention": 0,
"max_attention": 0,
"min_attention": 0,
"dominant_emotion": {},
"viewer_count": 0
}),
"emotion_distribution": defaultdict(float)
}
for result in results_list:
if not result.get("demographics"):
continue
# Collect demographics data
demographics = result.get("demographics", {})
gender = demographics.get("gender", "unknown")
age_group = demographics.get("ageGroup", "unknown")
comparison["demographics"]["gender_distribution"][gender] += 1
comparison["demographics"]["age_groups"][age_group] += 1
all_objects = set()
for result in results_list:
all_objects.update(result["objects"].keys())
for obj_name in all_objects:
attention_percentages = []
emotion_counts = defaultdict(float)
for result in results_list:
obj_data = result["objects"].get(obj_name, {})
if obj_data:
attention_percentages.append(obj_data.get("attention_percentage", 0))
for emotion, time in obj_data.get("emotions", {}).items():
emotion_counts[emotion] += time
if not attention_percentages:
continue
comparison["objects"][obj_name] = {
"avg_attention_percentage": numpy.mean(attention_percentages),
"attention_variance": numpy.var(attention_percentages),
"median_attention": numpy.median(attention_percentages),
"max_attention": max(attention_percentages),
"min_attention": min(attention_percentages),
"dominant_emotion": max(emotion_counts.items(), key=lambda x: x[1]) if emotion_counts else ("unknown", 0),
"viewer_count": len(attention_percentages)
}
total_emotion_time = defaultdict(float)
total_time = 0
for result in results_list:
for obj_data in result["objects"].values():
for emotion, time in obj_data.get("emotions", {}).items():
total_emotion_time[emotion] += time
total_time += time
if total_time > 0:
for emotion, time in total_emotion_time.items():
comparison["emotion_distribution"][emotion] = (time / total_time) * 100
return comparison
###
def generate_report(self, results_list, comparison=None):
"""
Generate a comprehensive report and return it as a JSON structure.
"""
if not results_list:
return {"error": "No results to generate report from"}
# Initialize the JSON report structure
report = {
"report_metadata": {
"generated_on": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"video_id": results_list[0].get("video_id", "unknown"),
"num_viewers_analyzed": len(results_list)
},
"overall_statistics": {
"total_frames": results_list[0].get("frames_with_objects", 0) + results_list[0].get("frames_without_objects", 0),
"frames_with_objects": results_list[0].get("frames_with_objects", 0),
"frames_without_objects": results_list[0].get("frames_without_objects", 0)
},
"viewer_demographics": {},
"object_attention_analysis": {},
"emotion_distribution": {},
"gaze_patterns": [],
"conclusions": {
"recommendations": []
},
"raw_text_report": "" # For backward compatibility if needed
}
# Calculate frame percentages
total_frames = report["overall_statistics"]["total_frames"]
if total_frames > 0:
report["overall_statistics"]["frames_with_objects_percentage"] = (results_list[0].get("frames_with_objects", 0) / total_frames) * 100
report["overall_statistics"]["frames_without_objects_percentage"] = (results_list[0].get("frames_without_objects", 0) / total_frames) * 100
# Demographics summary (if available)
if comparison:
# Calculate total viewers with demographic data for percentage calculation
total_gender_viewers = sum(comparison.get("demographics", {}).get("gender_distribution", {}).values())
total_age_viewers = sum(comparison.get("demographics", {}).get("age_groups", {}).values())
gender_distribution_percent = {
gender: (count / total_gender_viewers) * 100 if total_gender_viewers > 0 else 0
for gender, count in comparison.get("demographics", {}).get("gender_distribution", {}).items()
}
age_groups_percent = {
age_group: (count / total_age_viewers) * 100 if total_age_viewers > 0 else 0
for age_group, count in comparison.get("demographics", {}).get("age_groups", {}).items()
}
report["viewer_demographics"] = {
"gender_distribution": gender_distribution_percent,
"age_groups": age_groups_percent
}
else:
# Single viewer demographics
result = results_list[0]
demographics = result.get("demographics", {})
if demographics:
gender = demographics.get("gender", "unknown")
age_group = demographics.get("ageGroup", "unknown")
if not age_group and "age" in demographics:
# If ageGroup not available but age is, determine age group
age = demographics.get("age", 0)
if age < 18:
age_group = "under-18"
elif age < 30:
age_group = "18-29"
elif age < 45:
age_group = "30-44"
elif age < 60:
age_group = "45-59"
else:
age_group = "60+"
# Create single-viewer demographics structure
report["viewer_demographics"] = {
"gender_distribution": {gender: 100},
"age_groups": {age_group: 100}
}
print(f"Added single viewer demographics: {gender}, {age_group}")
# Object attention analysis
if comparison:
# Multi-viewer comparison
report["object_attention_analysis"] = {
obj_name: {
"viewed_by_count": obj_data["viewer_count"],
"viewed_by_percentage": (obj_data["viewer_count"] / comparison["total_viewers"]) * 100 if comparison["total_viewers"] > 0 else 0,
"avg_attention_percentage": obj_data["avg_attention_percentage"],
"attention_range": {
"min": obj_data["min_attention"],
"max": obj_data["max_attention"]
},
"attention_variance": obj_data["attention_variance"],
"dominant_emotion": {
"name": obj_data["dominant_emotion"][0] if obj_data["dominant_emotion"] else "unknown",
"time": obj_data["dominant_emotion"][1] if obj_data["dominant_emotion"] else 0
}
} for obj_name, obj_data in comparison["objects"].items()
}
# Emotion distribution from comparison
report["emotion_distribution"] = comparison["emotion_distribution"]
else:
# Single viewer analysis
report["object_attention_analysis"] = {
obj_name: {
"total_gaze_time": obj_data["total_gaze_time"],
"direct_gaze_time": obj_data["direct_gaze_time"],
"proximity_gaze_time": obj_data["proximity_gaze_time"],
"attention_percentage": obj_data["attention_percentage"],
"emotions": obj_data.get("emotions", {})
} for obj_name, obj_data in results_list[0]["objects"].items()
}
# ------------ IMPROVED EMOTION DISTRIBUTION FOR SINGLE VIEWER ------------
# Use emotion_totals if available (from our improved analyze_gaze_on_objects method)
result = results_list[0]
if "emotion_totals" in result and result["emotion_totals"]:
total_time = sum(result["emotion_totals"].values())
if total_time > 0:
report["emotion_distribution"] = {
emotion: (time / total_time) * 100
for emotion, time in result["emotion_totals"].items()
}
print(f"Using emotion_totals for distribution: {report['emotion_distribution']}")
else:
# Fall back to aggregating from object emotions if emotion_totals not available
all_emotions = {}
total_emotion_time = 0
for obj_data in result["objects"].values():
for emotion, time in obj_data.get("emotions", {}).items():
all_emotions[emotion] = all_emotions.get(emotion, 0) + time
total_emotion_time += time
# Calculate percentages
if total_emotion_time > 0:
report["emotion_distribution"] = {
emotion: (time / total_emotion_time) * 100
for emotion, time in all_emotions.items()
}
print(f"Using aggregated object emotions: {report['emotion_distribution']}")
elif "emotion_data" in self.emotion_data.get(result["video_id"], {}):
# If still no emotions, use raw emotion data counts as a last resort
emotion_counts = {}
for emotion_entry in self.emotion_data[result["video_id"]]:
emotion = emotion_entry.get("emotion", "unknown")
emotion_counts[emotion] = emotion_counts.get(emotion, 0) + 1
total_counts = sum(emotion_counts.values())
if total_counts > 0:
report["emotion_distribution"] = {
emotion: (count / total_counts) * 100
for emotion, count in emotion_counts.items()
}
print(f"Using raw emotion counts: {report['emotion_distribution']}")
# Gaze pattern analysis per viewer
for i, result in enumerate(results_list):
viewer_data = {
"viewer_id": f"Viewer {i+1}",
"demographics": result.get("demographics", {}),
"total_view_time": result.get("total_view_time", 0),
"time_on_objects": result.get("total_view_time", 0) - result.get("off_object_time", 0),
"time_off_objects": result.get("off_object_time", 0)
}
# Calculate percentages
if result.get("total_view_time", 0) > 0:
viewer_data["on_objects_percentage"] = ((result.get("total_view_time", 0) - result.get("off_object_time", 0)) / result.get("total_view_time", 0)) * 100
viewer_data["off_objects_percentage"] = (result.get("off_object_time", 0) / result.get("total_view_time", 0)) * 100
# Add object attention breakdown
viewer_data["object_attention"] = {
obj_name: {
"attention_percentage": obj_data["attention_percentage"],
"total_gaze_time": obj_data["total_gaze_time"]
} for obj_name, obj_data in result.get("objects", {}).items()
}
report["gaze_patterns"].append(viewer_data)
# Conclusions and recommendations
if comparison:
# Find most and least engaging objects
objects_by_attention = sorted(
comparison["objects"].items(),
key=lambda x: x[1]["avg_attention_percentage"],
reverse=True
)
if objects_by_attention:
most_engaging_obj, most_engaging_data = objects_by_attention[0]
report["conclusions"]["most_engaging_object"] = {
"name": most_engaging_obj,
"attention_percentage": most_engaging_data["avg_attention_percentage"]
}
if len(objects_by_attention) > 1:
least_engaging_obj, least_engaging_data = objects_by_attention[-1]
report["conclusions"]["least_engaging_object"] = {
"name": least_engaging_obj,
"attention_percentage": least_engaging_data["avg_attention_percentage"]
}
# Emotion insights
if comparison["emotion_distribution"]:
dominant_emotion = max(comparison["emotion_distribution"].items(), key=lambda x: x[1])[0]
report["conclusions"]["dominant_emotion"] = dominant_emotion
# Recommendations based on findings
report["conclusions"]["recommendations"] = [
"Focus on elements similar to the most engaging objects in future content",
f"Consider the {dominant_emotion if 'dominant_emotion' in report['conclusions'] else 'dominant'} emotional response when designing future content"
]
# Attention pattern recommendations
high_variance_objects = [
obj for obj, data in comparison["objects"].items()
if data["attention_variance"] > 100
]
if high_variance_objects:
report["conclusions"]["high_variance_objects"] = high_variance_objects
report["conclusions"]["recommendations"].append(
"The following objects had inconsistent attention across viewers and may need clearer positioning or emphasis: " +
", ".join(high_variance_objects)
)
# Demographic insights
if report["viewer_demographics"].get("gender_distribution") or report["viewer_demographics"].get("age_groups"):
report["conclusions"]["recommendations"].append(
"Consider the demographic breakdown when targeting future content"
)
else:
# Single viewer analysis conclusions
result = results_list[0]
objects_by_attention = sorted(
result["objects"].items(),
key=lambda x: x[1]["attention_percentage"],
reverse=True
)
if objects_by_attention:
most_engaging_obj, most_engaging_data = objects_by_attention[0]
report["conclusions"]["most_engaging_object"] = {
"name": most_engaging_obj,
"attention_percentage": most_engaging_data["attention_percentage"]
}
if len(objects_by_attention) > 1:
least_engaging_obj, least_engaging_data = objects_by_attention[-1]
report["conclusions"]["least_engaging_object"] = {
"name": least_engaging_obj,
"attention_percentage": least_engaging_data["attention_percentage"]
}
# Get dominant emotion
all_emotions = {}
for obj_data in result["objects"].values():
for emotion, time in obj_data.get("emotions", {}).items():
all_emotions[emotion] = all_emotions.get(emotion, 0) + time
if all_emotions:
dominant_emotion = max(all_emotions.items(), key=lambda x: x[1])[0]
report["conclusions"]["dominant_emotion"] = dominant_emotion
# Recommendations
report["conclusions"]["recommendations"] = [
"Consider collecting more viewer data for more robust insights",
f"The current viewer showed most interest in {most_engaging_obj}"
]
else:
# Handle the case where no objects were looked at
report["conclusions"]["recommendations"] = [
"Consider collecting more viewer data for more robust insights",
"Review the video content and gaze data to understand why no objects captured the viewer's attention"
]
# Generate a text report for backward compatibility (storing in raw_text_report field)
report_buffer = StringIO()
report_buffer.write("=" * 80 + "\n")
report_buffer.write("GAZE AND OBJECT DETECTION ANALYSIS REPORT\n")
report_buffer.write("=" * 80 + "\n")
report_buffer.write(f"Generated on: {report['report_metadata']['generated_on']}\n")
report_buffer.write(f"Video ID: {report['report_metadata']['video_id']}\n")
report_buffer.write(f"Number of viewers analyzed: {report['report_metadata']['num_viewers_analyzed']}\n\n")
# Add more text formatting as needed for backward compatibility
report["raw_text_report"] = report_buffer.getvalue()
return report
def load_object_detection_data_from_mongo(self, ad_doc):
"""
Load object detection (ad) data from a MongoDB ad document.
"""
video_id = ad_doc.get("_id", "unknown")
# Use provided video dimensions if available, or fall back to defaults.
self.video_dimensions[video_id] = {
"width": ad_doc.get("video_width", 1920),
"height": ad_doc.get("video_height", 1080)
}
self.object_data[video_id] = {
"frames": ad_doc.get("extracted_objects", []),
"ad_duration": ad_doc.get("ad_duration", 0)
}
print(f"Loaded object detection data for video ID: {video_id} from MongoDB")
return video_id
def load_gaze_emotion_data_from_mongo(self, gaze_doc):
"""
Load gaze and emotion data from a MongoDB gaze_emotion_data document.
"""
ad_id = gaze_doc.get("ad_id", "unknown")
self.gaze_data[ad_id] = gaze_doc.get("gaze_data", [])
self.emotion_data[ad_id] = gaze_doc.get("emotion_data", [])
# If demographic data is present; otherwise use an empty dict.
self.demographic_data[ad_id] = gaze_doc.get("demographic_data", {})
print(f"Loaded gaze and emotion data for ad ID: {ad_id} from MongoDB")
return ad_id
def process_files(object_detection_files, gaze_emotion_files, output_path="report.txt"):
"""
Process multiple files and generate a report.
"""
analyzer = GazeObjectAnalyzer()
if not object_detection_files:
print("No object detection files provided")
return
if not gaze_emotion_files:
print("No gaze and emotion data files provided")
return
video_id = None
for file_path in object_detection_files:
video_id = analyzer.load_object_detection_data(file_path)
if video_id:
break
if not video_id:
print("Failed to load any object detection data")
return
results_list = []
for file_path in gaze_emotion_files:
ad_id = analyzer.load_gaze_emotion_data(file_path)
if ad_id:
# analyzer.match_video_with_gaze_data(video_id, ad_id) # Not strictly needed
result = analyzer.analyze_gaze_on_objects(video_id)
if result:
results_list.append(result)
if not results_list:
print("No analysis results generated")
return
comparison = None
if len(results_list) > 1:
comparison = analyzer.compare_multiple_viewers(results_list)
analyzer.generate_report(results_list, comparison, output_path)
def process_mongo_data(ad_id):
"""Process MongoDB data for the given ad_id and generate metrics report."""
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["adInsight"]
try:
# Fetch the ad document from the 'ads' collection
ad_doc = db.ads.find_one({"_id": ad_id})
if not ad_doc:
print(f"Error: Ad with id {ad_id} not found in database.")
return None
# Fetch all corresponding gaze_emotion_data documents for this ad_id
gaze_docs = list(db.gaze_emotion_data.find({"ad_id": ad_id}))
if not gaze_docs:
# Try with the same ID (sometimes ad_id matches _id)
gaze_docs = list(db.gaze_emotion_data.find({"ad_id": ad_id.split('.')[0]}))
if not gaze_docs:
print(f"Error: No gaze and emotion data found for ad id {ad_id}.")
return None
print(f"Found {len(gaze_docs)} gaze documents for ad_id: {ad_id}")
# Debug emotion data
for i, doc in enumerate(gaze_docs):
emotion_data = doc.get("emotion_data", [])
print(f"Gaze doc #{i+1} has {len(emotion_data)} emotion records")
# Count emotion types
emotions = {}
for e in emotion_data:
emotion = e.get("emotion", "unknown")
emotions[emotion] = emotions.get(emotion, 0) + 1
print(f"Emotion types: {emotions}")
results_list = []
# Process each gaze document as a separate viewer/session
for gaze_doc in gaze_docs:
analyzer = GazeObjectAnalyzer()
video_id = analyzer.load_object_detection_data_from_mongo(ad_doc)
analyzer.load_gaze_emotion_data_from_mongo(gaze_doc)
# Debug loaded emotion data
print(f"Loaded {len(analyzer.emotion_data.get(video_id, []))} emotion records for analysis")
result = analyzer.analyze_gaze_on_objects(video_id)
if result:
results_list.append(result)
print(f"Successfully analyzed gaze data for video ID: {video_id}")
if not results_list:
print(f"Error: No analysis results could be generated for ad id {ad_id}.")
return None
# If more than one session was processed, compare viewers
comparison = None
if len(results_list) > 1:
comparison = analyzer.compare_multiple_viewers(results_list)
print(f"Successfully generated report for ad id {ad_id}.")
report = analyzer.generate_report(results_list, comparison)
# Validate report structure
print(f"Report keys: {list(report.keys())}")
if 'emotion_distribution' in report:
print(f"Emotion distribution: {report['emotion_distribution']}")
if 'object_attention_analysis' in report:
print(f"Object attention analysis: {report['object_attention_analysis']}")
if 'viewer_demographics' in report:
print(f"Viewer demographics: {report['viewer_demographics']}")
if 'gaze_patterns' in report:
print(f"Gaze patterns: {report['gaze_patterns']}")
return report
except Exception as e:
print(f"Error processing data for ad {ad_id}: {str(e)}")
import traceback
traceback.print_exc()
return None