如何使用OpencCV从Firebase读取图像? [英] How to read image from Firebase using OpencCV?
本文介绍了如何使用OpencCV从Firebase读取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有使用OpenCV从Firebase读取图像的想法?还是我必须先下载图片,然后从本地文件夹执行cv.imread功能?
Is there any idea for reading image from Firebase using OpenCV? Or do I have to download the pictures first and then do the cv.imread function from the local folder ?
有什么办法可以使用 cv.imread(link_of_picture_from_firebase)
?
推荐答案
在这里您可以:
- 从磁盘读取JPEG,
- 转换为JSON,
- 上传到Firebase
那么您可以:
- 从Firebase取回图像
- 将JPEG数据解码回Numpy数组
- 将检索到的图像保存在磁盘上
#!/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)
这篇关于如何使用OpencCV从Firebase读取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文