MikeLud1
IPCT Contributor
Try thisAnd yet one more request for help. I've got a list of plates and images that I would like to bring into the ALPR database. I'm trying to write a python program to call the API, but I'm apparently not clear on the format. Here's what I tried and got a 500 error.
import urllib.parse
import urllib.request
url = ""
header={"x-api-key" : 'a7f1bc2f790dfc6959...(redacted)...'}
post_param = urllib.parse.urlencode({
'plate_number' : '9MQC052',
'Image' : 'Alerts/Cam3a.20240611_123315_45027682.jpg',
'Camera' : 'LPR',
'timestamp' : '2024-06-11T19:33:17Z'
}).encode('UTF-8')
req = urllib.request.Request(url, post_param, header)
response = urllib.request.urlopen(req)
print(response.read())
Python:
import requests
import base64
from datetime import datetime, timezone
# File path for the image
image_file_path = 'test.jpg'
try:
# Open the image file in binary mode and encode it as Base64
with open(image_file_path, 'rb') as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
# Define the URL and headers
url = "http://192.168.1.21:3000/api/plate-reads"
headers = {
"x-api-key": "a7f1bc2f790dfc6959...(redacted)...",
"Content-Type": "application/json",
}
# Create the JSON payload
payload = {
"plate_number": "9MQC052",
"Camera": "LPR",
"timestamp": datetime.now(timezone.utc).isoformat(), # Use timezone-aware datetime
"Image": encoded_image
}
# Send the POST request
response = requests.post(url, headers=headers, json=payload)
# Check the response
print(response.status_code)
print(response.text)
except FileNotFoundError:
print(f"The file '{image_file_path}' does not exist.")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")