fix: use gpu

This commit is contained in:
ziesorx 2025-09-25 22:59:55 +07:00
parent 5f29392c2f
commit 6bb679f4d8
5 changed files with 533 additions and 84 deletions

View file

@ -199,23 +199,63 @@ class RTSPReader:
except Exception as e:
logger.debug(f"Camera {self.camera_id}: GStreamer NVDEC not available: {e}")
# Method 2: Try FFMPEG with NVIDIA CUVID hardware decoder
# Method 2: Try OpenCV CUDA VideoReader (if built with CUVID support)
if not hw_accel_success:
try:
import os
# Set FFMPEG to use NVIDIA CUVID decoder
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'video_codec;h264_cuvid|rtsp_transport;tcp|hwaccel;cuda'
# Check if OpenCV was built with CUDA codec support
build_info = cv2.getBuildInformation()
if 'cudacodec' in build_info or 'CUVID' in build_info:
logger.info(f"Attempting OpenCV CUDA VideoReader for camera {self.camera_id}")
# Use OpenCV's CUDA backend
self.cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG, [
cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY
])
if self.cap.isOpened():
hw_accel_success = True
logger.info(f"Camera {self.camera_id}: Using OpenCV CUDA hardware acceleration")
else:
logger.debug(f"Camera {self.camera_id}: OpenCV not built with CUDA codec support")
except Exception as e:
logger.debug(f"Camera {self.camera_id}: OpenCV CUDA not available: {e}")
# Method 3: Try FFMPEG with optimal hardware acceleration (CUVID/VAAPI)
if not hw_accel_success:
try:
from core.utils.ffmpeg_detector import get_optimal_rtsp_options
import os
# Get optimal FFmpeg options based on detected capabilities
optimal_options = get_optimal_rtsp_options(self.rtsp_url)
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = optimal_options
logger.info(f"Attempting FFMPEG with detected hardware acceleration for camera {self.camera_id}")
logger.debug(f"Camera {self.camera_id}: Using FFmpeg options: {optimal_options}")
logger.info(f"Attempting FFMPEG with h264_cuvid for camera {self.camera_id}")
self.cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG)
if self.cap.isOpened():
hw_accel_success = True
logger.info(f"Camera {self.camera_id}: Using FFMPEG with CUVID hardware acceleration")
# Try to get backend info to confirm hardware acceleration
backend = self.cap.getBackendName()
logger.info(f"Camera {self.camera_id}: Using FFMPEG hardware acceleration (backend: {backend})")
except Exception as e:
logger.debug(f"Camera {self.camera_id}: FFMPEG CUVID not available: {e}")
logger.debug(f"Camera {self.camera_id}: FFMPEG hardware acceleration not available: {e}")
# Method 3: Try VAAPI hardware acceleration (for Intel/AMD GPUs)
# Fallback to basic CUVID
try:
import os
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'video_codec;h264_cuvid|rtsp_transport;tcp|hwaccel;cuda'
self.cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG)
if self.cap.isOpened():
hw_accel_success = True
logger.info(f"Camera {self.camera_id}: Using basic FFMPEG CUVID hardware acceleration")
except Exception as e2:
logger.debug(f"Camera {self.camera_id}: Basic CUVID also failed: {e2}")
# Method 4: Try VAAPI hardware acceleration (for Intel/AMD GPUs)
if not hw_accel_success:
try:
gst_pipeline = (