LPR setup guide using openALPRd (open-source install - FREE option)

reflection

Getting comfortable
Joined
Jan 28, 2020
Messages
348
Reaction score
261
Location
Virginia
There is a lot of information out there so I wanted to consolidate the information that I used to get it working. Hope this helps others. I'll edit the first post as needed based on feedback. This is an ongoing project so I'll update this guide as necessary to improve results.

This guide is for an ubuntu linux install of alprd (alprd runs as a linux daemon process) and the camera used was a Dahua IPC-HFW5241E-Z12E. I already had an ubuntu VM running as my Plex server so I used that. alprd should run fine alongside Plex.

You've got your camera setup in terms of location, zoom, focus. There are many posts and tips and everyone's location will be different. Once you are happy with your settings for both day and night, you are ready for this guide. Your day/night profiles should have custom exposure, shutter, gain, etc as appropriate for your environment..
Screen Shot 2020-09-19 at 1.00.46 PM.png

0) Here are some tips that I used to dial-in my camera.
  • Camera Configuration — openalpr 2.8.101 documentation
  • These tips on the settings for this camera worked best for me: LPR with a pair of IPC-HFW5241E-Z12E
  • more good tips here: IPC-HFW5231E-Z12 LPR from 137 feet
  • location, location, location. This is the most part that contributes to successful ALPR, but of course you do the best with what you have. My spot has great light in the mornings but a glare in the afternoons. One way for me to get around this would be to install a second camera in the opposite direction (which I can't do because of a tree). Your mileage may vary.
  • I may setup a 3rd manual setting during the afternoon to go in B/W mode during the afternoon glare to see if that helps.
  • Make sure you can see your plates fine in the captured image/video with your eyes before you attempt to use any type of ALPR.

Screen Shot 2020-09-19 at 1.50.56 AM.pngScreen Shot 2020-09-19 at 1.51.16 AM.png
1) You will want your camera to switch between night and day modes at sunset and sunrise, respectively. You also want your pre-determined zoom and focus settings to get set after the night/day switchover. Because your camera will have a hard time auto-focusing when it's dark, we need to manually set it. This script will tell your camera to adjust its settings to what you've defined at the sunset/sunrise. Place this script on your ubuntu box, edit the settings for your environment, and setup a cron job for this to run around midnight each day. Note: the URL to get your current settings is in the script.

CameraDayNight.sh
Bash:
#!/bin/bash

# By reflection@IPcamtalk
# This script will switch between Day/Night modes at Sunrise/Sunset based on your location.
# Fill in the variables for your location (lat and lng)
# Fill in the variables for username and password
# Fill in the variables for your zoom and focus settings (for both day and night)
# This has been tested and works for Dahua IPC-HFW5241E-Z12E

# Edit this lat and lng based on your location
lat=38.283291
lng=-83.489213

# set your camera's IP address, username, password
cameraIP=192.168.x.x
username=userABC
password=*******

# find your desired zoom and focus using this URL (you will need to do this for day and night):
# http://<username>:<password>@<cameraIP>/cgi-bin/devVideoInput.cgi?action=getFocusStatus

# set your night zoom and focus (based on values from above)
nightzoom=1.000000
nightfocus=0.193333

# set your day zoom and focus (based on values from above)
dayzoom=1.000000
dayfocus=0.153667

# Get sunrise info
sunriseUTC=$(wget -qO- https://api.sunrise-sunset.org/json?lat=$lat\&lng=$lng\&date=today | sed "s/^.*sunrise\":\"/" | sed "s/\(^.\{1,11\}\).*/\1/" )
sunrise=$(date -d "$sunriseUTC UTC" +%I:%M%p)

# Get sunset info
sunsetUTC=$(wget -qO- https://api.sunrise-sunset.org/json?lat=$lat\&lng=$lng\&date=today | sed "s/^.*sunset\":\"/" | sed "s/\(^.\{1,11\}\).*/\1/" )
sunset=$(date -d "$sunsetUTC UTC" +%I:%M%p)


# Display sunrise and sunset values
echo "Sunrise for location $location: $sunrise"
echo "Sunset for location $location: $sunset"

# Switch to Day mode at sunrise
echo "curl -k --digest https://$username:$password@$cameraIP/cgi-bin/configManager.cgi?action=setConfig\&VideoInMode[0].Config[0]=0 ; sleep 30 ; curl -k --digest https://$username:$password@$cameraIP/cgi-bin/devVideoInput.cgi?action=adjustFocus\&focus=$dayfocus\&zoom=$dayzoom" | at -M $sunrise + 30 minutes

# Switch to Night mode at sunset
echo "curl -k --digest https://$username:$password@$cameraIP/cgi-bin/configManager.cgi?action=setConfig\&VideoInMode[0].Config[0]=1 ; sleep 30 ; curl -k --digest https://$username:$password@$cameraIP/cgi-bin/devVideoInput.cgi?action=adjustFocus\&focus=$nightfocus\&zoom=$nightzoom" | at -M $sunset - 20 minutes
2) Open-source alprd only supports MJPEG, so you have two options.
  • a) you can use the substream on the Dahua IPC-HFW5241E-Z12E. Unfortunately the max resolution is D1 (703x408). This may work for you if you have a good camera location/angle. This did not work for me because my angle was about 30degrees. I can visually see the plates fine, but alprd had difficulty recognizing plates. This is one gripe I have about the 5241, its MPJEG substream max's out at D1 (even my old IP cam could do a 1080P MPJEG substream).
  • b) use Blue Iris to convert the mainstream to MJPEG. This is what I did. My results were significantly better with a 720P stream than the D1 stream.

3) Install alprd using the guide here: OpenALPR - A How-To on Ubuntu 16.04 Desktop. This worked for me on my ubuntu 18.04 box.
  • My existing linux box was an ubuntu 18.04 VM that I used for Plex. I have two vCPU and 8GB ram allocated to it. With Plex, the CPU is runs cool. With alprd, my CPU gets pegged at 100%. I have have iGPU passthrough set for Plex which works great, but not sure that alprd is using the iGPU.
  • the MJPEG stream can be accessed via this URL: "https://<username>:<password>@<cameraIP>/cgi-bin/mjpg/video.cgi?channel=1&subtype=1". This needs to be placed in your alprd.conf file.
  • The URL to stream from Blue Iris is: http://<BI-server>:<port>/mjpg/<short-camera-name>/video.mjpg. Some settings to note if you go this route. I was not able to embed the username/password into the URL, so I went to Settings->Web Server->Advanced, and added the IP address of my linux box. Also, for Encoder options, make sure you bump up quality to 100% and enable Intel QSV. I also turned off Zero frame latency. I wanted my MJPEG stream to be as close to the original H.264 stream as possible. (note: I used H.264 because there is less compression which hopefully will produce a clearer image when transcoded into MJPEG - not sure if it does but figured why not?)
Screen Shot 2020-09-19 at 2.00.55 AM.png
4) Calibrate alprd using the guide here: Open Source — openalpr 2.8.101 documentation.
  • make sure your sample image is from your the feed you are using. In other words, the D1 feed from the camera, or the 720P/1080P feed from blue iris.
  • adjust your image until you are able to get the plate to be horizontal and the right aspect ratio (left-click and draw the red rectangle as your verification).
  • check to your settings by running alpr manually on your test images to see if the "confidence" level is better after your calibration. If you have a good camera angle, you probably don't need to calibrate.

5) You should start to see some results from your ALPR server (after step 3). Might look something like this:
aplrd-web.jpg

6) If your CPU is too high because of ALPRd, adjust your fps based on the speed of the vehicles. At 20mph, a vehicle will travel 30ft in a second. So at 10fps, you will get an image every 3ft. The higher the fps, the more processing required for alprd. Adjust it as necessary based on your CPU utilization.

7) If you have focus drift, you can use the following script. Set a cron job to run every 5-10 minutes. I found that my focus may change during the day when someone is walking their dog past the camera view.

Bash:
#!/bin/bash

# By reflection@IPcamtalk
# This script will adjust your focus in case there has been some drift.
# If you have different focus settings for day and night, set the dayfocus and nightfocus variables accordingly.
# Fill in the variables for your location (lat and lng)
# Fill in the variables for username and password
# Fill in the variables for your zoom and focus settings (for both day and night)
# This has been tested and works for Dahua IPC-HFW5241E-Z12E (the focus API)

# Edit lat and lng based on your location

lat=37.483844
lng=-83.439173

# set your camera's IP address, username, password
cameraIP=192.168.x.x
username=<username>
password=<password>

# find your desired zoom and focus using this URL (you will need to do this for day and night):
# http://<username>:<password>@<cameraIP>/cgi-bin/devVideoInput.cgi?action=getFocusStatus

# set your night zoom and focus
nightzoom=1.000000
nightfocus=0.186111

# set your day zoom and focus
dayzoom=1.000000
dayfocus=0.150000

# Get sunrise info
sunriseUTC=$(wget -qO- https://api.sunrise-sunset.org/json?lat=$lat\&lng=$lng\&date=today | sed "s/^.*sunrise\":\"/" | sed "s/\(^.\{1,11\}\).*/\1/" )
sunrise=$(date -d "$sunriseUTC UTC" +%k%M)

# Get sunset info
sunsetUTC=$(wget -qO- https://api.sunrise-sunset.org/json?lat=$lat\&lng=$lng\&date=today | sed "s/^.*sunset\":\"/" | sed "s/\(^.\{1,11\}\).*/\1/" )
sunset=$(date -d "$sunsetUTC UTC" +%k%M)

# Display sunrise and sunset values
echo "Sunrise for location $location: $sunrise"
echo "Sunset for location $location: $sunset"

CurrentTime=$(date +%k%M)

CurrentFocus=$(wget --no-check-certificate -qO - https://$username:$password@$cameraIP/cgi-bin/devVideoInput.cgi?action=getFocusStatus | grep "Focus=" | sed "s/status.Focus=/" | sed "s/\(^.\{1,8\}\).*/\1/" )

echo "Current focus: $CurrentFocus"
echo "Current time: $CurrentTime"

if [ $CurrentTime -gt 0 -a $CurrentTime -lt $sunrise ]; then
    echo "time is before sunrise"
    if [ $CurrentFocus != $nightfocus ]; then
        echo "fixing night focus"
            curl -k --digest https://$username:$password@$cameraIP/cgi-bin/devVideoInput.cgi?action=adjustFocus\&focus=$nightfocus\&zoom=$nightzoom
    fi
  elif [ $CurrentTime -gt $sunrise -a $CurrentTime -lt $sunset ]; then
           echo "time is daytime"
           if [ $CurrentFocus != $nightfocus ]; then
              echo "fixing day focus"
                  curl -k --digest https://$username:$password@$cameraIP/cgi-bin/devVideoInput.cgi?action=adjustFocus\&focus=$dayfocus\&zoom=$dayzoom
           fi
             elif [ $CurrentTime -gt $sunset -a $CurrentTime -lt 2400 ]; then
                   echo "time is after sunset"
                   if [ $CurrentFocus != $nightfocus ]; then
                  echo "fixing night focus"
                      curl -k --digest https://$username:$password@$cameraIP/cgi-bin/devVideoInput.cgi?action=adjustFocus\&focus=$nightfocus\&zoom=$nightzoom
                   fi
fi
 
Last edited:

reflection

Getting comfortable
Joined
Jan 28, 2020
Messages
348
Reaction score
261
Location
Virginia
I added a new script (item 7) to the post that will adjust your focus in case it drifts.
 

biggen

Known around here
Joined
May 6, 2018
Messages
2,563
Reaction score
2,837
This is a wealth of information here! Thank you for posting this.

I'm going to give this a try again. I messed with it a few months ago but wasn't able to finish it with a Debian VM. One of the php modules (mcrypt) is depreciated and not available on newer Ubuntu or Debian installations. I need to spin up an older Ubuntu VM and try it with that.

I see you are scraping sunrise/sunset time. I was in the process of writing some code like you did until I thought surely someone has already done this, right? Then I found a cool program called sunwait does just that and gets your sunrise/sunset times for any lat/long you feed it and then can execute a command based off of those times. So I created a simple bash cron job script that executes the sunwait program for the camera day/night change. Here is my cron job for switching the LPR over to night time 30 minutes prior to sunset.

Bash:
# LPR Cam Night Switch - 30 Minutes before Sunset
0 16 * * * /home/user/sunwait/sunwait wait set offset +00:30 30.19N 85.79W ; wget -O - --user admin --password password "10.200.200.12:65002/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1
 
Last edited:

reflection

Getting comfortable
Joined
Jan 28, 2020
Messages
348
Reaction score
261
Location
Virginia
I updated the mode switching script (item 1 above) to be more friendly to our international friends. Instead of zip code, it uses longitude and latitude for the location.
 

reflection

Getting comfortable
Joined
Jan 28, 2020
Messages
348
Reaction score
261
Location
Virginia
This is a wealth of information here! Thank you for posting this.

I'm going to give this a try again. I messed with it a few months ago but wasn't able to finish it with a Debian VM. One of the php modules (mcrypt) is depreciated and not available on newer Ubuntu or Debian installations. I need to spin up an older Ubuntu VM and try it with that.

I see you are scraping sunrise/sunset time. I was in the process of writing some code like you did until I thought surely someone has already done this, right? Then I found a cool program called sunwait does just that and gets your sunrise/sunset times for any lat/long you feed it and then can execute a command based off of those times. So I created a simple bash cron job script that executes the sunwait program for the camera day/night change. Here is my cron job for switching the LPR over to night time 30 minutes prior to sunset.

Bash:
# LPR Cam Night Switch - 30 Minutes before Sunset
0 16 * * * /home/user/sunwait/sunwait wait set offset +00:30 30.19N 85.79W ; wget -O - --user admin --password password "10.200.200.12:65002/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1
That program can definitely come in handy. Thanks for sharing. I still need to scrape for the sunrise and sunset for my focus drift script because I'm comparing current time. But for the switching modes, your way is nice.
 
Joined
Aug 8, 2018
Messages
7,415
Reaction score
26,000
Location
Spring, Texas
I already had an ubuntu VM running as my Plex server so I used that.
I have been following this and think this is great.

I know you are using VMs, but I do not have that ability. As I do not have a Linux box in my installation, I was thinking of making one and wonder if any of you know of one that would easily run this setup, and would be a 1U or 2U rack mountable unit.
 

reflection

Getting comfortable
Joined
Jan 28, 2020
Messages
348
Reaction score
261
Location
Virginia
@samplehold, I have 2 vCPU @3.5MHz allocated and my server is running at about 75% cpu utilization. I would guess that most 4 core processors would work. You don't need that much RAM. I allocated 8GB ram. You don't need much storage either. If you have an extra laptop, you could try it on that first.

Let me do a quick test for you. I'm going to clone my VM onto a bigger server and give it more resources and see if utilization goes down or it just consumes whatever you give it.
 

biggen

Known around here
Joined
May 6, 2018
Messages
2,563
Reaction score
2,837
@samplenhold You could always get an older Dell/HPE 1U or 2U server off eBay. Problem is they can be kinda noisy so I don't know if that is an issue for you or not.

A lot of Proxmox users are using NUCs. The new ones pack a lot of firepower inside those tiny little units.
 
Last edited:

reflection

Getting comfortable
Joined
Jan 28, 2020
Messages
348
Reaction score
261
Location
Virginia
I just install alprd on a fresh install of ubuntu 20.04. There are some adjustments you need to make:

- pip is no longer in the 20.04 distribution, so you have to install it a different way.
Bash:
sudo apt-get install python2.7
curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
sudo python2.7 get-pip.py
- mysql-python is not available. Instead, use pymysql
Bash:
pip install pymysql
- I used root to login to phpmyadmin. To enable root access: (in this example 'test' is the password)
Bash:
sudo mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';
- edit the Test.pyw script and change the following:
Bash:
#the commented lines are the ones you need to replace
#import MySQLdb
import pymysql

#db = MySQLdb.connect("localhost", "root", "<password>","alprdb")
db = pymysql.connect("localhost", "root", "<password>","alprdb")
 

cjowers

Getting the hang of it
Joined
Jan 28, 2020
Messages
107
Reaction score
36
Location
AUS
Nice guide Reflection!

I used to run separate night/day zoom/focus settings like this, but then I started to worry that it would wear our my camera motors (IPC-HFW5231EP-ZE-0560). Any idea how long (non-ptz) cams last with 2 moves a day? I imagine your focus-only change is less wearing than a zoom+focus change would be though...

Anyone ever worn out a variable focus zoom camera to have some idea?

Thanks
 

reflection

Getting comfortable
Joined
Jan 28, 2020
Messages
348
Reaction score
261
Location
Virginia
@samplenhold , I did a fresh install of ubuntu 20.04 as a VM. This time on a more powerful machine (this is my desktop server, which has an i9-9900X with 128GB ram). I gave the VM 4vCPU and 16GB ram and did PCIe passthrough for the GPU so it has a dedicated Nvidia 2080 Super for it. BI is feeding the same stream to both ALPRd servers.

4 cores definitely helps. I don't know how to get alprd to use the GPU. If someone finds a way, I'm all ears. It appears that alprd consumes all the resources I gave it.

here is the mtop output for both systems (one with 2vCPU and the other with 4vCPU)

Screen Shot 2020-09-21 at 7.45.39 PM.png

Screen Shot 2020-09-21 at 8.11.55 PM.png

For fun, I cloned my ubuntu 20.04 VM twice, this time with 8 vCPU and then 16 vCPU.
Screen Shot 2020-09-21 at 8.01.28 PM.png

this is with 16vCPU
Screen Shot 2020-09-21 at 8.04.44 PM.png

The total CPU% is around 105-115% each time.
 
Last edited:
Top