import firebase_admin from firebase_admin import credentials from firebase_admin import firestore from PIL import Image import time, os import camera import image_tools import config config.bootstrap() # Use a service account cred = credentials.Certificate(os.getenv('FIREBASE_CERT_FILE')) firebase_admin.initialize_app(cred) db = firestore.client() doc_ref_lot1 = db.collection(os.getenv('LOT_NAME')) def poll(): # Take the picture img = Image.open(camera.capture(os.getenv('CAMERA_DEVICE'))).load() #img = Image.open('./lib/test-images/lot-empty.jpg').load() reference = Image.open(os.getenv('REFERENCE_IMAGE')).load() stalls = doc_ref_lot1.get() for stall in stalls: stall_obj = stall.to_dict() color_reference = image_tools.average_color(stall_obj['locationX'], stall_obj['locationY'], stall_obj['sideN'], reference) color_compare = image_tools.average_color(stall_obj['locationX'], stall_obj['locationY'], stall_obj['sideN'], img) stall_occupied = image_tools.threshold(image_tools.differences(color_reference, color_compare), float(os.getenv('E_HAT_TRIGGER_LEVEL'))) stall_obj['open'] = not stall_occupied print(stall.id) db.collection(u'Lot1').document(stall.id).set( stall_obj ) start_time = time.time() while True: poll() poll_time = float(os.getenv('POLL_FREQUENCY')) time.sleep(poll_time - ((time.time() - start_time) % poll_time))