python-zed-实时获取图片脚本-Windows和Ubuntu教程
文章目录
前言:
官网有自己的教程,稍微整理一下;
Windows安装pyzed包教程:
1.安装zed_sdk
https://www.stereolabs.com/docs/app-development/python/install/
一定要找到对应的cuda版本!
2. 安装Python包,
进入目录
C:\Program Files (x86)\ZED SDK.
执行脚本
python get_python_api.py
3.会下载一个文件
pyzed-3.1-cp37-cp37m-windows_x86_64.whl
进入对应虚拟环境,用pip安装
pip install pyzed-3.1-cp37-cp37m-windows_x86_64.whl
Linux版本安装
Linux:
1. install ZED SDK (see Installation section)找到对应的cuda版本!
2. 安装Python包,进入路径
cd “/usr/local/zed/”
执行脚本
python get_python_api.py
3.会下载一个文件pyzed-3.1-cp37-cp37m-linux_x86_64.whl
进入对应虚拟环境,用pip安装
pip install pyzed-3.1-cp37-cp37m-linux_x86_64.whl
实时获取图片脚本
import pyzed.sl as sl
import numpy as np
class Zed_compute:
def __init__(self,
stream_flag=False,
resolution=1080,
left_flag=True,
fps=30,
):
self.point_a = None
if stream_flag:
zed = sl.Camera()
init_params = sl.InitParameters()
init_params.depth_mode = sl.DEPTH_MODE.NONE
init_params.coordinate_units = sl.UNIT.MILLIMETER
if resolution==1080:
init_params.camera_resolution = sl.RESOLUTION.HD1080
elif resolution==720:
init_params.camera_resolution = sl.RESOLUTION.HD720
elif resolution==1920:
init_params.camera_resolution = sl.RESOLUTION.HD1920
else:
init_params.camera_resolution = sl.RESOLUTION.HD1080
init_params.depth_minimum_distance = 0.3
init_params.camera_fps = fps
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
print(repr(err))
exit(-1)
self.lower_zed = zed
def zed_open(self,):
"""
open zed camera
"""
# Create a ZED camera object
zed = sl.Camera()
# Set configuration parameters(get video with depth)
init_params = sl.InitParameters()
init_params.depth_mode = sl.DEPTH_MODE.ULTRA # Use ULTRA depth mode
init_params.coordinate_units = sl.UNIT.MILLIMETER # Use millimeter units (for depth)
init_params.camera_resolution = sl.RESOLUTION.HD1080
init_params.depth_minimum_distance = 0.3
init_params.camera_fps = 10
# init_params.enable_right_side_measure = True
# Open the camera
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
exit(-1)
return zed
def get_cur_img(self,):
"""
return current image
"""
zed = self.zed_open()
image = sl.Mat()
point_cloud = sl.Mat()
runtime_parameters = sl.RuntimeParameters()
# while
runtime_parameters.sensing_mode = sl.SENSING_MODE.STANDARD
# runtime_parameters.sensing_mode = sl.SENSING_MODE.SENSING_MODE_FILL
if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
# A new image and depth is available if grab() returns SUCCESS
zed.retrieve_image(image, sl.VIEW.LEFT) # Retrieve left image
zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA) # A 3D point Cloud
# get current image of RGB(1080 * 1920)
cur_image = image.get_data()[:, :, :3]
# Get the 3D point cloud values
point3d = point_cloud.get_data()
return point3d, cur_image
def get_lower_img(self,):
"""
return current image
"""
zed = self.lower_zed
runtime_parameters = sl.RuntimeParameters()
image = sl.Mat()
# point_cloud = sl.Mat()
# while
runtime_parameters.sensing_mode = sl.SENSING_MODE.STANDARD
# runtime_parameters.sensing_mode = sl.SENSING_MODE.SENSING_MODE_FILL
if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
# A new image and depth is available if grab() returns SUCCESS
zed.retrieve_image(image, sl.VIEW.LEFT,) # Retrieve left image
# zed.retrieve_image(image, view=sl.VIEW.VIEW_LEFT, type=sl.MEM.MEM_GPU) # Retrieve left image
# zed.retrieve_measure(point_cloud, sl.MEASURE.MEASURE_XYZRGBA) # A 3D point Cloud
# get current image of RGB(1080 * 1920)
try:
cur_image = image.get_data()[:, :, :3]
except:
return np.zeros((720, 1280, 3), dtype='uint8')
return cur_image
def main():
zed_camera = Zed_compute()
import cv2
while True:
point3d, cur_image = zed_camera.get_cur_img()
cv2.imshow("img", cur_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()