0
votes

Hi im trying to detect some license platings with a Classifier from cv2. (on Windows)

I installed open-cv via pip install in a venv

(path: C:\Users\ramif\Desktop\StraßenverkehrProjekt\venv\Lib\site-packages\cv2)

When Im trying to run this code:

classifier = cv2.CascadeClassifier(os.path.join(cv2.data.haarcascades,'haarcascade_russian_plate_number.xml'))
platings = classifier.detectMultiScale(gray)

It always give me the error:

cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

Even when I take the full path, it still doesnt work:

classifier = cv2.CascadeClassifier('C:\\Users\\ramif\\Desktop\\Straßenverkehr Projekt\\venv\\Lib\\site- 
                                     packages\\cv2\\data\\haarcascade_russian_plate_number.xml')
platings = classifier.detectMultiScale(gray)

When I print:

print(cv2.data.haarcascades)

The output is: c:\Users\ramif\Desktop\Straßenverkehr Projekt\venv\lib\site-packages\cv2\data\

Why doesnt the program find the haarcascade.xml file? I appreciate any help!

1
I think it is saying that your image "gray" is empty. Where does gray originate?fmw42
posted the code as an answerRami

1 Answers

0
votes

My gray function converts the images in gray. (cv2imread can successfully read the images)

Full Code:

import os
import cv2
import matplotlib.pyplot as plt
import numpy as np

file = os.path.join(os.path.dirname(__file__), "040603")
os.chdir(file)
"list of the images"
img = []                  
for i in os.listdir(file):
    x = cv2.imread(i)
"testing just 1 image here"
    break                 

for j in img:
    gray = cv2.cvtColor(j, cv2.COLOR_BGR2GRAY)
    plt.imshow(gray, "gray")
    plt.show()
    classifier = cv2.CascadeClassifier(os.path.join(cv2.data.haarcascades, 'haarcascade_licence_plate_rus_16stages.xml'))
    platings = classifier.detectMultiScale(gray)
    print(platings)

When I print gray the output is:

[[ 51  51  51 ...  74 111  85]

 [ 51  51  51 ...  61  92  75]
 [ 51  51  52 ...  55  76  68]
 ...
 [ 52  52  52 ...  65  66  81]
 [ 52  51  50 ...  65  62  73]
 [ 54  53  52 ...  72  66  68]]

So gray converted the image successfully

Update: This code works for only 1 picture:

import cv2
import matplotlib.pyplot as plt


x = cv2.imread("P1010003.jpg")
gray = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
classifier = cv2.CascadeClassifier('venv\Lib\site-packages\cv2\data\haarcascade_russian_plate_number.xml')
platings = classifier.detectMultiScale(gray, minNeighbors = 10)

c = x.copy()
for plating in platings:
    x, y, w, h = plating
    cv2.rectangle(c, (x, y), (x+w, y+h), (55, 175, 212), 10)
z = cv2.cvtColor(c, cv2.COLOR_BGR2RGB)
print(plating)
plt.imshow(z)
plt.show()