-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_with_webcam.py
More file actions
64 lines (49 loc) · 1.87 KB
/
run_with_webcam.py
File metadata and controls
64 lines (49 loc) · 1.87 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
"""
Run the object detection app specifically with webcam.
This script tries different camera indices to find an available camera.
"""
import cv2
from main import ObjectDetectionApp
from camera import OpenCVCamera
from detection import ContourDetector
from color import HSVColorAnalyzer
from config import ConfigManager
def find_available_camera():
"""Find an available camera by trying different indices."""
print("🔍 Searching for available cameras...")
for camera_index in range(5): # Try camera indices 0-4
print(f" Trying camera index {camera_index}...")
cap = cv2.VideoCapture(camera_index)
if cap.isOpened():
ret, frame = cap.read()
if ret and frame is not None:
print(f"✅ Found working camera at index {camera_index}")
cap.release()
return camera_index
cap.release()
print("❌ No working camera found")
return None
def main():
"""Main function to run the object detection app with webcam."""
print("🎯 Object Detection with Webcam")
print("=" * 50)
# Find available camera
camera_index = find_available_camera()
if camera_index is None:
print("\n⚠️ No camera detected!")
print("Options:")
print("1. Connect a webcam and try again")
print("2. Run 'python demo.py' to see the system work with test images")
return
print(f"\n🎬 Starting object detection with camera {camera_index}")
print("Controls:")
print(" - Press 'q' to quit")
print(" - Press ESC to quit")
# Create custom camera with the found index
camera = OpenCVCamera(camera_index=camera_index, width=640, height=480)
# Create app with custom camera
app = ObjectDetectionApp(camera=camera)
# Run the app
app.run()
if __name__ == "__main__":
main()