1 回答

TA貢獻1765條經驗 獲得超5個贊
僅使用 Paramiko,您需要將文件復制到本地文件系統,然后將該本地文件用于 cv2。
cv2 不接受這種傳遞文件的方法。
當然,python 有所有東西的庫,所以我認為使用fs.sshfs,它是pyfilesystem2上包含 SFTP 的擴展,應該可以解決問題。 請注意,這實際上與opencv-python
.
編輯1:
從此處的文檔中,您可以看到可以將文件傳遞給 VideoCapture.Open() 的方式。
編輯代碼以在本地復制文件,然后將本地文件傳遞給 openCV 可以正常工作。
sftp.get('file.mp4', 'file.mp4')
sftp.close() # Also, close the sftp connection
cap = cv2.VideoCapture.open('file.mp4')
編輯2:
因此,使用工程將 SFTP 文件系統掛載到本地文件系統ssfhs。最好的方法是使用經過測試的方法在操作系統級別安裝 SFTP。下面是在 python 中執行所有操作的示例 python 代碼,但請注意,這假設ssfhs可以從命令行正確連接到 SFTP 主機。我不會在這里解釋那部分,因為有很多優秀的不同教程。
請注意,這僅包含一些基本的錯誤檢查,因此我建議您確保捕獲任何可能彈出的錯誤。這是概念證明。
import cv2
import os
import subprocess
g_remoteuser = 'USERNAME'
g_remotepassword = 'PASSWORD'
g_remotehost = 'HOSTIP'
g_remotepath = '/home/{remoteuser}/files'.format(remoteuser=g_remoteuser)
g_localuser = 'LOCAL_MACHINE_LINUX_USERNAME'
g_localmntpath = '/home/{localuser}/mnt/remotehost/'.format(localuser=g_localuser)
g_filename = 'file.mp4'
def check_if_path_exists(path):
# check if the path exists, create the path if it doesn't
if not os.path.exists(path):
os.makedirs(path)
def mount(remoteuser, remotehost, remotepath, remotepassword, localmntpath):
check_if_path_exists(localmntpath)
if not check_if_mounted(localmntpath):
subprocess.call([
'''echo "{remotepassword}" | sshfs {remoteuser}@{remotehost}:{remotepath} {localmntpath} \
-o password_stdin -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o auto_unmount -o allow_other'''.format(
remoteuser=remoteuser,
remotehost=remotehost,
remotepath=remotepath,
localmntpath=localmntpath,
remotepassword=remotepassword
)], shell=True)
def unmount(path):
try:
subprocess.call(['sudo umount -l {path}'.format(path=path)], shell=True)
except Exception as e:
print(e)
def check_if_mounted(path):
# check if there's actually files. Hacky way to check if the remote host is already mounted.
# will of course fail if there's no files in the remotehost
from os import walk
f = []
for (dirpath, dirnames, filenames) in walk(path):
f.extend(filenames)
f.extend(dirnames)
if dirnames or filenames or f:
return True
break
return False
if check_if_mounted(g_localmntpath):
unmount(g_localmntpath)
mount(g_remoteuser, g_remotehost, g_remotepath, g_remotepassword, g_localmntpath)
cap = cv2.VideoCapture()
cap.open(g_localmntpath + g_filename)
while True:
_, frame = cap.read()
print(frame)
cv2.imshow('res', frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
unmount(g_localmntpath)
添加回答
舉報