简体   繁体   English

如何在不下载的情况下从 Google Cloud Storage 读取视频? 使用视频网址

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

I want to read a video file using opencv but the problem is that the video is on my Google Cloud Storage, and opencv.videocapture method won't recognize it.我想使用 opencv 读取视频文件,但问题是视频在我的 Google Cloud Storage 上,而opencv.videocapture方法无法识别它。 Is there any other way to access video file without downloading the video on a local disk?有没有其他方法可以在不下载本地磁盘上的视频的情况下访问视频文件?

I also tried with urllib.urlopen but it didn't work.我也尝试过urllib.urlopen但它没有用。 Here's my code:这是我的代码:

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

Errors I got:我得到的错误:

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)

Yes, there is a way to read a video file stored in google cloud storage without downloading it, via generating a signed url.是的,有一种方法可以通过生成签名 url 来读取存储在谷歌云存储中的视频文件而无需下载它。

Google storage bucket structure :谷歌存储桶结构

在此处输入图片说明

Code (Tested):代码(已测试):

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

Library needs to be installed:需要安装库:

firebase_admin : pip install firebase_admin firebase_admin : pip install firebase_admin

yes.是的。 you need to use Google Storage Public URL您需要使用 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