Skip to content

alpr speed.py Process speed images with OPENALPR Automatic License Plate Reader

Claude Pageau edited this page Mar 7, 2021 · 16 revisions

How to Use OPENALPR with speed-camera

Note This feature is just for demonstration purposes and you will need to do more work to suit your needs.

The sample alpr-speed.py script will read speed-cam.py image paths from sqlite3 database entries. It will then use openalpr to search for license plate numbers in the speed-cam.py images per the data/speed-cam.db sqlite database entries. This method takes advantage of speed-cam.py motion tracking to only process potential candidates for real moving vehicles or objects.

You will need to configure openalpr to suit your needs eg country and regions Etc. As each image is processed the speed_cam.db speed table status field will be updated to 'none' or plate infor. image is only processed once.

For more details regarding OPENALPR See
Github repo at https://github.com/openalpr/openalpr
and OpenAlpr Docs at http://doc.openalpr.com/

Note

When using speed camera for openalpr purposes the speed settings will most likely not be needed and motion tracking will only be used for triggering image for license plate capture purposes. It is suggested you set speed-cam.py config.py image resolution WIDTH and HEIGHT to 640x480 with image_bigger = 1.0

This script will print out the license plates info. It will also add a license plate data status column to the data/speed_cam_db sqlite3 database. This demo code is still under development. It is also possible to add code to save results to another sqlite database table, csv or other file.

Installation

I installed openalpr on RPI's per

sudo apt-get install python-openalpr
sudo apt-get install python3-openalpr  
sudo apt-get install openalpr install openalpr-daemon
sudo apt-get openalpr-utils libopenalpr-dev

sudo apt-get install sqlite3

I Also needed to create symbolic link per below but this may be due to version that was loaded

sudo ln -s /usr/share/openalpr/runtime_data/ocr/tessdata/lus.traineddata /usr/share/openalpr/runtime_data/ocr/lus.traineddata

Configure ALPR settings in the alpr-speed.py file

ALPR settings are store in the alpr-speed.py python file. You will need to edit this script to change the settings to your country/state province etc. Default is us on (Ontario Canada). See list of alpr-speed.py variables below

cd ~ speed-camera
nano alpr-speed.py

Edit the User variable section per sample below. For more information about openalpr see https://github.com/openalpr/openalpr

#=================
# User Variables
#=================
VERBOSE_ON = True
DB_FILE = '/home/pi/speed-camera/data/speed_cam.db' # path to speed cam database
SPEED_DIR = '/home/pi/speed-camera'   # path to speed-camera folder
WAIT_SECS = 30  # seconds to wait between queries for images to process

ALPR_COUNTRY = "us"   # Country Code eg 'us', 'eu' See ALPR Docs
ALPR_REGION = "on"   # State/province Etc  See ALPR Docs
ALPR_TOP_N = 3        # Max Number of plates to search per image

# File path to ALPR configuration file per ALPR docs
ALPR_CONF_PATH = "/etc/openalpr/openalpr.conf"
# Directory path to ALPR runtime_data per ALPR docs
ALPR_RUNTIME_DATA_PATH = "/usr/share/openalpr/runtime_data"

ctrl-x y to save changes and exit nano.

How To Run Script

Configure speed-cam.py using config.py and set camera width height to 640x480 resolution and image_bigger = 1.0 Since the camera will be positioned to read license plates the speed calibration can be ignored.

Make sure openalpr is installed and can be run. Collect speed camera images with license plate(s) showing on the images. The image entries will be added to the data/speed_cam.db database file. The status column of the speed table will store the processing status of alpr-speed.py

Open SSH or terminal session and run alpr-speed.py per

cd ~/speed-camera
./alpr-speed.py

Processing status will be displayed as images are read. Any license data that is found will be displayed on the screen.
Note this script is for demonstration purposes and you may need to customize to suit your needs.

How to Query Sqlite3 DB for Plate data

Run alpr-speed.py until plate data is found. This can be viewed on the webserver images. If plates data has been found you can query the data/speed_cam.db database to display plate information. Note there may be multiple plate data per image.

Example query for licence plate data in the sqlite3 database.

cd ~/speed-camera
sqlite3 data/speed_cam.db

Perform the following query in the sqlite console

SELECT status, image_path 
FROM speed 
WHERE status NOT NULL;

or

SELECT status, image_path 
FROM speed 
WHERE status LIKE 'Plate%'

ctl-d to exit sqlite3 console