-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_camera.py
More file actions
136 lines (116 loc) · 4.12 KB
/
test_camera.py
File metadata and controls
136 lines (116 loc) · 4.12 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
"""
Quick camera diagnostic script.
Run this to test if FER and webcam are working correctly.
"""
import sys
print("=" * 60)
print("Camera & FER Diagnostic Test")
print("=" * 60)
# Test 1: Check if camera can be opened
print("\n[1/4] Testing webcam access...")
try:
import cv2
cap = cv2.VideoCapture(0)
if cap.isOpened():
print("✓ Webcam opened successfully")
ret, frame = cap.read()
if ret and frame is not None:
print(f"✓ Frame captured: {frame.shape}")
else:
print("✗ Failed to capture frame")
cap.release()
sys.exit(1)
cap.release()
else:
print("✗ Failed to open webcam")
print(" - Check if another app is using the camera")
print(" - On RPi: check 'sudo raspi-config' → Interface Options → Camera")
print(" - On RPi: ensure camera cable is connected properly")
print(" - On Windows: check camera permissions in Settings")
sys.exit(1)
except ImportError:
print("✗ OpenCV not installed")
print(" Run: pip install opencv-python")
sys.exit(1)
# Test 2: Check FER installation
print("\n[2/4] Testing FER installation...")
try:
from fer import FER
print("✓ FER library imported successfully")
except ImportError as e:
print(f"✗ FER import failed: {e}")
print("\nDiagnosing issue...")
# Check if fer package exists
try:
import fer
print(f" - fer package found at: {fer.__file__}")
print(f" - fer version: {fer.__version__ if hasattr(fer, '__version__') else 'unknown'}")
except ImportError:
print(" - fer package not found in Python path")
# Check dependencies
missing = []
for dep in ['tensorflow', 'torch', 'torchvision']:
try:
__import__(dep)
except ImportError:
missing.append(dep)
if missing:
print(f" - Missing dependencies: {', '.join(missing)}")
print("\n Try: pip uninstall fer -y && pip install fer tensorflow")
sys.exit(1)
except Exception as e:
print(f"✗ Unexpected error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
# Test 3: Initialize FER detector
print("\n[3/4] Initializing FER detector...")
try:
detector = FER(mtcnn=False)
print("✓ FER detector initialized")
except Exception as e:
print(f"✗ FER initialization failed: {e}")
sys.exit(1)
# Test 4: Capture and detect emotion
print("\n[4/4] Testing emotion detection...")
print(" Position your face in front of the camera...")
print(" Capturing in 3 seconds...")
import time
time.sleep(3)
try:
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()
if not ret:
print("✗ Failed to capture test frame")
sys.exit(1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = detector.detect_emotions(rgb_frame)
if not result or len(result) == 0:
print("✗ No face detected")
print("\nTroubleshooting:")
print(" - Ensure your face is clearly visible")
print(" - Check lighting (avoid backlighting)")
print(" - Move closer to the camera")
print(" - Make sure camera is not covered")
else:
print(f"✓ Detected {len(result)} face(s)")
emotions = result[0]["emotions"]
print("\nEmotion scores:")
for emotion, score in sorted(emotions.items(), key=lambda x: x[1], reverse=True):
bar = "█" * int(score * 20)
print(f" {emotion:10s} {score:.3f} {bar}")
dominant = max(emotions, key=emotions.get)
confidence = emotions[dominant]
print(f"\n✓ Dominant emotion: {dominant} (confidence: {confidence:.2f})")
if confidence < 0.3:
print(" ⚠️ Low confidence - improve lighting or face visibility")
except Exception as e:
print(f"✗ Emotion detection failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
print("\n" + "=" * 60)
print("✓ All tests passed! Camera is working correctly.")
print("=" * 60)
print("\nYou can now use the main app with CAMERA_ENABLED=true")