gpt4 book ai didi

python - 如何从 realsense L515 记录深度流

转载 作者:行者123 更新时间:2023-12-04 23:01:14 27 4
gpt4 key购买 nike

我有一个深度相机(Intel Realsense L515 ),我喜欢录制深度视频。
我看过这个answer正在使用 FFMPEG ,但我不知道如何在我的情况下复制它!
我正在使用这段代码:

import cv2
import numpy as np
import pyrealsense2 as rs
import time

pipeline = rs.pipeline()
config = rs.config()

"""
# Depth Mode
"""
# Resolution
res = [(1024, 768), (640, 480), (320, 240)]
resolution = res[0]
print("RealSense Resolution:{}\n".format(resolution))

# # initialize video writer
fourcc = cv2.VideoWriter_fourcc('F','F','V','1')
fps = 30
video_filename = 'output.avi'
out = cv2.VideoWriter(video_filename, fourcc, fps, resolution, False)


config.enable_stream(rs.stream.depth, resolution[0], resolution[1], rs.format.z16, 30)
profile = config.resolve(pipeline)
# Start streaming
pipeline.start(config)

# Declare sensor object and set options
depth_sensor = profile.get_device().first_depth_sensor()
depth_sensor.set_option(rs.option.visual_preset, 5) # 5 is short range, 3 is low ambient light
depth_sensor.set_option(rs.option.receiver_gain, 8)
depth_sensor.set_option(rs.option.pre_processing_sharpening, 0.0)
depth_sensor.set_option(rs.option.post_processing_sharpening, 3.0)
depth_sensor.set_option(rs.option.laser_power, 100)
depth_sensor.set_option(rs.option.confidence_threshold, 2)
# Get the sensor once at the beginning. (Sensor index: 1)

# # Filters
threshold_filter = rs.threshold_filter(min_dist=1.2, max_dist=1.4)
temporal_filter = rs.temporal_filter(smooth_alpha=0.1, smooth_delta = 9.0,persistence_control=7)

try:
# # Filters
threshold_filter = rs.threshold_filter(min_dist=1.2, max_dist=1.4)
temporal_filter = rs.temporal_filter(smooth_alpha=0.1, smooth_delta = 75.0,persistence_control=0)

tic = time.time()

while True:
# Wait for depth frames:
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
if not depth_frame:
continue

#------------
# # FILTERS |
#------------

depth_frame = threshold_filter.process(depth_frame)
depth_frame = temporal_filter.process(depth_frame)

# Convert images to numpy arrays
depth_array = np.asanyarray(depth_frame.get_data())
# depth_array = np.asanyarray(colorizer.colorize(depth_frame).get_data())

out.write(depth_array)
toc = time.time()
if(round(toc - tic) > 30):
break

finally:
out.release()
pipeline.stop()
并得到这个错误:

out.write(depth_array)cv2.error: OpenCV(4.5.4) /tmp/pip-req-build-kneyjnox/opencv/modules/videoio/src/cap_ffmpeg.cpp:186: error: (-215:Assertion failed) image.depth() == CV_8U in function 'write'


你能告诉我如何记录相机的深度吗?提前致谢。

最佳答案

不知道能不能指定pix_fmt使用 opencv 的 ffmpeg 子模块,但这里是使用我的 ffmpegio 的替代方法包 (GitHub)

pip install ffmpegio
import ffmpegio
import numpy as np

fps = 30
video_filename = "sandbox/output.avi"
shape = (640, 480)
with ffmpegio.open(
video_filename,
"wv", # mode of operation: video write
fps,
c="ffv1", # c is sufficient for video-only, o.w. `**{'c:v': 'ffv1'}`
overwrite=True, # False (or remove) to error out if output file exists
show_log=True, # False (or remove) to stop showing FFmpeg log on console
) as out:

# ...

while True:

# ...

out.write(depth_array) # first write will configure video format:
# dtype=np.uint16 -> pix_fmt='gray16le'
# shape=(h,w) -> s=f'{w}x{h}

# ...

注意:ffmpeg 要求 ffmpeg 二进制文件在系统上可用且可发现(与 opencv 不同)。
您也可以使用普通的 python 子进程来实现相同的功能,并预先提供额外的信息:

import subprocess as sp

ffmpeg_path = 'ffmpeg'

fps = 30
video_filename = 'output.avi'
width = 640
height = 480

out = sp.Popen([ffmpeg_path, '-y',
'-f','rawvideo','-r',str(fps),'-pix_fmt','gray16le','-s',f'{width}x{height}','-',
'-f','avi','-c:v','ffv1',video_filename],stdin=sp.PIPE):

try:
...
while True:
...
depth_array = np.asanyarray(depth_frame.get_data())

out.stdin.write(depth_array) # you may need to wrap with memory

...

finally:
out.stdin.close()
out.wait()
...

关于python - 如何从 realsense L515 记录深度流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71951959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com