-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPiCamera.py
More file actions
92 lines (82 loc) · 2.19 KB
/
PiCamera.py
File metadata and controls
92 lines (82 loc) · 2.19 KB
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
87
88
89
90
91
92
import RPi.GPIO as GPIO
from multiprocessing import Process
import picamera
import pyrebase
import string
import random
import time
import datetime
import os
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
IR_PIN = 22
LED_PIN = 24
GPIO.setup(IR_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)
nextImg = 0
db = None
storage = None
def snapPhoto():
global nextImg
global db
global storage
with picamera.PiCamera() as camera:
# Get the camera ready
rndStr = ''.join(random.choice(string.ascii_uppercase+string.digits) for _ in range(10))
rndStr += '.jpg'
time.sleep(1)
camera.capture(rndStr)
timeStmp = datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')
data = {
'key':rndStr,
'time': timeStmp,
'url':rndStr
}
db.child('Camera').child('data{0}'.format(nextImg)).set(data)
nextImg = nextImg + 1
print("Capturing Picture")
while (not os.path.exists(rndStr)):
time.sleep(1)
print(os.path.exists(rndStr))
storage.child(rndStr).put(rndStr)
print("Photo taken")
return
def runCameraPreview():
print("Starting process")
# try image capture in a separate process
p1 = Process(target=snapPhoto)
p1.start()
# Do any processing
print("Waiting")
p1.join() # Wait for the process to return
print('Done!')
if __name__=='__main__':
config = {
"apiKey": "**************************************",
"authDomain": "**** PROJECT ID ****.firebaseapp.com",
"databaseURL": "https://**** PROJECT ID ****.firebaseio.com",
"projectId": "**************",
"storageBucket": "**** PROJECT ID *****.appspot.com",
"serviceAccount": "*** PATH TO SERVICE ACCOUNT KEY JSON FILE ***",
"messagingSenderId": "***********"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
storage = firebase.storage()
initData = db.child('Camera').get()
if (initData.val()):
nextImg = len(initData.val())+1
else:
nextImg = 1
print(nextImg)
print('Ready')
while True:
current = GPIO.input(IR_PIN)
if current:
GPIO.output(LED_PIN, GPIO.HIGH)
print ('object detected')
snapPhoto()
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(5)
GPIO.cleanup()