Reputation: 87
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.
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.
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)
Upvotes: 2
Views: 4308
Reputation: 1
yes. you need to use Google Storage Public URL
cap = cv2.VideoCapture('https://storage.googleapis.com//your_Bucket//sample1.mp4')
Upvotes: -1
Reputation: 8699
Yes, there is a way to read a video file stored in google cloud storage without downloading it, via generating a signed 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
Upvotes: 4