You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Camera needs to be started and stopped explicitly:
31
24
32
25
```python
33
-
# V4L/USB cameras
34
-
usb_camera = Camera(0) # Camera index
35
-
usb_camera = Camera("1") # Index as string
36
-
usb_camera = Camera("/dev/video0") # Device path
37
-
38
-
# IP cameras
39
-
ip_camera = Camera("rtsp://192.168.1.100/stream")
40
-
ip_camera = Camera("http://camera.local/mjpeg",
41
-
username="admin", password="secret")
42
-
43
-
# WebSocket cameras
44
-
-`"ws://localhost:8080"`- WebSocket server URL (extracts host and port)
45
-
-`"localhost:9090"`- WebSocket server host:port format
46
-
```
47
-
48
-
## API Reference
49
-
50
-
### Camera Class
26
+
# Specify camera and configuration
27
+
camera = Camera(0, resolution=(640, 480), fps=15)
28
+
camera.start()
51
29
52
-
The main `Camera` class acts as a factory that creates the appropriate camera implementation:
30
+
image =camera.capture()
53
31
54
-
```python
55
-
camera = Camera(source, **options)
32
+
camera.stop()
56
33
```
57
34
58
-
**Parameters:**
59
-
-`source`: Camera source identifier
60
-
-`int`: V4L camera index (0, 1, 2...)
61
-
-`str`: Camera index, device path, or URL
62
-
-`resolution`: Tuple `(width, height)` or `None` for default
63
-
-`fps`: Target frames per second (default: 10)
64
-
-`transformer`: Pipeline of transformers that adjust the captured image
65
-
66
-
**Methods:**
67
-
-`start()`: Initialize and start camera
68
-
-`stop()`: Stop camera and release resources
69
-
-`capture()`: Capture frame as Numpy array
70
-
-`is_started()`: Check if camera is running
71
-
72
-
### Context Manager
73
-
35
+
Or you can leverage context support for doing that automatically:
74
36
```python
75
37
with Camera(source, **options) as camera:
76
38
frame = camera.capture()
39
+
if frame isnotNone:
40
+
print(f"Captured frame with shape: {frame.shape}")
77
41
# Camera automatically stopped when exiting
78
42
```
79
43
80
44
## Camera Types
45
+
The Camera class provides automatic camera type detection based on the format of its source argument. keyword arguments will be propagated to the underlying implementation.
81
46
82
-
### V4L/USB Cameras
47
+
Note: constructor arguments (except source) must be provided in keyword format to forward them correctly to the specific camera implementations.
83
48
84
-
For local USB cameras and V4L-compatible devices:
49
+
The underlying camera implementations can be instantiated explicitly (V4LCamera, IPCamera and WebSocketCamera), if needed.
85
50
86
-
```python
87
-
camera = Camera(0, resolution=(1280, 720), fps=30)
88
-
```
51
+
### V4L Cameras
52
+
For local USB cameras and V4L-compatible devices.
89
53
90
54
**Features:**
91
-
- Device enumeration via `/dev/v4l/by-id/`
92
-
- Resolution validation
93
-
- Backend information
94
-
95
-
### IP Cameras
96
-
97
-
For network cameras supporting RTSP or HTTP streams:
55
+
- Supports cameras compatible with the Video4Linux2 drivers
98
56
99
57
```python
100
-
camera = Camera("rtsp://admin:pass@192.168.1.100/stream",
101
-
timeout=10, fps=5)
58
+
camera = Camera(0) # Camera index
59
+
camera = Camera("/dev/video0") # Device path
60
+
camera = V4LCamera(0)
102
61
```
103
62
63
+
### IP Cameras
64
+
For network cameras supporting RTSP (Real-Time Streaming Protocol) and HLS (HTTP Live Streaming).
65
+
104
66
**Features:**
105
-
- RTSP, HTTP, HTTPS protocols
67
+
-Supports capturing RTSP, HLS streams
106
68
- Authentication support
107
-
- Connection testing
108
69
- Automatic reconnection
109
70
110
-
### WebSocket Cameras
111
-
112
-
For hosting a WebSocket server that receives frames from clients (single client only):
113
-
114
71
```python
115
-
camera = Camera("ws://0.0.0.0:9090", frame_format="json")
72
+
camera = Camera("rtsp://admin:secret@192.168.1.100/stream")
73
+
camera = Camera("http://camera.local/stream",
74
+
username="admin", password="secret")
75
+
camera = IPCamera("http://camera.local/stream",
76
+
username="admin", password="secret")
116
77
```
117
78
79
+
### WebSocket Cameras
80
+
For hosting a WebSocket server that receives frames from a single client at a time.
81
+
118
82
**Features:**
119
-
- Hosts WebSocket server (not client)
120
83
-**Single client limitation**: Only one client can connect at a time
121
-
- Additional clients are rejected with error message
122
-
- Receives frames from connected client
84
+
- Stream data from any client with WebSockets support
123
85
- Base64, binary, and JSON frame formats
124
-
- Frame buffering and queue management
125
-
- Bidirectional communication with connected client
126
-
127
-
**Client Connection:**
128
-
Only one client can connect at a time. Additional clients receive an error:
from arduino.app_peripherals.camera.camera import CameraFactory
175
-
176
-
# Create camera directly via factory
177
-
camera = CameraFactory.create_camera(
178
-
source="ws://localhost:8080/stream",
179
-
frame_format="json"
180
-
)
181
-
```
182
-
183
-
## Dependencies
184
-
185
-
### Core Dependencies
186
-
-`opencv-python` (cv2) - Image processing and V4L/IP camera support
187
-
-`Pillow` (PIL) - Image format handling
188
-
-`requests` - HTTP camera connectivity testing
189
-
190
-
### Optional Dependencies
191
-
-`websockets` - WebSocket server support (install with `pip install websockets`)
192
-
193
-
## Examples
194
-
195
-
See the `examples/` directory for comprehensive usage examples:
196
-
- Basic camera operations
197
-
- Different camera types
198
-
- Advanced configuration
199
-
- Error handling
200
-
- Context managers
201
-
202
119
## Migration from Legacy Camera
203
120
204
-
The new Camera abstraction is backward compatible with the existing Camera implementation. Existing code using the old API will continue to work, but new code should use the improved abstraction for better flexibility and features.
121
+
The new Camera abstraction is backward compatible with the existing Camera implementation. Existing code using the old API will continue to work, but will use the new Camera backend. New code should use the improved abstraction for better flexibility and features.
0 commit comments