Im writing a code that will recognize licence plates on cars with a help of opencv
and Python
. For that Im using haarcascades. I downloaded haarcascades from here (if you have better source, please let me know):
https://github.com/opencv/opencv/tree/master/data/haarcascades
This is the image:
When I work with first haarcascade, it only detects licence plate on right car *(two times), but It does not recognize plates on white car.
When I work second haarcascade, it gives me error that I do not know what it means and how to fix it, this is the error:
cascadedetect.cpp:567: error: (-2:Unspecified error) in function 'bool __thiscall cv::HaarEvaluator::Feature::read(const class cv::FileNode &,const class cv::Size_<int> &)'
> Invalid HAAR feature (expected: 'rw.r.x < W'), where
> 'rw.r.x' is 32
> must be less than
> 'W' is 16
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\nenad\OneDrive\Desktop\open cv slika\Tablice\tablica.py", line 14, in <module>
plates_cascade = cv2.CascadeClassifier('haarcascade_licence_plate_rus_16stages.xml')
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
I do not think that the problem is because these haar cascades have russian licence plates, I have watched all over the web and russian licence plates look similar to other. This is the code that I wrote:
# Standard imports
import cv2
import numpy as np
# Read image
img = cv2.imread("slika2.jpg", 1)
gray = cv2.cvtColor(img, 0)
cv2.imshow('img', gray)
cv2.waitKey(0)
#read haarcascade
#plates_cascade = cv2.CascadeClassifier('haarcascade_russian_plate_number.xml') #does not give me error, but result is not correct
plates_cascade = cv2.CascadeClassifier('haarcascade_licence_plate_rus_16stages.xml') #gives me error
plates = plates_cascade.detectMultiScale(gray, 1.2, 4)
for (x,y,w,h) in plates:
#detect plate with rectangle
#rec. start point (x,y), rec. end point (x+w, y+h), blue color(255,0,0), line width 1
plates_rec = cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 1)
#cv2.putText(plates_rec, 'Text', (x, y-3), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1)
gray_plates = gray[y:y+h, x:x+w]
color_plates = img[y:y+h, x:x+w]
#cv2.imshow('img', gray_plates)
#cv2.waitKey(0)
height, width, chanel = gray_plates.shape
print(height, width)
cv2.imshow('img', img)
cv2.waitKey(0)
print('Number of detected licence plates:', len(plates))
Any suggestion how can I improve my code, and fix this? Also, If you can tell me how to create my own haar cascade, I have looked all over the web but I couldnt find any solution that works.