-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcloud_api_helper.py
86 lines (72 loc) · 2.7 KB
/
cloud_api_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
"""
import os
import cv2
import time
import datetime
import configparser
from google.cloud import firestore, storage
from google.api_core import datetime_helpers
configPath = "config.ini"
# helper functions
parser = configparser.ConfigParser()
if parser.read(configPath)[-1] == "config.ini":
filePath = parser["CLOUD_CONFIG"].get("SFP")
projectName = parser["CLOUD_CONFIG"].get("PJN")
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.abspath(filePath)
db = firestore.Client()
camera_collection = db.collection(parser["CLOUD_CONFIG"].get("CAM"))
intruder_collection = db.collection(parser["CLOUD_CONFIG"].get("LOG"))
facedata_collection = db.collection(parser["CLOUD_CONFIG"].get("FAD"))
# Document Templates in firestore
log_document_template = {
"ImageURL": None,
"Labels": [],
"Timestamp": None,
"UserID": None,
}
facedata_document_template = {
"FaceEncodingURI": None,
"FirstSeen": None,
"ImageURI": None,
"LastSeen": None,
"UserSetLables": [],
}
# This document alone has to be fetched with the UID
cameralist_document_template = {"CameraName": [], "CameraURL": []}
# Bucket to store image files
image_bucket = storage.Client()
bucket = image_bucket.get_bucket(projectName + ".appspot.com")
document = facedata_collection.document()
print(document.id)
camera = cv2.VideoCapture(0)
_, image = camera.read()
if _:
currentTime = datetime_helpers.utcnow()
# reformat it asap
permFileName = (
parser["APP_CONFIG"].get("CameraName")
+ "_"
+ str(currentTime).replace(" ", "_").replace(":", "_")
+ ".jpg"
)
cv2.imwrite(permFileName, image)
blob = bucket.blob(permFileName)
blob.upload_from_filename(permFileName)
tempIntruderLogDocument = intruder_collection.document(
parser["APP_CONFIG"].get("CameraName") + " " + str(currentTime)
)
# this uri requires authentication to get a public url make the blob public and get the url
# blob.make_public()
log_document_template["ImageURL"] = blob.media_link
log_document_template["Labels"] = ["test", "test2"]
log_document_template["Timestamp"] = currentTime
log_document_template["UserID"] = parser["APP_CONFIG"].get("CameraName")
tempIntruderLogDocument.create(log_document_template)
os.remove(permFileName)
camera.release()
for doc in camera_collection.stream():
print(u"{} => {}".format(doc.id, doc.to_dict()))
for doc in intruder_collection.stream():
print(u"{} => {}".format(doc.id, doc.to_dict()))
exit(0)