Skip to content

Commit

Permalink
Create Mono and Ensenso Camera Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
clin-nexera committed Jun 8, 2022
0 parents commit 2d35409
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
107 changes: 107 additions & 0 deletions EnsensoCamera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import argparse
import cv2 as cv

import nxlib.api as api
from nxlib import NxLibCommand, NxLibException, NxLibItem
from nxlib.constants import *

class EnsensoCamera():

# camera_serial_str = "216866" camera near the window
# camera_serial_str = "216865" camera near the door
def __init__(self, serial= "216866"):
self.camera_serial = serial
self.is_camera_init_done = False
self.depth_image_f32_3d = None
self.texture_image_u8_4d = None
self.rendered_point_map_width = 1024
self.rendered_point_map_height = 1280


def open_camera(self):

if self.is_camera_init_done == False:
try:
# Wait for the cameras to be initialized
api.initialize()

# Open the camera with the given serial
with NxLibCommand(CMD_OPEN) as cmd:
cmd.parameters()[ITM_CAMERAS] = self.camera_serial
cmd.execute()

self.is_camera_init_done = True
except NxLibException as e:
print(f"An NxLib error occured: Error Text: {e.get_error_text()}")
except Exception:
print("An NxLib unrelated error occured:\n")
raise


def close_camera(self):
if self.is_camera_init_done == True:
try:
# Closes all open cameras
with NxLibCommand(CMD_CLOSE) as cmd:
cmd.execute()

self.is_camera_init_done = False
except NxLibException as e:
print(f"An NxLib error occured: Error Text: {e.get_error_text()}")
except Exception:
print("An NxLib unrelated error occured:\n")
raise


def capture(self):
if self.is_camera_init_done == True:
# Captures with the previous openend camera
with NxLibCommand(CMD_CAPTURE) as cmd:
cmd.parameters()[ITM_CAMERAS] = self.camera_serial
cmd.execute()

# Rectify the the captures raw images
with NxLibCommand(CMD_RECTIFY_IMAGES) as cmd:
cmd.execute()

# Compute the disparity map
with NxLibCommand(CMD_COMPUTE_DISPARITY_MAP) as cmd:
cmd.execute()

# get camera frame position/orientation relative to the robot frame
rotation_angle = NxLibItem()[ITM_CAMERAS][self.camera_serial][ITM_LINK][ITM_ROTATION][ITM_ANGLE].as_double()

rotation_axis_0 = NxLibItem()[ITM_CAMERAS][self.camera_serial][ITM_LINK][ITM_ROTATION][ITM_AXIS][0].as_double()
rotation_axis_1 = NxLibItem()[ITM_CAMERAS][self.camera_serial][ITM_LINK][ITM_ROTATION][ITM_AXIS][1].as_double()
rotation_axis_2 = NxLibItem()[ITM_CAMERAS][self.camera_serial][ITM_LINK][ITM_ROTATION][ITM_AXIS][2].as_double()

traslation_0 = NxLibItem()[ITM_CAMERAS][self.camera_serial][ITM_LINK][ITM_TRANSLATION][0].as_double()
traslation_1 = NxLibItem()[ITM_CAMERAS][self.camera_serial][ITM_LINK][ITM_TRANSLATION][1].as_double()
traslation_2 = NxLibItem()[ITM_CAMERAS][self.camera_serial][ITM_LINK][ITM_TRANSLATION][2].as_double()

# Generate orthographic projection 2D depth and texture images
rend_point_map = NxLibCommand(CMD_RENDER_POINT_MAP)
rend_point_map.parameters()[ITM_SIZE][0] = self.rendered_point_map_width # set width of the images, overriding the default value
rend_point_map.parameters()[ITM_SIZE][1] = self.rendered_point_map_height # height of the images, overriding the default value
rend_point_map.parameters()[ITM_FILL_XY_COORDINATES] = True
rend_point_map.parameters()[ITM_VIEW_POSE][ITM_TRANSLATION][0] = traslation_1 # shift target point to camera
rend_point_map.parameters()[ITM_VIEW_POSE][ITM_TRANSLATION][1] = traslation_0
rend_point_map.parameters()[ITM_VIEW_POSE][ITM_TRANSLATION][2] = 0.0
rend_point_map.execute()
self.depth_image_f32_3d = NxLibItem()[ITM_IMAGES][ITM_RENDER_POINT_MAP].get_binary_data()
self.texture_image_u8_4d = NxLibItem()[ITM_IMAGES][ITM_RENDER_POINT_MAP_TEXTURE].get_binary_data()


def save_texture_img(self, filename = "texture_img.png"):
print("here 2")
if self.texture_image_u8_4d is not None:
print("here")
cv.imwrite(filename, self.texture_image_u8_4d)


if __name__ == "__main__":
camera = EnsensoCamera()
camera.open_camera()
camera.capture()
camera.save_texture_img()
camera.close_camera()
45 changes: 45 additions & 0 deletions MonoCamera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import argparse

from nxlib.context import NxLib, MonoCamera
from nxlib.command import NxLibCommand
from nxlib.constants import *

class MonoCam():

def __init__(self, serial):
self.camera_serial = serial

def capture_raw(self, filename="raw.png"):
with NxLib(), MonoCamera("4104140356") as camera:
camera.capture()
save_image(filename, camera[ITM_IMAGES][ITM_RAW])

def capture_rectified(self, filename="rectified.png"):
with NxLib(), MonoCamera(self.camera_serial) as camera:
camera.capture()
camera.rectify()
save_image(filename, camera[ITM_IMAGES][ITM_RECTIFIED])


def save_image(filename, item):
with NxLibCommand(CMD_SAVE_IMAGE) as cmd:
cmd.parameters()[ITM_NODE] = item.path
cmd.parameters()[ITM_FILENAME] = filename
cmd.execute()


if __name__ == "__main__":

parser = argparse.ArgumentParser()
parser.add_argument("serial", type=str,
help="the serial of the stereo camera to open")
args = parser.parse_args()

camera_serial = args.serial
# instantiate camera
mono_camera = MonoCam(camera_serial)
# capture and save a raw image
mono_camera.capture_raw("raw_img.png")
# capture and save a rectified image
mono_camera.capture_rectified("rectified_img.png")

24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Classes For Interacting With IDS Cameras

## Requirements

1. Install the Ensenso SDK: https://www.optonic.com/en/support/download/ensenso-sdk/
2. Install the UEye Driver (maybe): https://www.optonic.com/en/support/download/ensenso-sdk/
3. pip install -r requirements.txt
- pip install nxlib
- pip install gpib_ctypes
4. Copy and paste everything into the constant.py file into the nxlib/constants.py file

## Serial Numbers
- RGB By Window: "4104140356"
- Ensenso By Window: "216866"
- Ensenso By Door: "216865"


## Classes

### EnsensoCamera
- open_camera : open camera
- close_camera : close camera
- capture : capture depth and texture images
- save_texture_img(filename="texture_img.png") : save the texture image
Binary file added requirements.txt
Binary file not shown.

0 comments on commit 2d35409

Please sign in to comment.