title: Using haarcascade to detect licence plates with OpenCV and Python
body: <p>Im writing a code that will recognize licence plates on cars with a help of <code>opencv</code> and <code>Python</code>. For that Im using haarcascades. I downloaded haarcascades from here (if you have better source, please let me know):</p>
  
  <p><a href="https://github.com/opencv/opencv/tree/master/data/haarcascades" rel="nofollow noreferrer">https://github.com/opencv/opencv/tree/master/data/haarcascades</a></p>
  
  <p>This is the image:</p>
  
  <p><a href="https://i.stack.imgur.com/2ntjZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2ntjZ.jpg" alt="enter image description here"></a></p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>cascadedetect.cpp:567: error: (-2:Unspecified error) in function 'bool __thiscall cv::HaarEvaluator::Feature::read(const class cv::FileNode &amp;,const class cv::Size_&lt;int&gt; &amp;)'
  &gt; Invalid HAAR feature (expected: 'rw.r.x &lt; W'), where
  &gt;     'rw.r.x' is 32
  &gt; must be less than
  &gt;     '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 &lt;module&gt;
      plates_cascade = cv2.CascadeClassifier('haarcascade_licence_plate_rus_16stages.xml')
  SystemError: &lt;class 'cv2.CascadeClassifier'&gt; returned a result with an error set
  </code></pre>
  
  <p>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:</p>
  
  <pre><code># 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))
  </code></pre>
  
  <p>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.</p>