是否有使用OpenCV从Firebase读取图像的想法?还是我必须先下载图片,然后从本地文件夹执行cv.imread功能?
有什么方法可以只使用cv.imread(link_of_picture_from_firebase)
?
答案 0 :(得分:0)
您可以按照以下方式操作:
然后您可以:
#!/usr/bin/env python3
import numpy as np
import cv2
from base64 import b64encode, b64decode
import pyrebase
config = {
"apiKey": "SECRET",
"authDomain": "SECRET",
"databaseURL": "SECRET",
"storageBucket": "SECRET",
"appId": "SECRET",
"serviceAccount": "FirebaseCredentials.json"
}
# Initialise and connect to Firebase
firebase = pyrebase.initialize_app(config)
db = firebase.database()
# Read JPEG image from disk...
# ... convert to UTF and JSON
# ... and upload to Firebase
with open("image2.jpg", 'rb') as f:
data = f.read()
str = b64encode(data).decode('UTF-8')
db.child("image").set({"data": str})
# Retrieve image from Firebase
retrieved = db.child("image").get().val()
retrData = retrieved["data"]
JPEG = b64decode(retrData)
image = cv2.imdecode(np.frombuffer(JPEG,dtype=np.uint8), cv2.IMREAD_COLOR)
cv2.imwrite('result.jpg',image)