Skip to content

Commit

Permalink
Small NMS correction in OCR.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomsilva committed Dec 26, 2018
1 parent fc334d4 commit 7867a42
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In order to easily run the code, you must have installed the Keras framework wit
$ cd darknet && make
```

**The current version was tested in an Ubuntu 16.04 machine, with Keras 2.0.6, TensorFlow 1.5.0 and Python 2.7.**
**The current version was tested in an Ubuntu 16.04 machine, with Keras 2.2.4, TensorFlow 1.5.0 and Python 2.7.**

## Download Models

Expand Down
3 changes: 2 additions & 1 deletion darknet/python/darknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
b = dets[j].bbox
res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
res = sorted(res, key=lambda x: -x[1])
wh = (im.w,im.h)
free_image(im)
free_detections(dets, num)
return res
return res,wh

if __name__ == "__main__":
#net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0)
Expand Down
13 changes: 9 additions & 4 deletions license-plate-ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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__':
Expand All @@ -23,7 +25,7 @@
ocr_net = dn.load_net(ocr_netcfg, ocr_weights, 0)
ocr_meta = dn.load_meta(ocr_dataset)

imgs_paths = glob('%s/*lp.png' % output_dir)
imgs_paths = sorted(glob('%s/*lp.png' % output_dir))

print 'Performing OCR...'

Expand All @@ -33,12 +35,15 @@

bname = basename(splitext(img_path)[0])

R = detect(ocr_net, ocr_meta, img_path ,thresh=ocr_threshold)
R,(width,height) = detect(ocr_net, ocr_meta, img_path ,thresh=ocr_threshold, nms=None)

if len(R):

R.sort(key=lambda x: x[2][0])
lp_str = ''.join([r[0] for r in 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')
Expand Down
9 changes: 9 additions & 0 deletions src/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ def lwrite(file_path,labels,write_probs=True):
fd.write('%d %f %f %f %f\n' % (cl,cc[0],cc[1],wh[0],wh[1]))


def dknet_label_conversion(R,img_width,img_height):
WH = np.array([img_width,img_height],dtype=float)
L = []
for r in R:
center = np.array(r[2][:2])/WH
wh2 = (np.array(r[2][2:])/WH)*.5
L.append(Label(ord(r[0]),tl=center-wh2,br=center+wh2,prob=r[1]))
return L


class Shape():

Expand Down
2 changes: 1 addition & 1 deletion vehicle-detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

bname = basename(splitext(img_path)[0])

R = detect(vehicle_net, vehicle_meta, img_path ,thresh=vehicle_threshold)
R,_ = detect(vehicle_net, vehicle_meta, img_path ,thresh=vehicle_threshold)

R = [r for r in R if r[0] in ['car','bus']]

Expand Down

0 comments on commit 7867a42

Please sign in to comment.