简体   繁体   中英

How to use opencv in a remote jupyter notebook execution?

In my local machine with macOS Mojave, I installed a virtual environment with Python 3.6, opencv package and some additional unrelated packages.

I started a very simple Jupyter Notebook that is executed with no problem, here it's the code:

import cv2
print(cv2.__version__)
my_img = cv2.imread("colibri_763_460.jpeg",1)
cv2.imshow("Original", my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Now, I'm trying to execute the same code in a Jupyter Notebook in my remote Ubuntu machine but I can't make it work. These are the steps that I took:

  • I copied the image file and the Notebook ypnb file to the remote ubuntu machine.
  • I opened an ssh session, selected a virtualenv with the same packages than my local machine and executed the "jupyter notebook"
  • I opened another terminal with ssh -L 8000:localhost:8888 mpastorg@mpgubu18 for the ssh tunneling
  • I opened my local browser in the localhost:8000 to execute the Notebooks that I have in my remote Ubuntu machine, it works with any Notebook not involving opencv.

I tried to solve my problem using two different ways:

  • Installing in the ubuntu remote machine the package opencv-contrib-python-headless: when I try to use cv2.imshow , I got the error method not found
  • Installing in the ubuntu remote machine the package opencv-contrib-python: I got the error that kernel is dead.

Does anyone know if there's any way to execute the remote jupyter notebook and see the picture locally? it maybe would be possible a workaround to embed the picture in the browser window?

Thank you very much Marcos Pastor

I wonder if the image is being rendered using X - which you have not explicitly enabled, in both your host machine, nor in your ssh tunnel.

On the Host machine. (Where you want to see the Image). Enter this comment

xhost+

This now allows remote X-Hosts to send you data. As you are on a Mac - you will need to have the XQuartz package installed.

Now modify your ssh command. - so that you allow X - this is typically done using a -X flag (Or it can be placed in your .ssh/config file like this

Host *
  ForwardX11 yes
  ForwardX11Trusted no
  XAuthLocation /opt/X11/bin/xauth
  ForwardAgent yes
  Compression yes
  KeepAlive yes
  Compression yes

If you are not using a config try the -X to start with.

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM