with open(file, ‘rb’) as fp: #request for Codeproject.AI
response = requests.post(
f’{CODE_PROJECT_HOST}/v1/image/alpr’,
files=dict(upload=fp),
)
plates = response.json() #respons from server
plate = None
plateM = “No plate”
#check if there is a plate
if len(plates[“predictions”]) > 0 and plates[“predictions”][0].get(“plate”):
plate = str(plates[“predictions”][0][“plate”]).replace(" ", “”)
score = plates[“predictions”][0][“confidence”]
#if there is a plate write the plate and the score of detection
with open(“/config/python_scripts/detected_plates.log”, “a”) as log:
log.write(f"[{datetime.datetime.now()}]: Camera - detected {plate} with a score of {score}\n")
plateM = str(plate)
#if there is no plate write a log that there was no plate
with open(“/config/python_scripts/detected_plates.log”, “a”) as log:
log.write(f"[{datetime.datetime.now()}]: - No plates detected in run\n")
#-mqtt broker
broker = ‘192.168.2.180’
port = 1883
topic = “python/mqtt” #Generate a Client ID with the publish prefix.
client_id = f’publish-{random.randint(0, 1000)}’
username = ‘xxx’#insert mqtt username
password = ‘xxx’#insert mqtt password
#connection to mqtt broker
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(“Connected to MQTT Broker!”)
else:
print(“Failed to connect, return code %d\n”, rc)
#what to publish
def publish(client):
time.sleep(0)
msg = str(plateM)
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send {msg} to topic {topic}“)
else:
print(f"Failed to send message to topic {topic}”)
#run the process of publishing
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
client.loop_stop()
run()
Automation that will open the gate for known plates
Folder python_scripts not found in configuration folder It’s here “/homeassistant/config/python_scripts/license_plate.py”, so why doesnt it work? fixed.
Q2: By Payload 1234 you mean the license plate, right?
Q3: service: shell_command.check_license_plate, it cannot found that, are you not missing something?
import datetime
import requests
import random
import time
from paho.mqtt import client as mqtt_client
file = "/config/www/plate.jpg" # Your file - picture - to be checked
CODE_PROJECT_HOST = "http://192.168.1.XX:32168" # Your Codeproject host
with open(file, 'rb') as fp: # request for Codeproject.AI
response = requests.post(
f'{CODE_PROJECT_HOST}/v1/image/alpr',
files=dict(upload=fp),
)
plates = response.json() # response from server
plate = None
plateM = "No plate"
# check if there is a plate
if len(plates["predictions"]) > 0 and plates["predictions"][0].get("plate"):
plate = str(plates["predictions"][0]["plate"]).replace(" ", "")
score = plates["predictions"][0]["confidence"]
# if there is a plate write the plate and the score of detection
with open("/config/python_scripts/detected_plates.log", "a") as log:
log.write(f"[{datetime.datetime.now()}]: Camera - detected {plate} with a score of {score}\n")
plateM = str(plate)
# if there is no plate write a log that there was no plate
else:
with open("/config/python_scripts/detected_plates.log", "a") as log:
log.write(f"[{datetime.datetime.now()}]: - No plates detected in run\n")
# -mqtt broker
broker = '192.168.1.XX'
port = 1883
topic = "python/mqtt"
# Generate a Client ID with the publish prefix.
client_id = f'publish-{random.randint(0, 1000)}'
username = 'XX' # insert mqtt username
password = 'XX' # insert mqtt password
# connection to mqtt broker
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
# what to publish
def publish(client):
time.sleep(0)
msg = str(plateM)
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send {msg} to topic {topic}")
else:
print(f"Failed to send message to topic {topic}")
# run the process of publishing
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
client.loop_stop()
run()
Thanks for the tutorial, got everythin installed (Snapshot from cam i stored correct and Codeproject.ai is on my Synology with Docker up an running) and configured exept the
And the python-script? /config/python_scripts/license_plate.py
this works
action: python_script.license_plate
data: {}
Got the shell running, now this message occurs:
Die Verarbeitung der zurückgegebenen Aktionsantwortdaten ist fehlgeschlagen. Es wurde ein Dictionary erwartet, aber es wurde <class ‘NoneType’> erhalten.
The processing of the returned action response data has failed. A dictionary was expected, but <class ‘NoneType’> was received.