· 4 years ago · Feb 20, 2021, 09:56 AM
1#!/usr/bin/python
2import base64
3import requests
4import json
5
6SECRET_KEY = 'sk_2d4b98d2744d849d162fb6eb'
7REGION = 'eu'
8
9# IMAGE_PATH = 'http://plates.openalpr.com/ea7the.jpg'
10# img_base64 = base64.b64encode(requests.get(IMAGE_PATH).content)
11
12
13def get_from_url(image_path):
14 img_base64 = base64.b64encode(requests.get(image_path).content)
15 license_plate_recognition(img_base64)
16
17
18def get_from_file(image_path):
19 with open(image_path, 'rb') as image_file:
20 img_base64 = base64.b64encode(image_file.read())
21 license_plate_recognition(img_base64)
22
23
24def license_plate_recognition(base64_img):
25 url = 'https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country={region}&secret_key={secretKey}'\
26 .format(region=REGION, secretKey=SECRET_KEY)
27 r = requests.post(url, data = base64_img)
28 print(json.dumps(r.json(), indent=2))
29 print(url)
30
31
32get_from_file("/home/pi/Pictures/carwithplates.jpg")
33
34
35# DB connection
36
37import mariadb
38import sys
39
40# Connect to MariaDB Platform
41try:
42 conn = mariadb.connect(
43 user="danielfiko",
44 password="hunter2",
45 host="127.0.0.1",
46 port=3306,
47 database="test"
48
49 )
50except mariadb.Error as e:
51 print('Error connecting to MariaDB Platform: {e}', e)
52 sys.exit(1)
53
54# Get Cursor
55cur = conn.cursor()
56
57skiltnummer = "AX990022";
58img_path = "/bilder/skilt.png";
59
60cur.execute("INSERT INTO VehicleEntries (license_plate, image_path) VALUES ({skiltnummer}, {img_path})",
61 skiltnummer, img_path)
62cur.execute("SELECT img_path FROM VehicleEntries WHERE id=1")
63myresult = cur.fetchall()
64
65print(myresult[0][0])
66
67conn.close()