简体   繁体   中英

How to open images on remote jupyter notebook with OpenCV?

I have two computers both running Windows 10:

  • Computer 1 has anaconda prompt running and jupyter notebook as command line instance running. It works as a 'server' instance.
  • Computer 2 has the jupyter notebook running in web browser.

These are connected through SSH tunnel with PuTTY. Now i'm running this code through Computer 2, but OpenCV opens the images on Computer 1. How can I get the images to open at computer 1?

Example code (modified from here ):

import cv2, numpy as np, random

image = np.zeros((224,224,3))
w, h = image.shape[1], image.shape[0]
image_to_show = np.copy(image)

def rand_pt():
 return (random.randrange(w),
 random.randrange(h))

finish = False
while not finish:
    cv2.imshow("result", image_to_show)
    key = cv2.waitKey(0)
    if key == ord('p'):
        for pt in [rand_pt() for _ in range(10)]:
            cv2.circle(image_to_show, pt, 3, (255, 0, 0), -1)
    elif key == 27:
        #if ESC then finish
        finish = True
cv2.destroyAllWindows()

Assuming that the computers are on the same network, you can access the file through a full network path as such (I have used a.txt in the example but the images should work the same)

file = '//network/path/to/file.txt'
f = open(file, "r")
print(f.read())

If the computers are not on the same network, you have some options like:

  • turn computer 1 into a web server
  • use a broker/subscriber or a similar kind on connection between the computers
  • place both computers in the same vpn, thus simulating a "same network" scenario

Note that these alternatives would require quite a lot of devops work and would have a VERY noticeable drop in speed compared to reading the images locally.

If you are going to open a big dataset or work with big images, consider keeping a separate copy of the images in computer 2. Otherwise you would have a huge overhead when running your models

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