4

I've been trying to make an ANPR program in python, i've found this library that in theory would do the trick. While testing however the code works fine, as can be seen here. The only problem is that what i needed the most out of this was the ANPR readings themselfs, like you get on the java anpr for example, and I'm having problems getting those, printing them. Does anyone know how to get these readings?

Thanks.

Code:

            #!/usr/bin/python2
import cv
import cv2
import copy
import numpy
import math
import os
# import matplotlib.pyplot as plt # crashes when using opencv windows
import Gnuplot

gp = Gnuplot.Gnuplot()
gp("set style data lines")

gost_width = [520.0, 408.0]
gost_height = 112.0
gost_ratio = [gost_height / w for w in gost_width]
min_error = 0.15;

path = "C:/Users/Arengorn/Desktop/testsnaps/"
img_index = 0
min_index = 0
max_img_index = len(os.listdir(path)) - 1
line_index = 0

def rms(values):
        rms = 0.0
        for v in values:
                rms += v**2;
        rms = math.sqrt(rms / len(values))

        return rms

def sma(values, size):
        new_values = []
        for i in range(len(values) - size):
                new_values.append(sum(values[i:i+size]) / float(size))
        return new_values

def compute():
        img_src = cv2.imread(path + "%d1.jpg" % img_index)
        img = cv2.cvtColor(img_src, cv.CV_RGB2GRAY)
        img = cv2.blur(img, (3, 3))
        #img = cv2.Canny(img, 400, 300)
        #retval, img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)
        img = cv2.adaptiveThreshold(img, 255,
                                                                cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                                                cv2.THRESH_BINARY,
                                                                31, -10)
        contours, _ = cv2.findContours(copy.copy(img),
                                                                   cv.CV_RETR_LIST,
                                                                   cv.CV_CHAIN_APPROX_SIMPLE)
        for contour in contours:
                rect = cv2.boundingRect(contour)
                ratio = float(rect[3]) / rect[2]
                if rect[3] * rect[2] > 800:
                        for gr in gost_ratio:
                                if abs(ratio - gr) < min_error:
                                        cv2.rectangle(img_src,
                                                                  (rect[0], rect[1]),
                                                                  (rect[0] + rect[2], rect[1] + rect[3]),
                                                                  (255, 0, 0), 2)
                                        break

        edge_matrix = numpy.array([[-0.0, -1.0, 0.0, 1.0, 0.0],
                                   [-1.0, -2.0, 0.0, 2.0, 1.0],
                                   [-0.0, -1.0, 0.0, 1.0, 0.0]])
        img = cv2.filter2D(img, -1, edge_matrix)
        py = []
        for y in range(img.shape[0]):
                py.append(rms(img[y, :]))
        pys = sma(py, 3)
        pyp = []
        for i in range(len(pys) - 1):
            pyp.append(abs(pys[i + 1] - pys[i]))
        pyps = sma(pyp, 1)
        #print (pyps)

        line_index = pys.index(max(pys))        
        cv2.line(img_src, (0, line_index), (img.shape[1] - 1, line_index), (255, 0, 0))
        cv2.imshow("img", img_src)
        cv2.imshow("img2", img)

        gp.plot(pys)


compute()

key = 0
while key != 27:
        key = cv2.waitKey()

        if key == 65363:
                img_index += 1
                if img_index > max_img_index:
                        img_index = 0
                compute()
        elif key == 65361:
                img_index -= 1
                if img_index < 0:
                        img_index = max_img_index
                compute()
Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52
  • 1
    why don't you try the c++ version? python implementation in the given repo is obviously not finished and probably wont ever be. i imagine the author started off with python to test the waters and switched to c++ after some failed experiments maybe.btw nice find..i must try out some of the tricks ^^ – Zaw Lin Apr 23 '14 at 18:54
  • Well, i need this to run in Linux (Ubuntu), so i thought Python would be a nice language to work with. C++ uses tesseract if i remember right, and I'm not sure its linux compatible. C# on the other hand can be compiled to linux with Mono, but the C# solution i found could only detect car plates, not bike plates as i need. Back to research i guess. – Bernardo Meurer Apr 24 '14 at 19:54

0 Answers0