-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_camera_test.py
More file actions
215 lines (174 loc) · 6.85 KB
/
simple_camera_test.py
File metadata and controls
215 lines (174 loc) · 6.85 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
#!/usr/bin/env python3
"""
Simple Camera Test - Standalone version
Tests camera access and hand tracking without Flask
"""
import cv2
from cvzone.HandTrackingModule import HandDetector
import time
import numpy as np # Added missing import for np
def test_camera():
"""Test basic camera functionality"""
print("🎥 Testing Camera Access...")
# Try to open camera
camera = cv2.VideoCapture(0)
if not camera.isOpened():
print("❌ Could not open camera at index 0")
# Try other camera indices
for i in range(1, 5):
print(f"🔄 Trying camera index {i}...")
camera = cv2.VideoCapture(i)
if camera.isOpened():
print(f"✅ Camera opened at index {i}")
break
if not camera.isOpened():
print("❌ No camera found at any index")
return False
# Get camera properties
width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = camera.get(cv2.CAP_PROP_FPS)
print(f"📊 Camera Properties:")
print(f" Resolution: {width}x{height}")
print(f" FPS: {fps}")
# Test reading a frame
ret, frame = camera.read()
if ret:
print("✅ Successfully read frame from camera")
else:
print("❌ Failed to read frame from camera")
camera.release()
return False
camera.release()
return True
def test_hand_tracking():
"""Test hand tracking functionality"""
print("\n🖐️ Testing Hand Tracking...")
try:
# Initialize hand detector
hand_detector = HandDetector(
maxHands=1,
detectionCon=0.8,
minTrackCon=0.5
)
print("✅ Hand detector initialized successfully")
# Test with a sample image (black image)
sample_image = cv2.imread("sample.jpg") if cv2.imread("sample.jpg") is not None else np.zeros((480, 640, 3), dtype=np.uint8)
# Test hand detection
hands, processed_image = hand_detector.findHands(sample_image)
print(f"✅ Hand detection test passed - Found {len(hands)} hands")
return True
except Exception as e:
print(f"❌ Hand tracking test failed: {e}")
return False
def interactive_camera_test():
"""Interactive camera test with real-time hand tracking"""
print("\n🎮 Starting Interactive Camera Test...")
print("📱 Press 'q' to quit, 's' to save image")
# Initialize camera
camera = cv2.VideoCapture(0)
if not camera.isOpened():
print("❌ Could not open camera for interactive test")
return False
# Initialize hand detector
hand_detector = HandDetector(
maxHands=1,
detectionCon=0.8,
minTrackCon=0.5
)
# Set camera properties
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
camera.set(cv2.CAP_PROP_FPS, 30)
print("✅ Camera and hand detector ready!")
print("🖐️ Show your hand to the camera...")
frame_count = 0
start_time = time.time()
while True:
ret, frame = camera.read()
if not ret:
print("❌ Failed to read frame")
break
# Flip frame horizontally for mirror effect
frame = cv2.flip(frame, 1)
# Find hands
hands, frame = hand_detector.findHands(frame, draw=True)
# Process hand data
if hands:
hand = hands[0]
landmarks = hand["lmList"]
# Get finger positions
fingers = hand_detector.fingersUp(hand)
finger_count = sum(fingers)
# Display information
cv2.putText(frame, f"Hand: {hand['type']}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.putText(frame, f"Fingers: {finger_count}", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.putText(frame, f"Pattern: {fingers}", (10, 90),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# Draw hand landmarks
for lm in landmarks:
cv2.circle(frame, (lm[0], lm[1]), 5, (255, 0, 255), cv2.FILLED)
else:
cv2.putText(frame, "No hand detected", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# Display frame info
cv2.putText(frame, f"Frame: {frame_count}", (10, frame.shape[0] - 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# Calculate FPS
frame_count += 1
elapsed_time = time.time() - start_time
if elapsed_time > 0:
fps = frame_count / elapsed_time
cv2.putText(frame, f"FPS: {fps:.1f}", (10, frame.shape[0] - 40),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# Display instructions
cv2.putText(frame, "Press 'q' to quit, 's' to save", (10, frame.shape[0] - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# Show frame
cv2.imshow("Camera Test - Hand Tracking", frame)
# Handle key presses
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
print("👋 Quitting interactive test...")
break
elif key == ord('s'):
filename = f"hand_test_{int(time.time())}.jpg"
cv2.imwrite(filename, frame)
print(f"💾 Saved image as {filename}")
# Cleanup
camera.release()
cv2.destroyAllWindows()
return True
def main():
"""Main function"""
print("🤟 Sign Language Learning - Camera Test")
print("=" * 50)
# Test 1: Basic camera access
if not test_camera():
print("\n❌ Camera test failed. Please check:")
print(" - Camera is connected and working")
print(" - No other application is using the camera")
print(" - Camera drivers are installed")
return
# Test 2: Hand tracking
if not test_hand_tracking():
print("\n❌ Hand tracking test failed. Please check:")
print(" - MediaPipe is properly installed")
print(" - OpenCV version is compatible")
return
print("\n✅ All tests passed!")
# Ask user if they want interactive test
try:
response = input("\n🎮 Do you want to run interactive camera test? (y/n): ").lower()
if response == 'y':
interactive_camera_test()
else:
print("👋 Camera test completed successfully!")
except KeyboardInterrupt:
print("\n👋 Test interrupted by user")
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
if __name__ == "__main__":
main()