亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在python中使用opencv從sftp位置讀取視頻文件

如何在python中使用opencv從sftp位置讀取視頻文件

慕容3067478 2022-01-18 16:35:04
我在使用 opencv lib 從 sftp 位置讀取文件時遇到問題。你能告訴我如何從 sftp 位置或 sftp 文件對象讀取文件嗎?如果你能告訴我直接將大文件讀取到 opencv lib 那么這是件好事。import paramikoimport cv2import numpy as npclient = 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:        breakcap.release()cv2.destroyAllWindows()
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

僅使用 Paramiko,您需要將文件復制到本地文件系統,然后將該本地文件用于 cv2。

cv2 不接受這種傳遞文件的方法。

當然,python 有所有東西的庫,所以我認為使用fs.sshfs,它是pyfilesystem2上包含 SFTP 的擴展,應該可以解決問題。 請注意,這實際上與opencv-python.


編輯1:

此處的文檔中,您可以看到可以將文件傳遞給 VideoCapture.Open() 的方式。

http://img1.sycdn.imooc.com//61e67bd2000175f906250148.jpg

編輯代碼以在本地復制文件,然后將本地文件傳遞給 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)


查看完整回答
反對 回復 2022-01-18
  • 1 回答
  • 0 關注
  • 262 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號