-
Notifications
You must be signed in to change notification settings - Fork 1
/
ocr_license_plate.py
36 lines (31 loc) · 1.12 KB
/
ocr_license_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
# import the necessary packages
import os
from pyimagesearch.anpr import PyImageSearchANPR
from imutils import paths
import argparse
import imutils
import cv2
def analyse_image(imagePath,anpr):
# load the input image from disk and resize it
image = cv2.imread(imagePath)
image = imutils.resize(image, width=600)
# apply automatic license plate recognition
(lpText, lpCnt) = anpr.find_and_ocr(image, psm=7, clearBorder=-1 > 0)
# only continue if the license plate was successfully OCR'd
if lpText is not None and lpCnt is not None and len(lpText)>5:
lpText = lpText[:-2]
print("[INFO] " + lpText)
return lpText
return ""
def detect_license_plate(id, dir):
anpr = PyImageSearchANPR(debug=-1 > 0)
all_paths = imagePaths = sorted(list(paths.list_images(dir)))
img_paths = [k for k in all_paths if ('car'+str(id)) in k]
detectedLicensePlates = []
for path in img_paths:
result = analyse_image(path,anpr)
if result != "":
detectedLicensePlates.append(result)
if len(detectedLicensePlates) == 0:
return ""
return detectedLicensePlates