-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsecurityCam.py
More file actions
137 lines (94 loc) · 3.38 KB
/
securityCam.py
File metadata and controls
137 lines (94 loc) · 3.38 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#
# ECE 471 Final Project
# Team Members: Derek Haas, Dustin Knight, Cameron Sullivan
# Description: Security camera system that uses a ultrasonic sensor to detect people
# Then turn a camera using a servo motor and take a picture.
#
# THIS IS THE MAIN CODE FOR THE PROJECT. RUNNING THIS PYTHON SCRIPT RUNS THE SECURITY SYSTEM.
from picamera import PiCamera
import time
from subprocess import call
import os.path
from flask import Flask, render_template
def takePic():
pictureLocation = "/home/pi/ECE471-Project/webServer/static/"
fileNum = 0
exists = True
# Check if previous image exists
if(os.path.exists(pictureLocation + "detected.jpg") == 0):
detectionPicture = "detection.jpg"
exists = False
while(exists):
if(os.path.exists(pictureLocation + "previous%d.jpg" % fileNum)):
fileNum += 1
else:
detectionPicture = "previous%d.jpg" % fileNum
call(["mv", "detected.jpg", detectionPicture])
detectionPicture = "detected.jpg"
camera = PiCamera()
camera.vflip = True
camera.start_preview() # Turn on Camera and preview
time.sleep(1) # Wait 1 second to focus
camera.capture(pictureLocation + detectionPicture) # Where to save pics
camera.stop_preview() # Stop the preview
camera.close()
return
def main():
counter = 0
print("Starting Ultrasonic Sensors...")
# Start the ultrasonic sensors
call("sudo ultrasonic/distance &", shell = True)
# Wait 23 seconds to allow sensors to initialize, get baseline distance, and get noise.
time.sleep(23)
# Indicate that baseline and noise are set by rotating the servo.
call(["sudo", "servo/servo", "0"])
time.sleep(0.5)
call(["sudo", "servo/servo", "180"])
time.sleep(0.5)
call(["sudo", "servo/servo", "90"])
# Detection.txt is the file used to indicate whether a person is detected. This file is written by getDistance.c
detected = open("detection.txt", "w")
detected.write("0")
detected.close()
print("SYSTEM READY")
# Infinite loop to detect a person and take a picture of them
while(1):
# Open file containing what sensor detected a person
detected = open("detection.txt", "r")
print("No one Detected")
# Busy loop to wait until someone is detected
while(int(detected.read()) == 0):
detected.close()
detected = open("detection.txt", "r")
detected.close()
detected = open("detection.txt", "r")
# If the value in the file is left, move camera to left sensor
if(int(detected.read()) == 1):
print("DETECTED LEFT")
call(["sudo", "servo/servo", "0"])
detected.close()
detected = open("detection.txt", "r")
# If the value in the file is center, move camera to center sensor
if(int(detected.read()) == 2):
print("DETECTED CENTER")
call(["sudo", "servo/servo", "90"])
detected.close()
detected = open("detection.txt", "r")
# If the value in the file is right, move camera to right sensor
if(int(detected.read()) == 3):
print("DETECTED RIGHT")
call(["sudo", "servo/servo", "180"])
print("Taking Picture...")
# Once camera is in the correct position, take a picture
takePic()
time.sleep(1)
# Kill the web server then run it again to display the photo.
call("pkill \"python3 webServer/web.py\" -f", shell = True)
call("python3 webServer/web.py &", shell = True)
print("Picture Taken!\n")
# Close the file containing what sensor detected a person
detected.close()
return
# Call the main function
if __name__ == "__main__":
main()