Hi Marcus. Here is my Python script. It watches for beanstalk jobs, strips out useful info, checks plates against a white list .csv and opens a gate if there is a match. Then records all detected plates in a .csv plates file. If there are duplicate plates, it attempts to delete pictures to save space on the Dropbox.
Hope it helps:-)
Steve
#!/usr/bin/python
import beanstalkc
import datetime
import json
import os
import time
import csv
import sys
#start openalpr
os.system("/usr/bin/alprd & ")
#fetch whitelist of plates
f = open('/home/anpr/Dropbox/anpr-whitelist/whitelist.csv')
csv_f = csv.reader(f)
whitelist = []
for row in f:
whitelist.append (row.strip())
print whitelist
#connect to beanstalk
beanstalk = beanstalkc.Connection(host='localhost', port=11300)
beanstalk.watch('alprd')
def opengate():
print ("Opening gate.")
os.system("sudo echo -e '\xff\x01\x01' > /dev/ttyUSB0") #on
time.sleep(1)
os.system("sudo echo -e '\\xff\\x01\\x00' > /dev/ttyUSB0") #off
print ("Gate Opened")
time.sleep(45)
emptybeanstalk();
def emptybeanstalk():
print ("Emptying beanstalk.")
s = beanstalk.stats_tube("alprd")
ss = s['current-jobs-ready']
while (ss > 0):
bos = beanstalk.reserve()
bos.delete()
ss -= 1
beanstalk.watch('alprd')
print ("Beanstalk emptied.")
print ("Watching gate.")
def main():
emptybeanstalk();
lastplate = "AAAAAA"
while True:
job = beanstalk.reserve()
jobb = job.body
job.delete()
d = json.loads(jobb)
epoch = d['epoch_time']
# print d
realtime = time.strftime('%Y-%m-%d %H:%M:%S')
# realtime = datetime.datetime.fromtimestamp(epoch).strftime('%Y-%m-%d %H:%M:%S')
siteid = d['site_id']
cameraid = d['camera_id']
plate = d['results'][0]['plate']
confid = d['results'][0]['confidence']
uuid = d['uuid']
if plate in whitelist:
opengate()
if lastplate == plate:
time.sleep(1)
if os.path.exists("/home/anpr/Dropbox/anpr-pictures/" + str(uuid) + ".jpg"):
os.remove("/home/anpr/Dropbox/anpr-pictures/" + str(uuid) + ".jpg")
print ("duplicate pic removed - " + str(uuid) + ".jpg")
else:
lastplate = plate
s = (str(realtime) + ", " + str(plate) + ", " + str(confid) + ", " + str(uuid) + "\n")
plates = open('/home/anpr/Dropbox/anpr-plates/plates.csv', 'a')
plates.write(s)
plates.close()
print s
if __name__ == "__main__":
main()