-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_aabb.py
More file actions
167 lines (141 loc) · 5.04 KB
/
test_aabb.py
File metadata and controls
167 lines (141 loc) · 5.04 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
#!/usr/bin/env python3
"""
Quick test script for AABB occupancy detection algorithm
Tests various scenarios to validate the logic
"""
def shrink_bbox(x1, y1, x2, y2, scale=0.8):
"""Shrink bounding box by scale factor"""
width = x2 - x1
height = y2 - y1
center_x = x1 + width / 2
center_y = y1 + height / 2
new_width = width * scale
new_height = height * scale
new_x1 = center_x - new_width / 2
new_y1 = center_y - new_height / 2
new_x2 = center_x + new_width / 2
new_y2 = center_y + new_height / 2
return (new_x1, new_y1, new_x2, new_y2)
def calculate_intersection_area(box1, box2):
"""Calculate intersection area between two bounding boxes"""
x1_1, y1_1, x2_1, y2_1 = box1
x1_2, y1_2, x2_2, y2_2 = box2
x1_i = max(x1_1, x1_2)
y1_i = max(y1_1, y1_2)
x2_i = min(x2_1, x2_2)
y2_i = min(y2_1, y2_2)
if x2_i < x1_i or y2_i < y1_i:
return 0.0
intersection_area = (x2_i - x1_i) * (y2_i - y1_i)
return intersection_area
def test_scenario(name, person_bbox, chair_bboxes, expected_chair_idx):
"""Test a specific scenario"""
print(f"\n{'='*60}")
print(f"Test: {name}")
print(f"{'='*60}")
# Shrink person bbox
person_shrunken = shrink_bbox(*person_bbox, scale=0.8)
print(f"Person (original): {person_bbox}")
print(f"Person (80% shrink): {[f'{x:.1f}' for x in person_shrunken]}")
# Test against each chair
max_intersection = 0.0
best_chair_idx = None
for idx, chair_bbox in enumerate(chair_bboxes):
chair_shrunken = shrink_bbox(*chair_bbox, scale=0.8)
intersection = calculate_intersection_area(person_shrunken, chair_shrunken)
print(f"\nChair {idx+1} (original): {chair_bbox}")
print(f"Chair {idx+1} (80% shrink): {[f'{x:.1f}' for x in chair_shrunken]}")
print(f" -> Intersection area: {intersection:.1f} pixels^2")
if intersection > max_intersection:
max_intersection = intersection
best_chair_idx = idx
# Result
print(f"\n{'-'*60}")
if max_intersection > 0:
print(f"[OK] Result: Chair {best_chair_idx + 1} is occupied (area: {max_intersection:.1f} px^2)")
else:
print(f"[X] Result: No chair occupied (no intersection)")
# Validation
if expected_chair_idx is None:
success = best_chair_idx is None
else:
success = best_chair_idx == expected_chair_idx
status = "[PASS]" if success else "[FAIL]"
print(f"{status} - Expected: {expected_chair_idx}, Got: {best_chair_idx}")
return success
# Run test scenarios
def main():
print("AABB Occupancy Detection Algorithm - Test Suite")
print("=" * 60)
results = []
# Test 1: Single person sitting on single chair
results.append(test_scenario(
"Single Person, Single Chair (Sitting)",
person_bbox=(100, 100, 200, 300),
chair_bboxes=[(90, 90, 210, 310)],
expected_chair_idx=0
))
# Test 2: Person between two chairs (closer to right)
results.append(test_scenario(
"Person Between Two Chairs",
person_bbox=(150, 100, 250, 300),
chair_bboxes=[
(50, 90, 180, 310), # Chair 1 (left)
(200, 90, 330, 310) # Chair 2 (right)
],
expected_chair_idx=1 # Should pick Chair 2
))
# Test 3: Person sitting on left chair of two
results.append(test_scenario(
"Person on Left Chair",
person_bbox=(50, 100, 150, 300),
chair_bboxes=[
(40, 90, 160, 310), # Chair 1 (left)
(200, 90, 320, 310) # Chair 2 (right)
],
expected_chair_idx=0 # Should pick Chair 1
))
# Test 4: Person not sitting (walking past)
results.append(test_scenario(
"Person Walking Past Chairs",
person_bbox=(400, 100, 500, 300),
chair_bboxes=[
(50, 100, 150, 300),
(180, 100, 280, 300)
],
expected_chair_idx=None # No chair occupied
))
# Test 5: Person overlapping 3 chairs (middle should win)
results.append(test_scenario(
"Person Overlapping 3 Chairs",
person_bbox=(150, 100, 250, 300),
chair_bboxes=[
(50, 90, 170, 310), # Chair 1
(140, 90, 260, 310), # Chair 2 (max overlap)
(220, 90, 340, 310) # Chair 3
],
expected_chair_idx=1 # Should pick Chair 2
))
# Test 6: Small overlap (person leaning)
results.append(test_scenario(
"Person Leaning on Chair",
person_bbox=(100, 50, 200, 200),
chair_bboxes=[(100, 150, 200, 350)],
expected_chair_idx=0 # Should still detect
))
# Summary
print(f"\n{'='*60}")
print("TEST SUMMARY")
print(f"{'='*60}")
passed = sum(results)
total = len(results)
print(f"Passed: {passed}/{total}")
print(f"Failed: {total - passed}/{total}")
if passed == total:
print("\n[SUCCESS] All tests passed!")
else:
print(f"\n[FAILED] {total - passed} test(s) failed")
return passed == total
if __name__ == "__main__":
success = main()
exit(0 if success else 1)