Image File Face Detection in Folder (using os.walk)

1.6k Views Asked by At
for root, dirs, files in os.walk('c:\images'):
    for fname in files:
        img = cv2.imread(fname)
        cv2.resize(img, None, fx=0.4, fy=0.3, interpolation=cv2.INTER_AREA)
        eyes = eye_cascade.detectMultiScale(img, 1.1, 5)
        faces = face_cascade.detectMultiScale(img, 1.2, 5)
        noses = nose_cascade.detectMultiScale(img, 1.1, 5)
        mouths = mouth_cascade.detectMultiScale(img, 1.1, 5)

has error

OpenCV Error: Assertion failed (ssize.area() > 0) in cv::resize, file ..\..\..\..\opencv\modules\imgproc\src\imgwarp.cpp, line 1968
Traceback (most recent call last):
  File "C:/FaceDetWeightSave.py", line 19, in <module>
    cv2.resize(img, None, fx=0.4, fy=0.3, interpolation=cv2.INTER_AREA)
cv2.error: ..\..\..\..\opencv\modules\imgproc\src\imgwarp.cpp:1968: error: (-215) ssize.area() > 0 in function cv::resize

I think that this error is from os.walk(for root, dirs, files <- this). How can I detect files?

1

There are 1 best solutions below

1
berak On

you need to prepend the dir-name to the image-name, else your script won't find the image:

for root, dirs, files in os.walk('c:\images'):
    for fname in files: # fname is the plain filename only
        src = os.path.join(root, fname) # this is the absolute path to the image
        img = cv2.imread(src)
        ...