文件夹中的图像文件人脸检测(使用os.walk)

时间:2014-11-22 10:28:30

标签: python opencv os.walk

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)

有错误

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

我认为此错误来自os.walk(for root, dirs, files <- this)。我该如何检测文件?

1 个答案:

答案 0 :(得分:0)

您需要将dir-name添加到图像名称之前,否则您的脚本将找不到图像:

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)
        ...