python-zed-实时获取图片脚本-Windows和Ubuntu教程

8 篇文章 0 订阅
7 篇文章 0 订阅

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()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hehedadaq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>