Firebase 数据库未导入到 python 代码文件中

问题描述 投票:0回答:1

我已经在 firebase 上成功创建了数据库,但是当我尝试使用以下函数来检索所述数据时,它只打印出“none”作为输出

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from firebase_admin import  storage

counter = 0
id = 0

while True:
    success, img = cap.read()

    imgS = cv2.resize(img,(0, 0), None , 0.25, 0.25)
    imgS = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    faceCurFrame = frm.face_locations(imgS)
    encodeCurrFrame = frm.face_encodings(imgS, faceCurFrame)


    bkground[162: 162+480, 55 : 55+640] = img #Cooridinates found by self, attaches the webcam to the window
    bkground[44:44 + 633, 808 : 808 + 414] = image_in_mode[0]
#     cv2.imshow("Vision" , img)
        
    for encodeFace, faceLoc in zip(encodeCurrFrame, faceCurFrame):
          matches = frm.compare_faces(encodeListKnown, encodeFace)
          Face_Distance = frm.face_distance(encodeListKnown, encodeFace)

          matchIndex = np.argmin(Face_Distance)
         
          if matches[matchIndex]:
                # print(studentIds[matchIndex])
                y1 , x2 , y2, x1 = faceLoc
                y1 , x2 , y2, x1 = y1*4 , x2*4 , y2*4, x1*4
                bbox = 55+x1, 162+y1 , x2-x1, y2-y1
                bkground = cvzone.cornerRect(bkground, bbox , rt=0)
                id = studentIds[matchIndex]
                if counter == 0:
                                counter = 1
          

cred = credentials.Certificate(r"C:\Users\Storm\After Hours(Python)\facial recognition\serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
    'databaseURL' :"https://facerecognition-17636-default-rtdb.firebaseio.com/",
    'storageBucket': "facerecognition-17636.appspot.com"
})
studentInfo = db.reference(f'Students/{id}').get()
print(studentInfo)

我什至尝试使用以下方法,得到没有属性名称child:

studentInfo = db.child("Students").get()
print(studentInfo.val())
python database firebase-realtime-database
1个回答
0
投票

据我所知,您的数据结构如下所示:

{
  "Students" : {
    "Elon" : {
      "logins" : 0,
      "name" : "Elon",
      "position" : "inspiration"
    },
    "Manansh" : {
      "logins" : 0,
      "name" : "Manansh",
      "position" : "Creator"
    }
  }
}

并且您的代码尝试使用

访问它
studentInfo = db.reference(f'Students/{id}').get()

id
设置为:

id = 0

"0"
下没有子属性
Students
,所以你不会得到任何值。

如果你想读取Elon的数据,请将

id
设置为
"Elon"

id = "Elon"
© www.soinside.com 2019 - 2024. All rights reserved.