Esempio n. 1
0
    def createImageBuffer(width=320, height=240, channels=3):
        """ This method creates image buffers with the specified \a width, \a height and number of
            color channels \a channels.

        @param width    - integer specifying the width of the image   (default: 320)
        @param height   - integer specifying the height of the image  (default: 240)
        @param channels - integer specifying number of color channels (default: 3)
        @return image, buffer array
        """

        if channels == 1:
            buf_image = yarp.ImageFloat()
            buf_image.resize(width, height)

            buf_array = np.zeros((height, width), dtype=np.float32)

        else:
            buf_image = yarp.ImageRgb()
            buf_image.resize(width, height)

            buf_array = np.zeros((height, width, channels), dtype=np.uint8)

        buf_image.setExternal(buf_array, buf_array.shape[1],
                              buf_array.shape[0])

        return buf_image, buf_array
    def __init__(self, input_port_name, output_port_name):
        # Prepare ports
        self._input_port = yarp.Port()
        self._input_port_name = input_port_name
        self._input_port.open(self._input_port_name)

        self._output_port = yarp.Port()
        self._output_port_name = output_port_name
        self._output_port.open(self._output_port_name)

        # Prepare image buffers
        # Input
        self._input_buf_image = yarp.ImageRgb()
        self._input_buf_image.resize(320, 240)
        self._input_buf_array = numpy.zeros((240, 320, 3), dtype=numpy.uint8)
        self._input_buf_image.setExternal(
            self._input_buf_array.__array_interface__['data'][0],
            self._input_buf_array.shape[1], self._input_buf_array.shape[0])
        # Output
        self._output_buf_image = yarp.ImageFloat()
        self._output_buf_image.resize(320, 240)
        self._output_buf_array = numpy.zeros((240, 320), dtype=numpy.float32)
        self._output_buf_image.setExternal(
            self._output_buf_array.__array_interface__['data'][0],
            self._output_buf_array.shape[1], self._output_buf_array.shape[0])
Esempio n. 3
0
    def __init__(self, options):

        self.options = options

        # Set requested GPU
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = str(options.gpu_id)

        # Initialize YARP network
        yarp.Network.init()

        # Initialize RF module
        yarp.RFModule.__init__(self)

        # Initialize inference
        self.inference = Inference(self.options.model, self.options.refine_model, self.options.width, self.options.height, self.options.cam_fx, self.options.cam_fy, self.options.cam_cx, self.options.cam_cy)

        # Initialize YARP ports
        self.rgb_in = yarp.BufferedPortImageRgb()
        self.rgb_in.open("/dense-fusion/camera/rgb:i")

        self.depth_in = yarp.BufferedPortImageFloat()
        self.depth_in.open("/dense-fusion/camera/depth:i")

        self.mask_in = yarp.BufferedPortImageMono()
        self.mask_in.open("/dense-fusion/camera/mask:i")

        self.camera_pose_in = yarp.BufferedPortVector()
        self.camera_pose_in.open("/dense-fusion/camera/pose:i")

        self.prediction_out = yarp.Port()
        self.prediction_out.open("/dense-fusion/pose:o")

        # Input buffers initialization
        self.rgb_buffer = bytearray(numpy.zeros((self.options.height, self.options.width, 3), dtype = numpy.uint8))
        self.rgb_image = yarp.ImageRgb()
        self.rgb_image.resize(self.options.width, self.options.height)
        self.rgb_image.setExternal(self.rgb_buffer, self.options.width, self.options.height)

        self.depth_buffer = bytearray(numpy.zeros((self.options.height, self.options.width, 1), dtype = numpy.float32))
        self.depth_image = yarp.ImageFloat()
        self.depth_image.resize(self.options.width, self.options.height)
        self.depth_image.setExternal(self.depth_buffer, self.options.width, self.options.height)

        self.mask_buffer = bytearray(numpy.zeros((self.options.height, self.options.width, 1), dtype = numpy.uint8))
        self.mask_image = yarp.ImageMono()
        self.mask_image.resize(self.options.width, self.options.height)
        self.mask_image.setExternal(self.mask_buffer, self.options.width, self.options.height)

        # Stamp initialization for finite differences
        self.last_stamp = -1
        self.last_total_position = None
Esempio n. 4
0
def yarp_send():
    img_array = numpy.random.uniform(0., 255.,
                                     (240, 320)).astype(numpy.float32)
    yarp_image = yarp.ImageFloat()
    yarp_image.setExternal(img_array.__array_interface__['data'][0],
                           img_array.shape[1], img_array.shape[0])
    output_port = yarp.Port()
    output_port.open("/python-image-port")
    yarp.Network.connect("/python-image-port", "/view01")
    output_port.write(yarp_image)
    # Cleanup
    output_port.close()
    return image
Esempio n. 5
0
    def __init__(self, port_name):
        print("Constructor Begin.")

        # Prepare Port.
        print(" - Port Init", " "*8, ": ", sep="", end="", flush=True)
        self.input_port = yarp.Port()
        self.input_port.open(port_name)

        # Prepare Array Buffer.
        print(" - Image Buffer", " "*5, ": ", sep="", end="\n", flush=False)
        self.yarp_image = yarp.ImageFloat()

        self.matrix = np.zeros((1,1), dtype=np.float32)

        self.fig = plt.figure()
def python_to_yarp():
    # Create the array with random data
    img_array = numpy.random.uniform(0., 255.,
                                     (240, 320)).astype(numpy.float32)

    # Create the yarp image and wrap it around the array
    yarp_image = yarp.ImageFloat()
    yarp_image.setExternal(img_array, img_array.shape[1], img_array.shape[0])

    # Alternatively, if using Python 2.x, try:
    # yarp_image.setExternal(img_array.__array_interface__['data'][0], img_array.shape[1], img_array.shape[0])

    # Create the yarp port, connect it to the running instance of yarpview and send the image
    output_port = yarp.Port()
    output_port.open("/python-image-port")
    yarp.Network.connect("/python-image-port", "/view01")
    output_port.write(yarp_image)

    # Cleanup
    output_port.close()
yarp.Network.init()

if not yarp.Network.checkNetwork():
    print "[error] Please try running yarp server"
    quit()

input_port = yarp.Port()
input_port.open("/python/ecroSim/depthImage:i")
if not yarp.Network.connect("/ecroSim/depthImage:o",
                            "/python/ecroSim/depthImage:i"):
    print "[error] Could not connect"
    quit()

# Just once to get measurements
yarp_tmp_image = yarp.ImageFloat()
input_port.read(yarp_tmp_image)
height = yarp_tmp_image.height()
width = yarp_tmp_image.width()

# Create numpy array to receive the image and the YARP image wrapped around it
img_array = numpy.zeros((height, width), dtype=numpy.float32)
yarp_image = yarp.ImageFloat()
yarp_image.resize(width, height)
yarp_image.setExternal(img_array, img_array.shape[1], img_array.shape[0])
# Read the data from the port into the image
input_port.read(yarp_image)
if yarp_image.getRawImage().__long__(
) <> img_array.__array_interface__['data'][0]:
    print "read() reallocated my yarp_image!"
# display the image that has been read
Esempio n. 8
0
import numpy
import yarp
 
# Initialise YARP
yarp.Network.init()
 
# Create the array with random data
img_array = numpy.random.uniform(0., 255., (240, 320)).astype(numpy.float32)
 
# Create the yarp image and wrap it around the array  
yarp_image = yarp.ImageFloat()
yarp_image.setExternal(img_array, img_array.shape[1], img_array.shape[0])
 
# Create the yarp port, connect it to the running instance of yarpview and send the image
output_port = yarp.Port()
output_port.open("/python-image-port")
yarp.Network.connect("/python-image-port", "/view01")
output_port.write(yarp_image)
 
# Cleanup
output_port.close()