Python "OpenCV Error: Assertion failed (size.width>0 && size.height>0)" when using script to load and detect faces from a given directory path
<p>I'm trying to load a set of images from a given directory, detect and count how many faces are detected there.
My code looks like this:</p>

<pre><code>import cv2
import numpy as np
import os

def read_images():
    path1 = '/home/sousajf1/Documents/opencv/facedetect/data'   
    path2 = '/home/sousajf1/Documents/opencv/facedetect/dataprocessed'    
    countFace=0
    listing = os.listdir(path1)    
for file in listing:
    img = cv2.imread('path1 + file',0)
    faceCascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
    eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
    faces = faceCascade.detectMultiScale(img, 1.3, 5)
    cv2.imshow('img',img)
    for (x,y,w,h) in faces:

        cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
        roiGray=gray[y:y+h, x:x+w]
        roiColor=img[y:y+h, x:x+w]

    if len(faces)!=0:
        countFace+=1

    #cv2.imwrite(path2 + file, "JPEG")

print countFace
cv2.waitKey(0)

read_images()   
</code></pre>

<p>But when I run this i get always 0 faces detected. I thought maybe there were no images being loaded at all, so i added the cv2.imshow line before the first for, and i got the "OpenCV Error: Assertion failed (size.width>0 &amp;&amp; size.height>0" , which is probably because there no image at all to show.</p>

<p>The code seems right to me, and I have no idea what to do now.
Would appreciate some help and a fresh mind. Thanks guys.</p>

<p>UPDATE</p>

<p>I've now tried using gob function, still didnt work:</p>

<pre><code>import cv2
import glob
countFace = 0
countEyes = 0
for img in glob.glob("/home/sousajf1/Documents/opencv/*.pgm"):

cvImg = cv2.imread(img,0)
faceCascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(cvImg, 1.3, 5)
#cv2.imshow('img',cvImg)
for (x,y,w,h) in faces:
    cv2.rectangle(cvImg, (x,y), (x+w, y+h), (255,0,0), 2)
    roiGray=gray[y:y+h, x:x+w]
    roiColor=cvImg[y:y+h, x:x+w]
    eyes=eyeCascade.detectMultiScale(roiGray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roiColor, (ex,ey), (ex+ew, ey+eh), (0,255,0), 1)
        if len(eyes)&gt;0:
            countEyes = countEyes +1
if len(faces)&gt;0:
    countFace = countFace +1


print countFace
print countEyes
</code></pre>