2

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:

enter image description here

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.

taga
  • 3,537
  • 13
  • 53
  • 119
  • The data set is for detecting *russian* register plates. Do you know if those are known to work (accurately) with non-russian register plates? I don't think it is clear/safe to assume that they would. – sebasth Nov 05 '19 at 14:48
  • I do not know why wouldnt work, they look the same like others. Do you know where can I download haarcascade for other plates? Is there any for european, US or combined? – taga Nov 05 '19 at 15:05
  • 1
    Train your own one – Micka Nov 05 '19 at 18:34
  • Can you tell me how to do that? – taga Nov 05 '19 at 18:48
  • @Micka Can you please tell me how to train/make my own haar cascade? – taga Nov 06 '19 at 13:59
  • @taga maybe [this](https://pythonprogramming.net/haar-cascade-object-detection-python-opencv-tutorial/) helps. – Quang Hoang Nov 06 '19 at 14:20
  • I have watched 10 times, still, I do not know what he is doing. Some of his tutorials are very good, and in my opinion, some are terrible. I have tried this: https://stackoverflow.com/questions/58730450/how-to-create-haarcascade-with-cascade-trainer-gui – taga Nov 06 '19 at 14:33
  • there are some tutorials online and maybe this one helps if you have questions about the positive samples https://stackoverflow.com/questions/40476041/haar-cascade-positive-example-image-sizing/40481309#40481309 – Micka Nov 06 '19 at 22:26
  • Can you check this question? https://stackoverflow.com/questions/58730450/how-to-create-haarcascade-with-cascade-trainer-gui – taga Nov 07 '19 at 06:07
  • Hi @taga, did you find (or have) a cascade file for the Brazilian license plate? Could you please share it with me? – Alex8752 Oct 07 '20 at 23:51

6 Answers6

1

The error occurs because in the starting of *.xml weight file, size is specified. Something like,(in the very beginning of non-commented lines)

<size> H W </size>

Now, if you read the error, its fairly intuitive what to do now, just change the value of W(for your case, since, you are getting error in width) to 32 or higher, (I'm not really aware of the impact, performance wise if you put randomly high value, but it sure doesn't give any error, so preferably keep as low as 32). That's one quick hack, although it doesn't explain why exactly such a size discrepancy occurred during the creation of *.xml file

0

Try to download (don't copy and paste it in newly created file) the XML file from the source who created this file. And then put it in the same directory in which your original python file is placed.

For example when I use frontalEyes.xml, it give following error

Invalid HAAR feature (expected: 'rw.r.x + rw.r.width <= W'), where 
>     'rw.r.x + rw.r.width' is 22
> must be less than or equal to
>     'W' is 16

When I open this xml file, I get the link of the original file, like in this case I got http://www-personal.umich.edu/~shameem/haarcascade_eye.xml, then I downloaded this file and put it in main project directory.

Now it will work fine.

am2505
  • 2,194
  • 15
  • 19
0

you need to replace this line : plates_cascade = cv2.CascadeClassifier('haarcascade_licence_plate_rus_16stages.xml') with this one : plat_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_russian_plate_number.xml")

john
  • 1
0

go to the haar casecade .xml file and change the value for the size.

<size> H W </size>
Zoe
  • 27,060
  • 21
  • 118
  • 148
0

Only change the opencv version to any old version. It will work.

Aman Srivastava
  • 1,007
  • 1
  • 13
  • 25
rahul
  • 1
  • 3
    How can you provide such an answer without even knowing what version the OP is using? Did you replicate the OP's code using some `opencv` version and got them right? If so, please include evidence. – Amitai Irron Jun 05 '20 at 16:01
-1

Try to install a different version of OpenCV and then run it. Version 3.4.4 worked for me.

Akshay Dagar
  • 13
  • 1
  • 2