-
Notifications
You must be signed in to change notification settings - Fork 9
/
censor_plate.py
46 lines (34 loc) · 1.27 KB
/
censor_plate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import cv2
import sys
from openalpr import Alpr
# region currently set to EU (Europe). Other regions include SG (Singapore) and US
alpr = Alpr("eu", "openalpr.conf", "runtime_data")
# Make sure the library loaded before continuing.
if not alpr.is_loaded():
print("Error loading OpenALPR")
sys.exit(1)
# Optionally, provide the library with a region for pattern matching. Example: md for maryland
# alpr.set_default_region("md")
if len(sys.argv) > 1:
filepath = str(sys.argv[1])
else:
print "No file path provided. Program closing."
sys.exit(1)
img = cv2.imread(filepath)
if not img.size:
print "file does not exist"
sys.exit(1)
analyzed_file = alpr.recognize_file(filepath)
if not analyzed_file['results']:
print "No license plate detected"
sys.exit(1)
#x,y coordinates of opposite corners of license plate
x1 = analyzed_file['results'][0]['coordinates'][0]['x']
y1 = analyzed_file['results'][0]['coordinates'][0]['y']
x3 = analyzed_file['results'][0]['coordinates'][2]['x']
y3 = analyzed_file['results'][0]['coordinates'][2]['y']
cv2.rectangle(img,(x1,y1),(x3,y3),(0,0,0),-1)
cv2.imwrite(filepath.partition(".")[0]+"_plate_censor.jpg",img)
print "License plate successfully censored"
# Call when completely done to release memory
alpr.unload()