-1

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 ?

Is there any way that I could just use cv.imread(link_of_picture_from_firebase)?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432

1 Answers1

1

Here's how you can:

  • read a JPEG from disk,
  • convert to JSON,
  • upload to Firebase

Then you can:

  • retrieve the image back from Firebase
  • decode the JPEG data back into a Numpy array
  • save the retrieved image on disk

#!/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)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • okayy I will give a try first , but for the picture , is it possible for me to retrieve it from the firebase storage ? Because I uploaded it with my android app , which is pure JPEG image – Yusuf Mohd Suhair Dec 16 '19 at 03:38