第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

慕容3067478 2022-01-18 16:35:04
我在使用 opencv lib 從 sftp 位置讀取文件時(shí)遇到問(wèn)題。你能告訴我如何從 sftp 位置或 sftp 文件對(duì)象讀取文件嗎?如果你能告訴我直接將大文件讀取到 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貢獻(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() 的方式。

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

編輯代碼以在本地復(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)


查看完整回答
反對(duì) 回復(fù) 2022-01-18
  • 1 回答
  • 0 關(guān)注
  • 252 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)