Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ssh read image

I am using Python 2.7 and OpenCV 2.4. I want to read and show the image from the remote machine. Then, I try to use the library paramiko. However, I cannot read the file.

Here is my code. First, I import all libraries and setup connection:

import paramiko
import cv2
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("XXX.XXX.XXX",22,username="NAME",password='PW',timeout=4)

Second, I open an SFTP session and open the target image:

sftp = s.open_sftp()
remote_file = sftp.open('/home/frame/image.jpg')

I try to print the remote_file : print remote_file

It returns **paramiko.sftp_file.SFTPFile object at 0x000000000572AC50**

Finally, I try to read and show the image:

img = cv2.imread(remote_file)
cv2.imshow("image", img)

However, error comes out:

  File "ssh.py", line 25, in <module>
    img = cv2.imread(remote_file)
TypeError: expected string or Unicode object, SFTPFile found

_________________________________________________________________________-

I am asking for help how can I read the image from another remote machine. Is my way to do is correct? Thank you.

like image 350
VICTOR Avatar asked Dec 01 '25 09:12

VICTOR


1 Answers

Here a solution:

import numpy as np

...

sftp = s.open_sftp()
with sftp.open('/home/frame/image.jpg') as f:
  img = cv2.imdecode(np.fromstring(f.read(), np.uint8), 1)

cv2.imshow("image", img)
cv2.waitKey(0)
like image 96
Ulrich Stern Avatar answered Dec 03 '25 23:12

Ulrich Stern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!