Free ALPR - Codeproject.AI

Since OpenALPR is not working for a long time(free). The new option for me is Codeproject.AI

  1. First of all install Codeproject.AI (If you install on windows don’t forget to open port 32168) and License Plate Reader addon

  2. Add to configuration.yaml

python_script:

  1. Connect with ssh to homeassistant and install python,requests for python, paho-mqtt for python

apk add python3
apk add py3-requests
apk add py3-paho-mqtt

  1. Do an automation to take snapshot from camera and run the shell command

action:

  • service: camera.snapshot
    target:
    device_id: 478fb96ef90cc5d3dd6d0888db7f627c
    data:
    filename: /config/www/plate.jpg
  • service: shell_command.check_license_plate
    data: {}
  1. Code for /config/python_scripts/license_plate.py

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.x.xxx:32168#Your 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() #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)

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()

  1. Automation that will open the gate for known plates

alias: Tablice
description: “”
trigger:

  • platform: mqtt
    topic: python/mqtt
    payload: 1234
  • platform: mqtt
    topic: python/mqtt
    payload: 2345
  • platform: mqtt
    topic: python/mqtt
    payload: 3456
  • platform: mqtt
    topic: python/mqtt
    payload: 4567
  • platform: mqtt
    topic: python/mqtt
    payload: 5678
    condition:
    action:
  • service: automation.trigger
    target:
    entity_id: automation.porton
    data:
    skip_condition: true
    mode: single

Happy new year

2 Likes

Hmm. Not working for me yet.

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?

Got the feeling this is incomplete :slight_smile: @Gregor_Tomazin

Hi,
you installed Codeproject in HA?