-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_system.py
More file actions
205 lines (153 loc) · 6.35 KB
/
test_system.py
File metadata and controls
205 lines (153 loc) · 6.35 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
"""
Test script for the object detection system.
This script provides basic testing functionality to verify components work correctly.
"""
import numpy as np
import cv2
from camera import OpenCVCamera
from detection import ContourDetector
from color import HSVColorAnalyzer
from models import BoundingBox, DetectedObject
from config import ConfigManager
def test_camera():
"""Test camera functionality."""
print("Testing camera...")
camera = OpenCVCamera()
if not camera.initialize():
print("❌ Camera initialization failed")
return False
print("✅ Camera initialized successfully")
frame = camera.capture_frame()
if frame is None:
print("❌ Frame capture failed")
camera.release()
return False
print(f"✅ Frame captured successfully: {frame.shape}")
resolution = camera.get_resolution()
print(f"✅ Camera resolution: {resolution}")
camera.release()
return True
def test_detector():
"""Test object detector."""
print("\nTesting object detector...")
detector = ContourDetector()
# Create a test image with simple shapes
test_image = np.zeros((400, 600, 3), dtype=np.uint8)
# Add some shapes
cv2.rectangle(test_image, (50, 50), (150, 150), (255, 255, 255), -1)
cv2.circle(test_image, (300, 200), 50, (255, 255, 255), -1)
cv2.rectangle(test_image, (450, 100), (550, 200), (255, 255, 255), -1)
objects = detector.detect_objects(test_image)
print(f"✅ Detected {len(objects)} objects")
for i, obj in enumerate(objects):
print(f" Object {i+1}: Area={obj.area}, Confidence={obj.confidence:.2f}")
return len(objects) > 0
def test_color_analyzer():
"""Test color analyzer."""
print("\nTesting color analyzer...")
analyzer = HSVColorAnalyzer()
# Create test images with different colors
test_colors = [
((255, 0, 0), "red"),
((0, 255, 0), "green"),
((0, 0, 255), "blue"),
((255, 255, 0), "yellow")
]
for (r, g, b), expected_color in test_colors:
# Create test image
test_image = np.full((100, 100, 3), (b, g, r), dtype=np.uint8) # BGR for OpenCV
# Create mock detected object
bbox = BoundingBox(10, 10, 80, 80)
contour = np.array([[10, 10], [90, 10], [90, 90], [10, 90]])
detected_object = DetectedObject(bbox, contour)
# Analyze color
color = analyzer.analyze_color(test_image, detected_object)
print(f" Expected: {expected_color}, Detected: {color.name} (confidence: {color.confidence:.2f})")
supported_colors = analyzer.get_supported_colors()
print(f"✅ Color analyzer supports {len(supported_colors)} colors: {supported_colors}")
return True
def test_config_manager():
"""Test configuration manager."""
print("\nTesting configuration manager...")
config_manager = ConfigManager()
try:
detection_config = config_manager.get_detection_config()
print("✅ Detection config loaded successfully")
color_config = config_manager.get_color_config()
print("✅ Color config loaded successfully")
# Test specific config access
camera_width = config_manager.get_config("detection_config", "camera.width")
print(f"✅ Camera width from config: {camera_width}")
return True
except Exception as e:
print(f"❌ Config manager test failed: {e}")
return False
def run_visual_test():
"""Run a visual test with live camera feed."""
print("\nRunning visual test (press 'q' to quit)...")
camera = OpenCVCamera()
detector = ContourDetector()
color_analyzer = HSVColorAnalyzer()
if not camera.initialize():
print("❌ Cannot initialize camera for visual test")
return
cv2.namedWindow("Visual Test", cv2.WINDOW_AUTOSIZE)
try:
while True:
frame = camera.capture_frame()
if frame is None:
continue
# Detect objects
objects = detector.detect_objects(frame)
# Analyze colors and visualize
for obj in objects:
obj.color = color_analyzer.analyze_color(frame, obj)
# Draw bounding box
bbox = obj.bounding_box
cv2.rectangle(frame, (bbox.x, bbox.y),
(bbox.x + bbox.width, bbox.y + bbox.height),
(0, 255, 0), 2)
# Draw color label
if obj.color:
label = f"{obj.color.name} ({obj.color.confidence:.2f})"
cv2.putText(frame, label, (bbox.x, bbox.y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
# Show object count
cv2.putText(frame, f"Objects: {len(objects)}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.imshow("Visual Test", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except KeyboardInterrupt:
print("Visual test interrupted")
finally:
camera.release()
cv2.destroyAllWindows()
def main():
"""Run all tests."""
print("🧪 Object Detection System Test Suite")
print("=" * 50)
tests = [
("Camera", test_camera),
("Object Detector", test_detector),
("Color Analyzer", test_color_analyzer),
("Configuration Manager", test_config_manager)
]
passed = 0
for test_name, test_func in tests:
try:
if test_func():
passed += 1
except Exception as e:
print(f"❌ {test_name} test failed with exception: {e}")
print(f"\n📊 Test Results: {passed}/{len(tests)} tests passed")
if passed == len(tests):
print("🎉 All tests passed! System is ready to use.")
# Ask user if they want to run visual test
response = input("\nWould you like to run the visual test? (y/n): ")
if response.lower() in ['y', 'yes']:
run_visual_test()
else:
print("⚠️ Some tests failed. Please check the system configuration.")
if __name__ == "__main__":
main()