将图像作为 np 数组上传到 FIrebase 存储
Upload image as np array to FIrebase storage
我正在尝试将图像上传到 Firebase 存储。
我在上传本地图片时没有问题,使用:
import pyrebase
db_credentials = "local_file.json"
firebase_config = {
'apiKey': "...",
'authDomain': "...",
'databaseURL': "...",
'projectId': "...",
'storageBucket': "...",
'messagingSenderId': "...",
'appId': "...",
'measurementId': "...",
'serviceAccount': db_credentials
}
firebase_storage = pyrebase.initialize_app(firebase_config)
storage = firebase_storage.storage()
storage.child('remote_path').put('local_path.png')
然而,在我的例子中,图像是来自远程摄像机的帧,使用 OpenCV 检索,因此我将其作为 np.array。我试过像前面的例子一样使用它,但它不起作用。
cap = cv2.VideoCapture(remote_camera_address)
ret, frame = cap.read() -> frame is a np array
storage.child('remote_path').put(frame)
返回错误:
raise ValueError('total bytes could not be determined. Please '
ValueError: total bytes could not be determined. Please pass an explicit size.
显然,我可以将 np 数组保存到本地图像,上传该图像并在之后删除它,但我想知道我是否可以以更简洁的方式进行。
从看来,你可以传递一个chunk_size
参数来摆脱这个问题:
blob = self.bucket.blob(filename, chunk_size=262144) # 256KB
blob.upload_from_file(image)
由于您使用的是 Pyrebase,对 upload_from_file
的调用实际上包含在您正在调用的 put
implementation 中,因此您可以为此操作放弃 Pyrebase 并在存储桶上进行调用直接,或者您可以向 Pyrebase 提交功能请求以添加对此功能的支持。
我正在尝试将图像上传到 Firebase 存储。 我在上传本地图片时没有问题,使用:
import pyrebase
db_credentials = "local_file.json"
firebase_config = {
'apiKey': "...",
'authDomain': "...",
'databaseURL': "...",
'projectId': "...",
'storageBucket': "...",
'messagingSenderId': "...",
'appId': "...",
'measurementId': "...",
'serviceAccount': db_credentials
}
firebase_storage = pyrebase.initialize_app(firebase_config)
storage = firebase_storage.storage()
storage.child('remote_path').put('local_path.png')
然而,在我的例子中,图像是来自远程摄像机的帧,使用 OpenCV 检索,因此我将其作为 np.array。我试过像前面的例子一样使用它,但它不起作用。
cap = cv2.VideoCapture(remote_camera_address)
ret, frame = cap.read() -> frame is a np array
storage.child('remote_path').put(frame)
返回错误:
raise ValueError('total bytes could not be determined. Please '
ValueError: total bytes could not be determined. Please pass an explicit size.
显然,我可以将 np 数组保存到本地图像,上传该图像并在之后删除它,但我想知道我是否可以以更简洁的方式进行。
从chunk_size
参数来摆脱这个问题:
blob = self.bucket.blob(filename, chunk_size=262144) # 256KB
blob.upload_from_file(image)
由于您使用的是 Pyrebase,对 upload_from_file
的调用实际上包含在您正在调用的 put
implementation 中,因此您可以为此操作放弃 Pyrebase 并在存储桶上进行调用直接,或者您可以向 Pyrebase 提交功能请求以添加对此功能的支持。