2

I have the following little code:

from google.colab import drive
from IPython.display import display
import PIL
from PIL import Image, ImageDraw
import kraken
from kraken import pageseg
import cv2 as cv

img = Image.open("/content/drive/My Drive/images/dropfire.jpg")

face_cascade = cv.CascadeClassifier(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_frontalface_default.xml")
eye_cascade = cv.CascadeClassifier(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_eye.xml")

file_name = "/content/drive/My Drive/images/dropfire.jpg"
img = cv.imread(file_name)
pil_img = Image.open(file_name)
cv_img = pil_img.convert('L')
cv_img = cv.imread(file_name)

faces = face_cascade.detectMultiScale(cv_img)

When I run the last cell (with faces), it raises:

error                                     Traceback (most recent call last)
<ipython-input-23-2bd7582f8a20> in <module>()
----> 1 faces = face_cascade.detectMultiScale(cv_img)

error: OpenCV(4.1.2) /io/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

Until this cell everything works fine. According to a Stack Overflow answer, I added cv.data.haarcascades + in the brackets of cv.CascadeClassifier. Although people said this worked for them, it doesn't for me somehow.

halfer
  • 19,824
  • 17
  • 99
  • 186
VIVID
  • 555
  • 6
  • 14
  • 1
    you have to add `cv.data.haarcascades` but without `/content/drive/My Drive/datas` - to read files preinstalled with `cv2`. If you have it in `/content/drive/My Drive/datas` then don't add `cv.data.haarcascades`. Simply use `print(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_eye.xml")` to see if you created correct path. – furas Aug 15 '20 at 08:36
  • maybe first check if you use correct path to image `/content/drive/My Drive/images/dropfire.jpg` . CV2 `imread()` doesn't raise error when you use wrong path but it returns empty data - check `print(cv_img)` - and it raise error later when you try to use this image in other function - like `detectMultiScale(cv_img)` – furas Aug 15 '20 at 08:39
  • @furas `/usr/local/lib/python3.6/dist-packages/cv2/data//content/drive/My Drive/datas/haarcascade_eye.xml` this is what I get when I run `print(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_eye.xml")` – VIVID Aug 15 '20 at 08:47
  • @furas Also, I displayed the image and it works fine... – VIVID Aug 15 '20 at 08:48
  • is this correct path ? Do you have on disk `/usr/local/lib/python3.6/dist-packages/cv2/data//content/drive/My Drive/datas/haarcascade_eye.xml` - probably not. You probably have `/usr/local/lib/python3.6/dist-packages/cv2/data/haarcascade_eye.xml` (which is `cv.data.haarcascades + "haarcascade_eye.xml"`) and/or `/content/drive/My Drive/datas/haarcascade_eye.xml` (if you download it) – furas Aug 15 '20 at 09:07
  • @furas I replaced that in=bracket thing with `cv.data.haarcascades + "haarcascade_eye.xml"` but still this error – VIVID Aug 15 '20 at 09:30
  • first use `print()` to see value in `cv_img` and paths you can also use `os.path.exists()` to check if files really exist - `filename` and `cv.data.haarcascades + "haarcascade_eye.xml"` – furas Aug 15 '20 at 10:15

2 Answers2

1

The issue is to check whether XML is loaded or not, use eye_cascade.empty() to check whether it's loaded or not.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

imho, that "little code" of yours has too much noise (unnecessary lines of code) already. I think it makes for you even harder to understand what's going on.

Comment out/delete all the lines except these:

    import cv2 as cv
    
    face_cascade = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_default.xml")
    eye_cascade = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_eye.xml")

    file_name = "/content/drive/My Drive/images/dropfire.jpg"
    img = cv.imread(file_name)  # this reads the image already
    
    cv_img = cv.imread(file_name) # this reads same image once more, not sure if intended
    faces = face_cascade.detectMultiScale(cv_img)  

If your image "dropfire" really exists at that path, this should work without error, but it won't show any results of face detection. You can add these lines at the end to see the result:

    for (x, y, w, h) in faces:
        cv.rectangle(cv_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
    cv.imshow("Nice face", cv_img)
    cv.waitKey(0)

cv.data.haarcascades already has path to all those cv2 xml files, so you only need to use file names.

Ewalldinho
  • 56
  • 1
  • 5