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

标签 python python-3.x opencv video-streaming sftp

我在使用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/

相关文章:

python - 无法使用 Python3 中的 multiprocessing.Process 对象更改类变量

python - 使用网页抓取来检查商品是否有库存

matlab - 在 Matlab 中计算 opencv 的透视变换时出错

python - “pip install causallift”未安装

python - 无法在 macOS 上的 Anaconda3 python3.6 上安装 OpenCV3

python - 在 Databricks 中导入笔记本

python - Anaconda OpenCV Arch Linux libselinux.so 错误

python - 蒙版图像的 BGR 值(OpenCV、Python)

Python 创建自定义 NaN

python - Django累积总和或运行总和