gpt4 book ai didi

python - 如何从Google Cloud Storage中读取视频而不进行下载?使用视频网址

转载 作者:行者123 更新时间:2023-12-02 16:52:46 24 4
gpt4 key购买 nike

我想使用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来读取存储在Google云存储中的视频文件而无需下载它。

Google存储桶结构:

enter image description here

代码(已测试):

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

关于python - 如何从Google Cloud Storage中读取视频而不进行下载?使用视频网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57232521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com