my code
import sys
import pyzed.sl as sl
import cv2
import numpy as np
import subprocess as sp
import argparse
def ZedVideoCaptureAndPushRTSP(rtsp_url, fps=30, resolution=sl.RESOLUTION.HD1080):
init_params = sl.InitParameters()
init_params.camera_fps = fps
init_params.depth_mode = sl.DEPTH_MODE.NONE
init_params.camera_resolution = resolution
zed_camera = sl.Camera()
status = zed_camera.open(init_params)
if status != sl.ERROR_CODE.SUCCESS:
print(f"Failed to open camera: {repr(status)}")
sys.exit(1)
zed_image = sl.Mat()
# FFmpeg命令,用于推送到RTSP服务器
ffmpeg_cmd = [
'ffmpeg',
'-y',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24', # 假设ZED SDK输出BGR格式
'-s', '1920x1080',
'-r', str(fps),
'-i', '-',
'-tune', 'zerolatency',
'-preset', 'ultrafast',
'-g', '1', # 关键帧间隔设置为1,可能需要根据实际情况调整
'-c:v', 'h264_nvmpi',
'-pix_fmt', 'yuv420p',
'-f', 'rtsp',
rtsp_url
]
ffmpeg_process = sp.Popen(ffmpeg_cmd, stdin=sp.PIPE, stdout=sp.DEVNULL, stderr=sp.PIPE)
try:
while True:
status = zed_camera.grab()
if status == sl.ERROR_CODE.SUCCESS:
zed_camera.retrieve_image(zed_image, sl.VIEW.SIDE_BY_SIDE) # 假设我们只使用左目图像
zed_image_data = np.array(zed_image.get_data(), dtype=np.uint8)
zed_image_data = cv2.cvtColor(zed_image_data, cv2.COLOR_BGR2RGB)
zed_image_data = cv2.resize(zed_image_data, (
1920, 1080), interpolation=cv2.INTER_AREA)
zed_image_data = zed_image_data.tobytes()
# 写入FFmpeg进程
ffmpeg_process.stdin.write(zed_image_data)
ffmpeg_process.stdin.flush() # 确保数据尽快发送到FFmpeg
else:
print(f"Failed to grab frame: {repr(status)}")
break
finally:
zed_camera.close()
ffmpeg_process.stdin.close()
ffmpeg_process.wait()
# cv2.destroyAllWindows() # 如果不再显示视频帧,可以移除这行代码
# 解析命令行参数
if __name__ == '__main__':
resolution = sl.RESOLUTION.HD1080
ZedVideoCaptureAndPushRTSP('rtsp://192.168.1.27/live/test_stream', 30, resolution)
# gst-launch-1.0 playbin uri=rtsp://192.168.1.27/live/test_stream ! videoconvert ! autovideosink
my error
[2024-10-14 16:51:49 UTC][ZED][INFO] Logging level INFO
[2024-10-14 16:51:50 UTC][ZED][WARNING] CAMERA NOT DETECTED in sl::ERROR_CODE sl::Camera::open(sl::InitParameters)
Failed to open camera: CAMERA NOT DETECTED
[2024-10-14 16:51:51 UTC][ZED][ERROR] [ZED] sl::Camera::Open has not been called, no Camera instance running.
[2024-10-14 16:51:51 UTC][ZED][ERROR] [ZED] sl::Camera::Open has not been called, no Camera instance running.
dmesg_1.log (10.7 KB)
dmesg_1.log (10.7 KB)
dmesg.log (101.3 KB)