gpt4 book ai didi

python - 如何在python中使用opencv从sftp位置读取视频文件

转载 作者:行者123 更新时间:2023-12-02 17:05:21 25 4
gpt4 key购买 nike

我在使用opencv lib从sftp位置读取文件时遇到问题。您能告诉我如何从sftp位置或sftp文件对象读取文件。如果您能告诉我直接将大文件读取到opencv lib,那就好了。

import paramiko
import cv2
import numpy as np

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("IPADDRESS", port=22, username='USERNAME', password='PASSWORD')
t = client.get_transport()
sftp = paramiko.SFTPClient.from_transport(t)
sftp.chdir("/home/bizviz/devanshu_copy")

obj = sftp.open("SampleVideo_1280x720_1mb.mp4")

cap = cv2.VideoCapture.open(obj)

while True:
_,frame = cap.read()
print(frame)
cv2.imshow('res', frame)
key = cv2.waitKey(1)
if key == 27:
break

cap.release()
cv2.destroyAllWindows()

最佳答案

仅使用Paramiko,您需要将文件复制到本地文件系统,然后将该本地文件用于cv2。

cv2不接受这种传递文件的方法。

当然,python具有所有库,因此,我认为使用fs.sshfs可以解决问题,它是pyfilesystem2的扩展,包括SFTP。
请注意,这实际上与opencv-python配合使用效果不佳。

编辑1:

docs here中,您可以看到将文件传递给VideoCapture.Open()的方式。
enter image description here

编辑代码以在本地复制文件,然后将本地文件传递给openCV可以正常工作。

sftp.get('file.mp4', 'file.mp4')
sftp.close() # Also, close the sftp connection

cap = cv2.VideoCapture.open('file.mp4')

编辑2:

因此,使用 ssfhs 将SFTP文件系统挂载到本地文件系统是可行的。最好的方法是使用经过测试的方法在操作系统级别挂载SFTP。以下是可在python中完成所有操作的示例python代码,但请注意,这假定 ssfhs可以从命令行正确连接到SFTP主机。我在这里不解释该部分,因为有 excellent不同的 tutorials

请注意,这仅包含一些基本的错误检查,因此我建议确保您捕获任何可能弹出的错误。这是概念的证明。

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)

关于python - 如何在python中使用opencv从sftp位置读取视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com