Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/webhooks/webhook_lambda/lambda_function.py
1072 views
1
import base64
2
import json
3
from urllib.parse import unquote_plus
4
5
6
def print_vehicle_info(results):
7
for result in results:
8
print("Vehicle detected:")
9
print("Plate: ", result["candidates"][0]["plate"])
10
print("Color: ", result["color"][0]["color"])
11
print(
12
"Make - Model: ",
13
result["model_make"][0]["make"] + " - " + result["model_make"][0]["model"],
14
)
15
print("Orientation: ", result["orientation"][0]["orientation"])
16
17
18
def handle_payload_with_image(body, boundary):
19
# Split the body into parts using the boundary
20
parts = body.split(b"--" + boundary.encode("utf-8"))
21
22
# Filter out empty parts
23
parts = [part for part in parts if part.strip()]
24
25
# Process each part
26
for part in parts:
27
try:
28
# Separate headers and content
29
headers, content = part.split(b"\r\n\r\n", 1)
30
except ValueError:
31
# Skip if the part is not valid
32
continue
33
34
# Handle JSON data
35
if b"json" in headers:
36
data = json.loads(content.decode("utf-8"))
37
results = data["data"]["results"]
38
print_vehicle_info(results)
39
40
# Handle image
41
if b"upload" in headers:
42
# Add logic to handle image
43
pass
44
45
46
def handle_payload(body):
47
# Handle JSON data
48
if b"json" in body:
49
content = unquote_plus(body.split(b"json=")[1].decode("utf-8"))
50
data = json.loads(content)
51
results = data["data"]["results"]
52
print_vehicle_info(results)
53
54
55
def lambda_handler(event, context):
56
# Retrieve the raw request body from the Lambda event
57
body = base64.b64decode(event["body"])
58
59
# Extract content type and boundary from headers
60
content_type = event["headers"]["content-type"]
61
62
split_by_boundary = content_type.split("boundary=")
63
64
if len(split_by_boundary) > 1:
65
# There is image included in payload
66
boundary = split_by_boundary[1]
67
handle_payload_with_image(body, boundary)
68
69
else:
70
handle_payload(body)
71
72
# Return a response
73
response = {"statusCode": 200, "body": "Webhook processing successful"}
74
return response
75
76