Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems in getting the image transported between two ports using .tobytes() with setExternal() method since setExternal() crashes with python 3.6.9 #2474

Open
aliomair080369 opened this issue Feb 1, 2021 · 0 comments
Labels
Component: Bindings swig, python, java, ruby, perl, octave, matlab, lua, csharp, tcl Issue Type: QA/Support This is a generic question or a support request that does not require a patch

Comments

@aliomair080369
Copy link

aliomair080369 commented Feb 1, 2021

Hi all,
I am using Ubuntu 18.04.5 LTS (Bionic Beaver), python v3.6.9, Numpy v1.19.5, Yarp v3.3 and Opencv v4.2.0.

I wrote a script to read an image from the folder using opencv and wrote that image to a port /sender. I wrote another script that listens to the port /sender and reads that image from that port and displays it using plt.imshow().

Since I had faced the similar problems with setExternal() as mentioned in the issue #1873 (Bindings: setExternal crashes for RGB images with python>=3.6), so I tried to follow the tobytes() workaround to transport the image between two ports which was also suggested in the same issue #1873 .

When I send the image from /sender to /reader, the only thing I get back is a black image. I don't know where I made the mistake. Can someone please point out my mistake. I have attached the scripts for sender.py and receiver.py below.

Any help in this regard would be highly appreciated. I have been trying to get around this issue for almost two weeks now and didn't get any help from anywhere so far. So please take some time to at least point me to the right direction.

Thanks in advance.

@andylucny
@BrutusTT
@Nicogene
@drdanz

################################################################################################
Here is the script of sender.py. It reads the image from the folder and opens and writes to /sender port:

import numpy as np
import yarp
import cv2
import matplotlib.pyplot as plt

yarp.Network.init()  #network initializing

image_path = 'bottles_01.jpg'  # image to load from the folder

cap = cv2.VideoCapture(image_path,cv2.CAP_IMAGES) #using cv2.VideoCapture since later I want to read frames from camera
_, frame = cap.read() 

height, width, channels = frame.shape  # height = 230, width = 219, channels = 3

i = yarp.ImageRgb()  # yarp image
i.resize(width,height) # yarp image resizing

a = frame.tobytes()  
i.setExternal(a, 219, 230)

#img = np.frombuffer(a, dtype=frame.dtype).reshape(230,219,3)
#plt.imshow(img)
#plt.show()

output_port = yarp.Port() # initializing the port
output_port.open("/sender") # opening the port /sender

yarp.Network.connect('/sender' , '/reader') # connecting the /sender port with /reader port
output_port.write(i) # writing the image to the port

output_port.close(); # cleaning up
yarp.Network.fini();

########################################################################################
Here is the script of reader.py. It opens a /reader port which reads the image from the /sender and displays it using plt.imshow():

import yarp
import numpy as np
import matplotlib.pylab as plt

yarp.Network.init()# network initializing

port_in = yarp.Port() # initializing the port
port_in.open('/reader') # opening the /reader port 


i = yarp.ImageRgb() # yarp image 
i.resize(219,230)

a = np.zeros((230,219,3)) # image buffer to store the image
b = a.tobytes()

i.setExternal(b,219,230) # wraping the yarp image around the image buffer

port_in.read(i) # reading the image
img = np.frombuffer(b).reshape(230,219,3) # getting the image from the buffer

plt.imshow(img)
plt.show()

port_in.close();
yarp.Network.fini();

###################################################################################################
On terminal 1, I start yarpserver.
On terminal 2, i run python3 reader.py
On terminal 3, i run python3 sender.py

here is the image that I am trying to transport
bottles_01

And here is what I get at the end
Figure_1

@drdanz drdanz added Component: Bindings swig, python, java, ruby, perl, octave, matlab, lua, csharp, tcl Issue Type: QA/Support This is a generic question or a support request that does not require a patch labels Feb 2, 2021
@robotology robotology deleted a comment from support bot Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Bindings swig, python, java, ruby, perl, octave, matlab, lua, csharp, tcl Issue Type: QA/Support This is a generic question or a support request that does not require a patch
Projects
None yet
Development

No branches or pull requests

2 participants