|
| 1 | +# SinricPro Camera Example |
| 2 | + |
| 3 | +A comprehensive example demonstrating how to integrate a smart camera with SinricPro, supporting snapshot capture, motion detection, and power control. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Power Control**: Turn camera on/off via voice or app |
| 8 | +- **Snapshot Capture**: Capture and upload images on request |
| 9 | +- **Motion Detection**: Send alerts when motion is detected |
| 10 | +- **Push Notifications**: Alert users of camera events |
| 11 | +- **Settings Control**: Configure resolution, night mode, sensitivity |
| 12 | + |
| 13 | +## Hardware Options |
| 14 | + |
| 15 | +### Raspberry Pi |
| 16 | +```python |
| 17 | +# Install: pip install picamera2 |
| 18 | +from picamera2 import Picamera2 |
| 19 | +camera = Picamera2() |
| 20 | +camera.start() |
| 21 | +image_data = camera.capture_file("snapshot.jpg") |
| 22 | +``` |
| 23 | + |
| 24 | +### ESP32-CAM |
| 25 | +- Built-in OV2640 camera module |
| 26 | +- Motion detection via frame comparison |
| 27 | +- Upload images via HTTP/HTTPS |
| 28 | + |
| 29 | +### OpenCV |
| 30 | +```python |
| 31 | +# Install: pip install opencv-python |
| 32 | +import cv2 |
| 33 | +cap = cv2.VideoCapture(0) |
| 34 | +ret, frame = cap.read() |
| 35 | +_, image_data = cv2.imencode('.jpg', frame) |
| 36 | +``` |
| 37 | + |
| 38 | +## Installation |
| 39 | + |
| 40 | +1. Install dependencies: |
| 41 | +```bash |
| 42 | +pip install sinricpro opencv-python # or picamera2 for Raspberry Pi |
| 43 | +``` |
| 44 | + |
| 45 | +2. Configure your device: |
| 46 | + - Create a Camera device in [SinricPro Portal](https://portal.sinric.pro) |
| 47 | + - Copy your Device ID, App Key, and App Secret |
| 48 | + - Update the constants in `camera_example.py` |
| 49 | + |
| 50 | +3. Run the example: |
| 51 | +```bash |
| 52 | +python camera_example.py |
| 53 | +``` |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +### Power Control |
| 58 | +```python |
| 59 | +async def on_power_state(state: bool) -> bool: |
| 60 | + if state: |
| 61 | + # Turn on camera, start streaming |
| 62 | + camera.start() |
| 63 | + else: |
| 64 | + # Turn off camera, stop streaming |
| 65 | + camera.stop() |
| 66 | + return True |
| 67 | + |
| 68 | +camera.on_power_state(on_power_state) |
| 69 | +``` |
| 70 | + |
| 71 | +### Snapshot Capture |
| 72 | +```python |
| 73 | +async def on_snapshot(device_id: str) -> bool: |
| 74 | + # Capture image from camera |
| 75 | + image_data = capture_image() |
| 76 | + |
| 77 | + # Upload to SinricPro |
| 78 | + await camera.send_snapshot(image_data) |
| 79 | + return True |
| 80 | + |
| 81 | +camera.on_snapshot(on_snapshot) |
| 82 | +``` |
| 83 | + |
| 84 | +### Motion Detection |
| 85 | +```python |
| 86 | +# Send motion event with optional video data |
| 87 | +async def detect_motion(): |
| 88 | + if motion_detected(): |
| 89 | + # Simple event |
| 90 | + await camera.send_motion_event() |
| 91 | + |
| 92 | + # Or with video data |
| 93 | + video_data = capture_motion_video() |
| 94 | + await camera.send_motion_event(motion_data=video_data) |
| 95 | +``` |
| 96 | + |
| 97 | +### Settings Control |
| 98 | +```python |
| 99 | +async def on_setting(setting: str, value: Any) -> bool: |
| 100 | + if setting == "resolution": |
| 101 | + camera.set_resolution(value) # "720p", "1080p", "4K" |
| 102 | + elif setting == "night_mode": |
| 103 | + camera.set_night_mode(value) # True/False |
| 104 | + elif setting == "motion_sensitivity": |
| 105 | + camera.set_sensitivity(value) # 0-100 |
| 106 | + return True |
| 107 | + |
| 108 | +camera.on_setting(on_setting) |
| 109 | +``` |
| 110 | + |
| 111 | +## Complete Example: Raspberry Pi Camera |
| 112 | + |
| 113 | +```python |
| 114 | +import asyncio |
| 115 | +from picamera2 import Picamera2 |
| 116 | +from sinricpro import SinricPro, SinricProCamera, SinricProConfig |
| 117 | + |
| 118 | +# Initialize camera |
| 119 | +pi_camera = Picamera2() |
| 120 | +config = pi_camera.create_still_configuration() |
| 121 | +pi_camera.configure(config) |
| 122 | + |
| 123 | +async def on_snapshot(device_id: str) -> bool: |
| 124 | + try: |
| 125 | + # Capture snapshot |
| 126 | + pi_camera.start() |
| 127 | + pi_camera.capture_file("snapshot.jpg") |
| 128 | + pi_camera.stop() |
| 129 | + |
| 130 | + # Read image data |
| 131 | + with open("snapshot.jpg", "rb") as f: |
| 132 | + image_data = f.read() |
| 133 | + |
| 134 | + # Upload to SinricPro |
| 135 | + camera = SinricPro.get_instance().get_device(device_id) |
| 136 | + await camera.send_snapshot(image_data) |
| 137 | + return True |
| 138 | + except Exception as e: |
| 139 | + print(f"Error: {e}") |
| 140 | + return False |
| 141 | + |
| 142 | +# Setup camera device |
| 143 | +camera = SinricProCamera("YOUR_DEVICE_ID") |
| 144 | +camera.on_snapshot(on_snapshot) |
| 145 | +``` |
| 146 | + |
| 147 | +## Complete Example: OpenCV Motion Detection |
| 148 | + |
| 149 | +```python |
| 150 | +import asyncio |
| 151 | +import cv2 |
| 152 | +import numpy as np |
| 153 | +from sinricpro import SinricPro, SinricProCamera |
| 154 | + |
| 155 | +async def motion_detection_loop(camera: SinricProCamera): |
| 156 | + cap = cv2.VideoCapture(0) |
| 157 | + prev_frame = None |
| 158 | + |
| 159 | + while True: |
| 160 | + ret, frame = cap.read() |
| 161 | + if not ret: |
| 162 | + continue |
| 163 | + |
| 164 | + # Convert to grayscale and blur |
| 165 | + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| 166 | + gray = cv2.GaussianBlur(gray, (21, 21), 0) |
| 167 | + |
| 168 | + if prev_frame is None: |
| 169 | + prev_frame = gray |
| 170 | + continue |
| 171 | + |
| 172 | + # Compute difference |
| 173 | + frame_delta = cv2.absdiff(prev_frame, gray) |
| 174 | + thresh = cv2.threshold(frame_delta, 25, 255, cv2.THRESH_BINARY)[1] |
| 175 | + |
| 176 | + # Check for motion |
| 177 | + if np.sum(thresh) > 10000: # Threshold for motion |
| 178 | + print("Motion detected!") |
| 179 | + |
| 180 | + # Capture snapshot |
| 181 | + _, image_data = cv2.imencode('.jpg', frame) |
| 182 | + |
| 183 | + # Send motion event |
| 184 | + await camera.send_motion_event() |
| 185 | + |
| 186 | + # Wait before next detection |
| 187 | + await asyncio.sleep(30) |
| 188 | + |
| 189 | + prev_frame = gray |
| 190 | + await asyncio.sleep(0.1) |
| 191 | + |
| 192 | + cap.release() |
| 193 | +``` |
| 194 | + |
| 195 | +## Voice Commands |
| 196 | + |
| 197 | +Once configured, you can control your camera with: |
| 198 | + |
| 199 | +- **Alexa**: "Alexa, turn on the camera" |
| 200 | +- **Alexa**: "Alexa, turn off the camera" |
| 201 | +- **Google Home**: "Hey Google, turn on the camera" |
| 202 | +- **App**: Request snapshots, view motion events |
| 203 | + |
| 204 | +## Advanced Features |
| 205 | + |
| 206 | +### Night Mode |
| 207 | +```python |
| 208 | +async def on_setting(setting: str, value: Any) -> bool: |
| 209 | + if setting == "night_mode": |
| 210 | + if value: |
| 211 | + # Enable IR LEDs |
| 212 | + enable_ir_leds() |
| 213 | + else: |
| 214 | + # Disable IR LEDs |
| 215 | + disable_ir_leds() |
| 216 | + return True |
| 217 | +``` |
| 218 | + |
| 219 | +### Recording Modes |
| 220 | +```python |
| 221 | +recording_mode = "motion" # or "continuous", "scheduled" |
| 222 | + |
| 223 | +async def on_setting(setting: str, value: Any) -> bool: |
| 224 | + if setting == "recording_mode": |
| 225 | + global recording_mode |
| 226 | + recording_mode = value |
| 227 | + configure_recording(value) |
| 228 | + return True |
| 229 | +``` |
| 230 | + |
| 231 | +### Privacy Mode |
| 232 | +```python |
| 233 | +privacy_mode = False |
| 234 | + |
| 235 | +async def on_power_state(state: bool) -> bool: |
| 236 | + global privacy_mode |
| 237 | + if not state: |
| 238 | + # Camera off - enable privacy mode |
| 239 | + privacy_mode = True |
| 240 | + disable_camera() |
| 241 | + cover_lens() # Physical privacy shutter |
| 242 | + else: |
| 243 | + privacy_mode = False |
| 244 | + enable_camera() |
| 245 | + uncover_lens() |
| 246 | + return True |
| 247 | +``` |
| 248 | + |
| 249 | +## Troubleshooting |
| 250 | + |
| 251 | +### Camera Not Capturing |
| 252 | +- Verify camera is connected and accessible |
| 253 | +- Check camera permissions |
| 254 | +- Test camera independently first |
| 255 | + |
| 256 | +### Motion Events Not Sending |
| 257 | +- Check rate limiting (max 1 event per 60 seconds by default) |
| 258 | +- Verify camera is powered on |
| 259 | +- Check network connectivity |
| 260 | + |
| 261 | +### Snapshots Not Uploading |
| 262 | +- Verify image format (JPEG recommended) |
| 263 | +- Check image size (< 5MB recommended) |
| 264 | +- Ensure proper authentication |
| 265 | + |
| 266 | +## Related Examples |
| 267 | + |
| 268 | +- [Motion Sensor](../motionsensor/) - PIR motion detection |
| 269 | +- [Doorbell](../doorbell/) - Doorbell with button press |
| 270 | +- [Switch](../switch/) - Basic power control |
| 271 | + |
| 272 | +## Additional Resources |
| 273 | + |
| 274 | +- [SinricPro Documentation](https://sinricpro.github.io/python-sdk/) |
| 275 | +- [Camera API Reference](https://sinricpro.github.io/python-sdk/capabilities/#cameracontroller) |
| 276 | +- [OpenCV Documentation](https://docs.opencv.org/) |
| 277 | +- [Raspberry Pi Camera](https://www.raspberrypi.com/documentation/computers/camera_software.html) |
0 commit comments