Anyone tried ANPR?

308 views
Skip to first unread message

Andrew Jones

unread,
May 20, 2017, 6:59:08 PM5/20/17
to motioneye
Has anyone had any luck integrating with Automatic Number Plate Recognition software to detect UK car number plates?

Andrew

Tim Donovan

unread,
Jun 7, 2017, 6:38:09 AM6/7/17
to motioneye
Clearly its possible (http://nevonprojects.com/raspberry-pi-vehicle-number-plate-recognition/) but I don't think they released the source. Also I think the hardware most people are running motioneye on (rasp pi's) will struggle to do OCR on top of running motioneye. 

skywatch1269

unread,
Jun 15, 2017, 6:18:30 AM6/15/17
to motioneye
Look into using openCV - they are using that for anpr and the code is on github I think.

IAm Orion

unread,
Jun 10, 2018, 9:52:13 AM6/10/18
to motioneye

I realise this is an old topic, BUT, I had the same exact thought...  I've done some testing...

it's not super fast but I did find a sloppy method of doing this!  (I've yet to code any actions based on the response other than printing out the results).

1) Visit https://cloud.openalpr.com (https://cloud.openalpr.com/cloudapi/) and sign up for a FREE account.  
This gives you 2000 free API Credits a month, the equivalent of about 64 lookups per day... or 32 if you want the vehicle details returned as well as just the reg

2) Make a bash script file, for example, in /data/output/Camera1/ name whatever you like.  For example: test.sh

3) Here's the code I used:

if [ -z "$1" ]
  then
    echo -e "\nNo argument supplied\n"
    exit 1
fi

file=$1

if [ -f "$file" ]
then
#echo "$file found."
python test.py
exit 1
else
echo "$file not found."
exit 1
fi


Replace YOUR_SECRET_KEY with the Secret Key provided in your API page (https://cloud.openalpr.com/cloudapi)



Replace YOUR_COUNTRY_CODE with your country code.  GB for Great Britain, EU for other European plates. Check their website for country codes available

If you use 'recognize_vehicle=1' - then it costs 2 API credits per search and will also return the vehicle make, model, colour etc etc.
If you use 'recognize_vehicle=0' - then it only costs 1 API credit and only returns the Number Plate.

The CURL link in the script has an argument - $1.  This would be the image file you want to send.  It's worth noting that in your file storage location, there is an alias called 'lastsnap.jpg' which is the last stored still image.

As an example, if my output folder was: /data/output/Camera1/

Then I would use:

./test.sh /data/output/Camera1/lastsnap.jpg

And it will therefore always send the most recent captured still image to be checked.

Once the results file is saved (CURL saves the output to 'results.json'), I then use some python to break it down - my 'test.py' file is as follows:

import json

from pprint import pprint

with open('results.json') as f:
    data = json.load(f)

#pprint(data)

if len(data['results']) > 0:
  
  print("\nLicense Plate: " + data['results'][0]['plate'])
  print("Vehicle: " + data['results'][0]['vehicle']['make_model'][0]['name'].title().replace("_"," "))
  print("Year: " + data['results'][0]['vehicle']['year'][0]['name'].title())
  print("Color: " + data['results'][0]['vehicle']['color'][0]['name'].title())
  print("\nCredits Used: " + str(data['credits_monthly_used']) + " out of " + str(data['credits_monthly_total']) + "\n")

else:

  print("\nNo vehicle data found")
  print("\nCredits Used: " + str(data['credits_monthly_used']) + " out of " + str(data['credits_monthly_total']) + "\n")

Please note

  1) This assumes you ARE using 'recognize_vehicle=1' and thus is coded specifically for that usage.
  2) I was originally trying to write a script in python using requests, but requests isn't installed, and you CAN'T install it if you're using the MotionEyeOS like I am.  I did try with httplib but failed with that too.  So I then made the bash script so I could just use CURL... the decoding of the json I had already written in python, hence why at this stage it's half bash (.sh file) and then half pyhon (.py file).

I will try and update my bash script so that it does all the work itself, and doesn't need a separate python file... it just so happened to be my progress at the time as I realised some stuff I couldn't do in python and couldn't install the needed add-ons (eg python-requests)

IAm Orion

unread,
Jun 10, 2018, 12:13:09 PM6/10/18
to motioneye
UPDATE:

(Updating from my earlier post...)

I've now written this entirely in python.

save this code as a python file, for example: anpr.py

# =======================================================
# Script Settings
# =======================================================

# Required Settings
# =======================================================

secret_key = 'YOUR_SERCRET_KEY' # Find your secret key in your CloudALPR API account
country_code = 'YOUR_COUNTRY_CODE'
recognize_vehicle = '1' # 0 = Returns plate only, 1 = Returns plate and vehicle details such as make, model, colour, year

filename = '/data/output/Camera1/lastsnap.jpg'

# Additional Options
# =======================================================

save_results = 1 # 0 = false, 1 = true -> save the returned results to a file?
save_filename = 'results.json'

# Debugging
# =======================================================

debug = 0 # 0 = false, 1 = true -> Change this to 1 if you want to pretty print the json data

# =======================================================
# Don't edit below this line
# =======================================================

import pycurl
import cStringIO
import json

from pprint import pprint

buf = cStringIO.StringIO()

c = pycurl.Curl()
c.setopt(c.URL, 'https://api.openalpr.com/v2/recognize?secret_key=' + secret_key + '&recognize_vehicle=' + recognize_vehicle + '&country=' + country_code + '&return_image=0&topn=10')
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(c.HTTPPOST, [('image', (c.FORM_FILE, filename, )),])
c.perform()
c.close()

f = buf.getvalue()
buf.close()

data = json.loads(f)

if save_results:
file = open(save_filename, 'w')
file.write(f)
file.close()

if debug:
pprint(data)
print("\n Please note, the 'u' prefix seen above simply means the string is in Unicode")

if len(data['results']) > 0:

if recognize_vehicle == '1':
  
print("\nLicense Plate: " + data['results'][0]['plate'])
print("Vehicle: " + data['results'][0]['vehicle']['make_model'][0]['name'].title().replace("_"," "))
print("Year: " + data['results'][0]['vehicle']['year'][0]['name'].title())
print("Color: " + data['results'][0]['vehicle']['color'][0]['name'].title())
print("\nCredits Used: " + str(data['credits_monthly_used']) + " out of " + str(data['credits_monthly_total']) + "\n")
else:
print("\nLicense Plate: " + data['results'][0]['plate'])
print("\nCredits Used: " + str(data['credits_monthly_used']) + " out of " + str(data['credits_monthly_total']) + "\n")

else:

  print("\nNo vehicle data found")
  print("\nCredits Used: " + str(data['credits_monthly_used']) + " out of " + str(data['credits_monthly_total']) + "\n")

On Saturday, 20 May 2017 23:59:08 UTC+1, Andrew Jones wrote:
Reply all
Reply to author
Forward
0 new messages