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

问题描述 投票:0回答:1

我在使用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()
python python-3.x opencv video-streaming sftp
1个回答
2
投票

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

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

当然,python有所有的库,所以我认为使用fs.sshfs,它是pyfilesystem2的扩展,包括SFTP,应该可以做到这一点。请注意,这实际上并不适合与opencv-python


EDIT1

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')

Aaditi:

因此,使用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)

© www.soinside.com 2019 - 2024. All rights reserved.