forked from sergiomsilva/alpr-unconstrained
-
Notifications
You must be signed in to change notification settings - Fork 0
/
license-plate-ocr.py
64 lines (40 loc) · 1.28 KB
/
license-plate-ocr.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
import cv2
import numpy as np
import traceback
import darknet.python.darknet as dn
from os.path import splitext, basename
from glob import glob
from darknet.python.darknet import detect
from src.label import dknet_label_conversion
from src.utils import nms
if __name__ == '__main__':
try:
input_dir = sys.argv[1]
output_dir = input_dir
ocr_threshold = .4
ocr_weights = 'data/ocr/ocr-net.weights'
ocr_netcfg = 'data/ocr/ocr-net.cfg'
ocr_dataset = 'data/ocr/ocr-net.data'
ocr_net = dn.load_net(ocr_netcfg, ocr_weights, 0)
ocr_meta = dn.load_meta(ocr_dataset)
imgs_paths = sorted(glob('%s/*lp.png' % output_dir))
print 'Performing OCR...'
for i,img_path in enumerate(imgs_paths):
print '\tScanning %s' % img_path
bname = basename(splitext(img_path)[0])
R,(width,height) = detect(ocr_net, ocr_meta, img_path ,thresh=ocr_threshold, nms=None)
if len(R):
L = dknet_label_conversion(R,width,height)
L = nms(L,.45)
L.sort(key=lambda x: x.tl()[0])
lp_str = ''.join([chr(l.cl()) for l in L])
with open('%s/%s_str.txt' % (output_dir,bname),'w') as f:
f.write(lp_str + '\n')
print '\t\tLP: %s' % lp_str
else:
print 'No characters found'
except:
traceback.print_exc()
sys.exit(1)
sys.exit(0)