Giter Club home page Giter Club logo

pyzed's Introduction

import sys import numpy as np import pyzed.sl as sl import cv2

help_string = "[s] Save side by side image [d] Save Depth, [n] Change Depth format, [p] Save Point Cloud, [m] Change Point Cloud format, [q] Quit" prefix_point_cloud = "Cloud_" prefix_depth = "Depth_" path = "./"

count_save = 0 mode_point_cloud = 0 mode_depth = 0 point_cloud_format_ext = ".ply" depth_format_ext = ".png"

def point_cloud_format_name(): global mode_point_cloud if mode_point_cloud > 3: mode_point_cloud = 0 switcher = { 0: ".xyz", 1: ".pcd", 2: ".ply", 3: ".vtk", } return switcher.get(mode_point_cloud, "nothing")

def depth_format_name(): global mode_depth if mode_depth > 2: mode_depth = 0 switcher = { 0: ".png", 1: ".pfm", 2: ".pgm", } return switcher.get(mode_depth, "nothing")

def save_point_cloud(zed, filename) : print("Saving Point Cloud...") tmp = sl.Mat() zed.retrieve_measure(tmp, sl.MEASURE.XYZRGBA) saved = (tmp.write(filename + point_cloud_format_ext) == sl.ERROR_CODE.SUCCESS) if saved : print("Done") else : print("Failed... Please check that you have permissions to write on disk")

def save_depth(zed, filename) : print("Saving Depth Map...") tmp = sl.Mat() zed.retrieve_measure(tmp, sl.MEASURE.DEPTH) saved = (tmp.write(filename + depth_format_ext) == sl.ERROR_CODE.SUCCESS) if saved : print("Done") else : print("Failed... Please check that you have permissions to write on disk")

def save_sbs_image(zed, filename) :

image_sl_left = sl.Mat()
zed.retrieve_image(image_sl_left, sl.VIEW.LEFT)
image_cv_left = image_sl_left.get_data()

image_sl_right = sl.Mat()
zed.retrieve_image(image_sl_right, sl.VIEW.RIGHT)
image_cv_right = image_sl_right.get_data()

sbs_image = np.concatenate((image_cv_left, image_cv_right), axis=1)

cv2.imwrite(filename, sbs_image)

def process_key_event(zed, key) : global mode_depth global mode_point_cloud global count_save global depth_format_ext global point_cloud_format_ext

if key == 100 or key == 68:
    save_depth(zed, path + prefix_depth + str(count_save))
    count_save += 1
elif key == 110 or key == 78:
    mode_depth += 1
    depth_format_ext = depth_format_name()
    print("Depth format: ", depth_format_ext)
elif key == 112 or key == 80:
    save_point_cloud(zed, path + prefix_point_cloud + str(count_save))
    count_save += 1
elif key == 109 or key == 77:
    mode_point_cloud += 1
    point_cloud_format_ext = point_cloud_format_name()
    print("Point Cloud format: ", point_cloud_format_ext)
elif key == 104 or key == 72:
    print(help_string)
elif key == 115:
    save_sbs_image(zed, "ZED_image" + str(count_save) + ".png")
    count_save += 1
else:
    a = 0

def print_help() : print(" Press 's' to save Side by side images") print(" Press 'p' to save Point Cloud") print(" Press 'd' to save Depth image") print(" Press 'm' to switch Point Cloud format") print(" Press 'n' to switch Depth format")

def main() :

# Create a ZED camera object
zed = sl.Camera()

# Set configuration parameters
input_type = sl.InputType()
if len(sys.argv) >= 2 :
    input_type.set_from_svo_file(sys.argv[1])
init = sl.InitParameters(input_t=input_type)
init.camera_resolution = sl.RESOLUTION.HD1080
init.depth_mode = sl.DEPTH_MODE.PERFORMANCE
init.coordinate_units = sl.UNIT.MILLIMETER

# Open the camera
err = zed.open(init)
if err != sl.ERROR_CODE.SUCCESS :
    print(repr(err))
    zed.close()
    exit(1)

# Display help in console
print_help()

# Set runtime parameters after opening the camera
runtime = sl.RuntimeParameters()
runtime.sensing_mode = sl.SENSING_MODE.STANDARD

# Prepare new image size to retrieve half-resolution images
image_size = zed.get_camera_information().camera_resolution
image_size.width = image_size.width /2
image_size.height = image_size.height /2

# Declare your sl.Mat matrices
image_zed = sl.Mat(image_size.width, image_size.height, sl.MAT_TYPE.U8_C4)
depth_image_zed = sl.Mat(image_size.width, image_size.height, sl.MAT_TYPE.U8_C4)
point_cloud = sl.Mat()

key = ' '
while key != 113 :
    err = zed.grab(runtime)
    if err == sl.ERROR_CODE.SUCCESS :
        # Retrieve the left image, depth image in the half-resolution
        zed.retrieve_image(image_zed, sl.VIEW.LEFT, sl.MEM.CPU, image_size)
        zed.retrieve_image(depth_image_zed, sl.VIEW.DEPTH, sl.MEM.CPU, image_size)
        # Retrieve the RGBA point cloud in half resolution
        zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA, sl.MEM.CPU, image_size)

        # To recover data from sl.Mat to use it with opencv, use the get_data() method
        # It returns a numpy array that can be used as a matrix with opencv
        image_ocv = image_zed.get_data()
        depth_image_ocv = depth_image_zed.get_data()

        cv2.imshow("Image", image_ocv)
        cv2.imshow("Depth", depth_image_ocv)

        key = cv2.waitKey(10)

        process_key_event(zed, key)

cv2.destroyAllWindows()
zed.close()

print("\nFINISH")

if name == "main": main()

pyzed's People

Contributors

betulbayram avatar

Stargazers

Derya Babul avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.