Vehicle-Anti-Theft-Face-Rec.../final_demo.py

215 lines
8 KiB
Python
Raw Permalink Normal View History

2021-04-03 22:41:40 +00:00
import os, signal
import RPi.GPIO as GPIO
import pyrebase # u need to install Pyrebase module firstly
2021-04-04 15:42:34 +00:00
import time
from picamera import PiCamera
2021-04-03 22:41:40 +00:00
from time import sleep
2021-04-04 15:42:34 +00:00
class Car:
2021-04-03 22:41:40 +00:00
print("Starting of the program")
def __init__(self):
config = {
2021-04-04 15:42:34 +00:00
"apiKey": "AIzaSyAdL0W5HscjEDFPK4BDi6Cnc7FLa30GPYY",
"authDomain": "vehicleantitheftrecognition.firebaseapp.com",
"databaseURL": "https://vehicleantitheftrecognition.firebaseio.com",
"projectId": "vehicleantitheftrecognition",
"storageBucket": "vehicleantitheftrecognition.appspot.com",
"messagingSenderId": "163692530359",
"appId": "1:163692530359:web:b6dc7ccfc56a79afb11b32",
"measurementId": "G-EPWP2LK89Q"
2021-04-03 22:41:40 +00:00
}
2021-04-04 15:42:34 +00:00
2021-04-03 22:41:40 +00:00
self.firebase = pyrebase.initialize_app(config)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
2021-04-04 15:42:34 +00:00
# preset GPIO ports for 2 motors
2021-04-03 22:41:40 +00:00
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
2021-04-04 19:59:30 +00:00
self.Motor3 = {'EN': 6, 'input1': 19, 'input2': 13}
self.Motor4 = {'EN': 16, 'input1': 20, 'input2': 21}
2021-04-04 15:42:34 +00:00
2021-04-03 22:41:40 +00:00
# preset the port for buttons and alarm
GPIO.setup(26,GPIO.OUT) # alarm output
2021-04-04 15:42:34 +00:00
# preset the port for the distance sensor
self.GPIO_TRIGGER = 18
self.GPIO_ECHO = 4
# set GPIO direction (IN / OUT)
GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(self.GPIO_ECHO, GPIO.IN)
2021-04-03 22:41:40 +00:00
for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT)
2021-04-04 19:59:30 +00:00
GPIO.setup(self.Motor3[x], GPIO.OUT)
GPIO.setup(self.Motor4[x], GPIO.OUT)
2021-04-04 15:42:34 +00:00
# utilize PWM function, enable motors and frequency is 100Hz
2021-04-03 22:41:40 +00:00
self.EN1 = GPIO.PWM(self.Motor1['EN'], 100)
2021-04-04 19:59:30 +00:00
self.EN2 = GPIO.PWM(self.Motor2['EN'], 100)
self.EN3 = GPIO.PWM(self.Motor3['EN'], 100)
self.EN4 = GPIO.PWM(self.Motor4['EN'], 100)
2021-04-03 22:41:40 +00:00
self.EN1.start(0)
self.EN2.start(0)
2021-04-04 19:59:30 +00:00
self.EN3.start(0)
self.EN4.start(0)
2021-04-04 15:42:34 +00:00
# stop signals for motors and alarm
2021-04-04 03:08:57 +00:00
self.motorStop=True
self.alarmStop=True
self.cameraOff=True
2021-04-03 22:41:40 +00:00
2021-04-04 15:42:34 +00:00
# countor for theaf picture been taken
self.counter = self.firebase.database().child("signal").child(1).child("counter").get().val()
#print(str(self.counter))
2021-04-04 15:42:34 +00:00
2021-04-03 22:41:40 +00:00
# new update motor and alarm functions, are able to connect embedded system throught firebase
def start_motor(self):
2021-04-04 03:08:57 +00:00
self.motorStop=False
self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
2021-04-04 19:59:30 +00:00
self.EN3.ChangeDutyCycle(50)
self.EN4.ChangeDutyCycle(50)
2021-04-04 03:26:50 +00:00
GPIO.output(self.Motor1['input1'], GPIO.LOW)
GPIO.output(self.Motor1['input2'], GPIO.HIGH)
2021-04-04 03:08:57 +00:00
GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
2021-04-04 19:59:30 +00:00
GPIO.output(self.Motor3['input1'], GPIO.LOW)
GPIO.output(self.Motor3['input2'], GPIO.HIGH)
GPIO.output(self.Motor4['input1'], GPIO.HIGH)
GPIO.output(self.Motor4['input2'], GPIO.LOW)
2021-04-04 03:08:57 +00:00
print("motor is turned on")
2021-04-03 22:41:40 +00:00
def stop_motor(self):
2021-04-04 03:08:57 +00:00
print("stopping motor...")
self.motorStop=True
self.EN1.ChangeDutyCycle(0)
self.EN2.ChangeDutyCycle(0)
2021-04-04 19:59:30 +00:00
self.EN3.ChangeDutyCycle(0)
self.EN4.ChangeDutyCycle(0)
2021-04-04 03:08:57 +00:00
print("motor stops")
2021-04-03 22:41:40 +00:00
def start_alarm(self):
2021-04-04 03:08:57 +00:00
print("Alarm is turned on")
self.alarmStop=False
GPIO.output(26,True)
return False
2021-04-03 22:41:40 +00:00
def stop_alarm(self):
2021-04-04 03:08:57 +00:00
print("Alarm turning off...")
self.alarmStop=True
GPIO.output(26,False)
print("Alarm is off")
return True
def kill_target(self, target):
cmd_run="ps aux | grep {}".format(target)
out=os.popen(cmd_run).read()
for line in os.popen("ps ax | grep "+target+" | grep -v grep"):
fields = line.split()
#print(fields)
pid = fields[0]
a = os.kill(int(pid),signal.SIGKILL)
print('Killed PID %s, return value:%s' % (pid, a))
2021-04-03 22:41:40 +00:00
2021-04-04 03:08:57 +00:00
def start_camera(self):
self.cameraOff=False
print("Remote camera is turned on")
os.system('python3 remote_camera.py &')
2021-04-03 22:41:40 +00:00
2021-04-04 03:08:57 +00:00
def stop_camera(self):
self.cameraOff=True
self.kill_target("remote_camera.py")
print("Remote camera is off")
#return False
2021-04-04 15:42:34 +00:00
def distance(self):
# set Trigger to HIGH
GPIO.output(self.GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(self.GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(self.GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(self.GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
2021-04-03 22:41:40 +00:00
if __name__=="__main__":
#print("Execute function...")
2021-04-04 15:42:34 +00:00
car=Car()
2021-04-03 22:41:40 +00:00
while True: # turn on the system forever
2021-04-04 15:42:34 +00:00
# get connection to firebase
database = car.firebase.database()
2021-04-04 03:08:57 +00:00
signals = database.child("signal")
2021-04-04 15:42:34 +00:00
auth = car.firebase.auth()
storage = car.firebase.storage()
# get signal from firebase
2021-04-04 03:08:57 +00:00
motorSignal = signals.child(1).child("motor").get().val()
signals = database.child("signal")
alarmSignal = signals.child(1).child("alarm").get().val()
signals = database.child("signal")
cameraSignal = signals.child(1).child("camera").get().val()
signals = database.child("signal")
powerSignal = signals.child(1).child("power").get().val()
2021-04-04 15:42:34 +00:00
# get distance data from distance sensor
dist = car.distance()
2021-04-04 20:58:29 +00:00
#print ("Measured Distance = %.1f cm" % dist)
2021-04-04 15:42:34 +00:00
# Turn on motor if get sensor signal
2021-04-04 20:58:29 +00:00
if ((motorSignal=="on" and dist>15) and car.motorStop):
2021-04-04 03:08:57 +00:00
car.start_motor()
2021-04-04 20:49:52 +00:00
elif ((motorSignal=="off" or dist<=10) and not car.motorStop):
2021-04-04 15:42:34 +00:00
# Stop the motor if the vehicle is too close to the item at front as well
2021-04-04 03:08:57 +00:00
car.stop_motor()
2021-04-04 15:42:34 +00:00
# Turn on alarm if get sensor signal
2021-04-04 03:08:57 +00:00
if (alarmSignal=="on" and car.alarmStop):
car.start_alarm()
elif (alarmSignal=="off" and not car.alarmStop):
car.stop_alarm()
2021-04-04 15:42:34 +00:00
# Turn on remote camera if get sensor signal
if (cameraSignal=="on" and car.cameraOff and powerSignal=="off"):
2021-04-04 03:08:57 +00:00
car.start_camera()
elif ((cameraSignal=="off" and not car.cameraOff) or powerSignal =="on"):
2021-04-04 15:42:34 +00:00
car.stop_camera()
# Take a picture of someone or some thing try to get close to the vehicle
if (dist<=15 and car.cameraOff and car.motorStop and car.alarmStop):
time.sleep(5)
dist = car.distance()
if (dist<=15):
print('Take a theaf picture due to distance at ' + str(int(dist)) + 'cm')
camera = PiCamera()
camera.start_preview()
# Camera warm-up time
time.sleep(1)
camera.capture('/home/pi/Vehicle-Anti-Theft-Face-Recognition-System/sensor/picture'+str(car.counter)+'.jpg')
camera.close()
storage.child('Photos_of_Thieves/Thief_Sensor/picture'+str(car.counter)+'.jpg').put('/home/pi/Vehicle-Anti-Theft-Face-Recognition-System/sensor/picture'+str(car.counter)+'.jpg')
car.counter+=1
car.firebase.database().child("signal").child(1).child("counter").set(car.counter)
2021-04-04 19:59:30 +00:00
time.sleep(1)