-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
342 lines (274 loc) · 11.2 KB
/
camera.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
Camera Module for Doorbell Camera Operations.
This module provides functionality for interacting with a Reolink doorbell camera,
primarily focused on capturing snapshots. It supports:
Methods:
- API-based snapshot capture
- FFmpeg-based snapshot capture
- Resolution control
- Error handling and logging
The module can capture snapshots using either:
1. Reolink's HTTP API (default, more reliable)
2. FFmpeg (alternative method, useful for some camera models)
Snapshots are saved with timestamps and optional resolution information.
All operations include comprehensive error handling and logging.
"""
import os
import time
import logging
import subprocess
import requests
import random
import string
from datetime import datetime
from pathlib import Path
from typing import Optional, Tuple, Union, Dict, Any
from urllib.parse import urlparse
from . import config
#------------------------------------------------------------------------------
# Constants
#------------------------------------------------------------------------------
# API Configuration
API_ENDPOINT = "/cgi-bin/api.cgi"
RANDOM_STRING_LENGTH = 16
RANDOM_STRING_CHARS = string.ascii_letters + string.digits
# FFmpeg Configuration
FFMPEG_TIMEOUT = 10 # seconds
FFMPEG_TRANSPORT = "tcp"
# Image Configuration
JPEG_MAGIC_BYTES = b'\xFF\xD8\xFF'
SNAPSHOTS_DIR = "snapshots"
# Time Configuration
TIMESTAMP_FORMAT = "%Y%m%d_%H%M%S"
#------------------------------------------------------------------------------
# Logger Configuration
#------------------------------------------------------------------------------
# Configure logger
logger = logging.getLogger('camera')
#------------------------------------------------------------------------------
# Camera Class
#------------------------------------------------------------------------------
class Camera:
"""
Handles doorbell camera operations and snapshot capture.
This class manages:
- Camera connection details
- Snapshot capture methods
- Image saving and validation
- Error handling
Attributes:
ip (str): Camera IP address
username (str): Authentication username
password (str): Authentication password
channel (int): Camera channel number
use_ffmpeg (bool): Whether to use FFmpeg for snapshots
"""
def __init__(self, rtsp_url: str, username: str, password: str,
channel: int = 0, use_ffmpeg: bool = False):
"""
Initialize camera handler.
Args:
rtsp_url: Camera's RTSP URL
username: Authentication username
password: Authentication password
channel: Camera channel number (default: 0)
use_ffmpeg: Whether to use FFmpeg for snapshots (default: False)
Raises:
ValueError: If required parameters are invalid
"""
parsed_url = urlparse(rtsp_url)
if not parsed_url.hostname:
raise ValueError("Invalid RTSP URL")
self.ip = parsed_url.hostname
self.username = username
self.password = password
self.channel = channel
self.use_ffmpeg = use_ffmpeg
# Ensure snapshots directory exists
os.makedirs(SNAPSHOTS_DIR, exist_ok=True)
def _generate_rs(self) -> str:
"""
Generate a random string for API requests.
Returns:
Random string for request identification
"""
return ''.join(random.choices(
RANDOM_STRING_CHARS,
k=RANDOM_STRING_LENGTH
))
def _generate_filename(self, resolution: Optional[Union[str, Tuple[int, int]]] = None) -> str:
"""
Generate snapshot filename with timestamp.
Args:
resolution: Optional resolution specification
Returns:
Generated filename with path
"""
timestamp = datetime.now().strftime(TIMESTAMP_FORMAT)
if resolution:
if isinstance(resolution, tuple):
res_str = f"{resolution[0]}x{resolution[1]}"
else:
res_str = resolution
return f"{SNAPSHOTS_DIR}/snapshot_{timestamp}_{res_str}.jpg"
return f"{SNAPSHOTS_DIR}/snapshot_{timestamp}.jpg"
def _take_snapshot_api(self, resolution: Optional[Tuple[int, int]] = None) -> str:
"""
Take snapshot using Reolink API method.
Args:
resolution: Optional tuple of (width, height)
Returns:
Path to saved snapshot or error message
Raises:
requests.RequestException: If API request fails
ValueError: If received data is invalid
"""
try:
# Prepare API request
params: Dict[str, Any] = {
'cmd': 'Snap',
'channel': self.channel,
'rs': self._generate_rs(),
'user': self.username,
'password': self.password
}
if resolution:
params['width'], params['height'] = resolution
url = f"http://{self.ip}{API_ENDPOINT}"
# Make API request
response = requests.get(url, params=params)
response.raise_for_status()
# Validate image data
if not response.content.startswith(JPEG_MAGIC_BYTES):
raise ValueError("Received data is not a valid JPEG image")
# Generate and save file
filename = self._generate_filename(resolution)
with open(filename, 'wb') as f:
f.write(response.content)
logger.info(f"Saved snapshot to {filename}")
return filename
except Exception as e:
error_msg = f"Error taking snapshot via API: {str(e)}"
logger.error(error_msg)
return error_msg
def _take_snapshot_ffmpeg(self, resolution: Optional[str] = None) -> str:
"""
Take snapshot using FFmpeg method.
Args:
resolution: Optional string in format 'WIDTHxHEIGHT'
Returns:
Path to saved snapshot or error message
Raises:
subprocess.SubprocessError: If FFmpeg command fails
subprocess.TimeoutExpired: If command times out
"""
try:
filename = self._generate_filename(resolution)
# Build FFmpeg command
command = [
'ffmpeg',
'-rtsp_transport', FFMPEG_TRANSPORT,
'-i', f"rtsp://{self.username}:{self.password}@{self.ip}/h264Preview_01_main",
'-frames:v', '1'
]
# Add scale filter if resolution specified
if resolution:
command.extend(['-vf', f'scale={resolution.replace("x", ":")}'])
command.append(filename)
# Execute FFmpeg
result = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=FFMPEG_TIMEOUT
)
if result.returncode == 0:
logger.info(f"Saved snapshot to {filename}")
return filename
else:
error = result.stderr.decode()
error_msg = f"FFmpeg error: {error}"
logger.error(error_msg)
return error_msg
except subprocess.TimeoutExpired:
error_msg = "Error: Snapshot timed out"
logger.error(error_msg)
return error_msg
except Exception as e:
error_msg = f"Error taking snapshot via FFmpeg: {str(e)}"
logger.error(error_msg)
return error_msg
def take_snapshot(self, resolution: Optional[Union[str, Tuple[int, int]]] = None) -> str:
"""
Take snapshot using configured method.
Args:
resolution: Optional resolution specification
For FFmpeg: string 'WIDTHxHEIGHT'
For API: tuple (width, height)
If None, uses default resolution
Returns:
Path to saved snapshot or error message
Raises:
ValueError: If resolution format is invalid
Exception: For other errors during snapshot capture
"""
try:
logger.info(f"Taking snapshot with resolution: {resolution}")
if self.use_ffmpeg:
# Ensure resolution is in string format for FFmpeg
if isinstance(resolution, tuple):
resolution = f"{resolution[0]}x{resolution[1]}"
return self._take_snapshot_ffmpeg(resolution)
else:
# Ensure resolution is in tuple format for API
if isinstance(resolution, str):
width, height = map(int, resolution.split('x'))
resolution = (width, height)
elif resolution is None:
resolution = config.SNAPSHOT_RESOLUTION
return self._take_snapshot_api(resolution)
except Exception as e:
error_msg = f"Error in take_snapshot: {str(e)}"
logger.error(error_msg)
return error_msg
#------------------------------------------------------------------------------
# Module-level interface
#------------------------------------------------------------------------------
_default_camera = None
def get_camera() -> Optional[Camera]:
"""
Get or create the default camera instance if camera features are enabled.
Returns:
Camera instance if DOORBELL or VISION features are enabled, None otherwise
"""
global _default_camera
if not (config.FEATURES['DOORBELL'] or config.FEATURES['VISION']):
return None
if _default_camera is None:
_default_camera = Camera(
rtsp_url=config.DOORBELL_URL,
username=config.DOORBELL_USERNAME,
password=config.DOORBELL_PASSWORD,
channel=config.SNAPSHOT_CHANNEL,
use_ffmpeg=config.USE_FFMPEG_SNAPSHOT
)
return _default_camera
def take_snapshot(resolution: Optional[Union[str, Tuple[int, int]]] = None) -> str:
"""
Take snapshot using default camera instance if available.
This is a convenience function that uses the default camera
configuration from the config module.
Args:
resolution: Optional resolution specification
For FFmpeg: string 'WIDTHxHEIGHT'
For API: tuple (width, height)
If None, uses default resolution
Returns:
Path to saved snapshot or error message
Raises:
RuntimeError: If camera features are not enabled
"""
camera = get_camera()
if camera is None:
raise RuntimeError("Camera features are not enabled. Enable DOORBELL or VISION in config.py")
return camera.take_snapshot(resolution)