1 回答

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
僅使用 Paramiko,您需要將文件復(fù)制到本地文件系統(tǒng),然后將該本地文件用于 cv2。
cv2 不接受這種傳遞文件的方法。
當(dāng)然,python 有所有東西的庫(kù),所以我認(rèn)為使用fs.sshfs,它是pyfilesystem2上包含 SFTP 的擴(kuò)展,應(yīng)該可以解決問(wèn)題。 請(qǐng)注意,這實(shí)際上與opencv-python
.
編輯1:
從此處的文檔中,您可以看到可以將文件傳遞給 VideoCapture.Open() 的方式。
編輯代碼以在本地復(fù)制文件,然后將本地文件傳遞給 openCV 可以正常工作。
sftp.get('file.mp4', 'file.mp4')
sftp.close() # Also, close the sftp connection
cap = cv2.VideoCapture.open('file.mp4')
編輯2:
因此,使用工程將 SFTP 文件系統(tǒng)掛載到本地文件系統(tǒng)ssfhs。最好的方法是使用經(jīng)過(guò)測(cè)試的方法在操作系統(tǒng)級(jí)別安裝 SFTP。下面是在 python 中執(zhí)行所有操作的示例 python 代碼,但請(qǐng)注意,這假設(shè)ssfhs可以從命令行正確連接到 SFTP 主機(jī)。我不會(huì)在這里解釋那部分,因?yàn)橛泻芏鄡?yōu)秀的不同教程。
請(qǐng)注意,這僅包含一些基本的錯(cuò)誤檢查,因此我建議您確保捕獲任何可能彈出的錯(cuò)誤。這是概念證明。
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)
添加回答
舉報(bào)