簡體   English   中英

如何在不下載的情況下從 Google Cloud Storage 讀取視頻? 使用視頻網址

[英]How to read Video from Google Cloud Storage without downloading it? using video URL

我想使用 opencv 讀取視頻文件,但問題是視頻在我的 Google Cloud Storage 上,而opencv.videocapture方法無法識別它。 有沒有其他方法可以在不下載本地磁盤上的視頻的情況下訪問視頻文件?

我也嘗試過urllib.urlopen但它沒有用。 這是我的代碼:

import urllib
import numpy as np
import cv2

uri = 'gs://call-gestures/dictionany/AIDS.mp4'
url = "https://storage.cloud.google.com/call- 
gestures/dictionany/AIDS.mp4?authuser=1"
# Open a sample video available in sample-videos
r = cv2.imread(url)
print('Result: ',r)
cap = cv2.VideoCapture(url)
print(cap)
if cap.isOpened():
    print ("File Can be Opened")
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()
        #print cap.isOpened(), ret
        if frame is not None:
            # Display the resulting frame
            cv2.imshow('frame',frame)
            # Press q to close the video windows before it ends if you want
            if cv2.waitKey(22) & 0xFF == ord('q'):
                break
        else:
            print("Frame is None")
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    print ("Video stop")
else:
    print("Not Working")

我得到的錯誤:

warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:901)
warning: https://storage.cloud.google.com/call-gestures/dictionany/Asia.mp4?authuser=1 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:902)

是的,有一種方法可以通過生成簽名 url 來讀取存儲在谷歌雲存儲中的視頻文件而無需下載它。

谷歌存儲桶結構

在此處輸入圖片說明

代碼(已測試):

import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
import datetime

import urllib.request as req
import cv2

cred = credentials.Certificate('Path\to\your\google-credential-file.json')
app = firebase_admin.initialize_app(cred, {'storageBucket': 'cnc-designs.appspot.com'}, name='storage')
bucket = storage.bucket(app=app)

def generate_image_url(blob_path):
    """ generate signed URL of a video stored on google storage. 
        Valid for 300 seconds in this case. You can increase this 
        time as per your requirement. 
    """                                                        
    blob = bucket.blob(blob_path) 
    return blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET')


url = generate_image_url('sample1.mp4')
req.urlretrieve(url, "sample1.mp4")
cap = cv2.VideoCapture('sample1.mp4')

if cap.isOpened():
    print ("File Can be Opened")
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()
        #print cap.isOpened(), ret
        if frame is not None:
            # Display the resulting frame
            cv2.imshow('frame',frame)
            # Press q to close the video windows before it ends if you want
            if cv2.waitKey(22) & 0xFF == ord('q'):
                break
        else:
            print("Frame is None")
            break
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    print ("Video stop")
else:
    print("Not Working")

需要安裝庫:

firebase_admin : pip install firebase_admin

是的。 您需要使用 Google Storage 公共網址

cap = cv2.VideoCapture('https://storage.googleapis.com//your_Bucket//sample1.mp4')

1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM