· 4 years ago · Feb 20, 2021, 12:36 PM
1#!/usr/bin/python
2import base64
3import requests
4import json
5import mariadb
6import sys
7
8import RPi.GPIO as GPIO
9import time
10import os
11from datetime import datetime
12
13SECRET_KEY = ''
14REGION = 'eu'
15
16# IMAGE_PATH = 'http://plates.openalpr.com/ea7the.jpg'
17# img_base64 = base64.b64encode(requests.get(IMAGE_PATH).content)
18
19
20def get_from_url(image_path):
21 img_base64 = base64.b64encode(requests.get(image_path).content)
22 license_plate_recognition(img_base64)
23
24
25def get_from_file(image_path):
26 with open(image_path, 'rb') as image_file:
27 img_base64 = base64.b64encode(image_file.read())
28 license_plate_recognition(img_base64)
29
30
31def license_plate_recognition(base64_img):
32 url = 'https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country={region}&secret_key={secretKey}'\
33 .format(region=REGION, secretKey=SECRET_KEY)
34 r = requests.post(url, data = base64_img)
35 # print(r.json())
36 # print(json.dumps(r.json(), indent=2)
37 json_data = r.json()
38 json_data = json.dumps(json_data)
39 print(json_data["results"]["plate"])
40
41
42
43# Connect to MariaDB Platform
44try:
45 conn = mariadb.connect(
46 user="",
47 password="",
48 host="",
49 port=3306,
50 database=""
51
52 )
53except mariadb.Error as e:
54 print('Error connecting to MariaDB Platform: {e}', e)
55 sys.exit(1)
56
57# Get Cursor
58cur = conn.cursor()
59
60def db_insert_img(license_plate, img_path):
61 q = f"INSERT INTO entries (license_plate, img_path) VALUES ('{license_plate}', '{img_path}')"
62 cur.execute(q)
63 conn.commit()
64
65
66def db_check_license_plate(license_plate):
67 cur.execute(f"SELECT licence_plate FROM approved_vehicles WHERE licence_plate = '{license_plate}'")
68 result = cur.fetchall()
69 print(result[0][0])
70
71
72# db_check_license_plate("EL99017")
73# conn.close()
74
75
76button = 16
77led = 18
78
79
80
81
82def setup():
83 GPIO.setmode(GPIO.BOARD)
84 GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
85 GPIO.setup(led, GPIO.OUT)
86
87
88def loop():
89 while True:
90 button_state = GPIO.input(button)
91 if button_state == False:
92 GPIO.output(led, True)
93 now = datetime.now()
94 current_time = now.strftime("%H_%M_%S")
95 os.system(f'fswebcam -r 1280x720 --no-banner /home/pi/webcam/{current_time}.jpg')
96 get_from_file(f"/home/pi/webcam/{current_time}.jpg")
97 print('Picture taken...')
98 # os.system('sudo python trueFalseGPIO.py')
99 while GPIO.input(button) == False:
100 time.sleep(0.2)
101 else:
102 GPIO.output(led, False)
103
104def endprogram():
105 GPIO.output(led, False)
106 GPIO.cleanup()
107
108
109if __name__ == '__main__':
110 setup()
111 try:
112 loop()
113 except KeyboardInterrupt:
114 print('keyboard interrupt detected')
115 endprogram()
116
117