Saving image with OpenCV in online platform


#1

Hello, I tried to save image with OpenCV using this code:

image_msg = camera_right.value
if image_msg is not None:
img = CvBridge().imgmsg_to_cv2(image_msg, “rgb8”)
cv2.imwrite(‘somefile.png’, img)

The image doesn’t save and i think it’s beacuse I have to specify the path.

I’d like to ask if there’s a path prefix I have to use when using online HBP or there’s other way to save image.

Thanks for your help.


#2

Please try to give the “resources” path and check if the files is saved under “resources” from the online file explorer.

Axel


#3

Thank you for your response.

I tried using only “resources”, but got an error, that there’s no writer for such file.

Current Code:

@nrp.MapCSVRecorder("recorder",
                    filename="my_csv.csv",
                    headers=["My_header1", "My_header2", "My_header3"])

@nrp.MapRobotSubscriber("camera_right",
                        Topic("/icub/icub_model/right_eye_camera/image_raw",
                        sensor_msgs.msg.Image))

@nrp.Robot2Neuron("camera_right")
def record_right_image (t, recorder, camera_right):
    image_msg = camera_right.value;
    if image_msg is not None:
        img = CvBridge().imgmsg_to_cv2(image_msg, "rgb8")
        recorder.record_entry('{:.0f}'.format(t), img, "data3", "data4")
        cv2.imwrite("resources/myfile2.png", img)

If I tried, to print img (converted file), I only got it in csv as [values] … [values] … [values], like it’s only preview of data. So the conversion is alright.

But still the last line doesn’t save the file. I assume the path should be specified in another way.


#4

Also tried creating py Module I imported into resources:

import cv2
class FileSaver:
    def __init__(self):
        pass
        
def save_file(self, image):
    cv2.imwrite("file.png", image)

using import FileSaver I get an error:
global name ‘FileSaver’ is not defined (Runtime)


#5

Dear Adrian,

There is actually no simple way to do it yet. So I added a feature request for you and we will handle this asap.
https://hbpneurorobotics.atlassian.net/browse/NUIT-138

Sorry, that this is the best answer I can give you so far.
Best regards