简体   繁体   English

如何在 pycharm 中修复此错误? cv2.error: OpenCV(3.4.2) 错误: (-215:Assertion failed)

[英]How to fix this error in pycharm? cv2.error: OpenCV(3.4.2) error: (-215:Assertion failed)

I'm new to python and opencv, I want to crop face images from my input images.我是 python 和 opencv 的新手,我想从我的输入图像中裁剪人脸图像。 I really don't know where went wrong, could anyone help me to fix it?我真的不知道哪里出了问题,有人能帮我解决吗?

I think there might be something wrong with the directory of input images.我认为输入图像的目录可能有问题。 So I have changed all the '/' to '\\', it's still not right.所以我把所有的'/'都改成了'\\',还是不行。 Also, under this directory, there are many subfolders, and in each subfolder, there are some raw images.另外,在这个目录下,有很多子文件夹,在每个子文件夹中,都有一些原始图像。

import cv2
import os


def fetch_face_pic(img,face_cascade):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize = (30, 30), flags = 0)

    for(x, y, w, h) in faces:
        crop = img[y:y+h, x:x+w]
    return crop


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_defalut.xml')

#path_save = '/Users/oysdfx/Desktop/Face-anti-spoofing-master/crop/ImposterCrop'
path_read = '/Users/oysdfx/Desktop/Face-anti-spoofing-master/raw/ImposterRaw'

for file in os.listdir(path_read):
    pic = os.path.join(path_read, file)
    img = cv2.imread(pic)
    crop = fetch_face_pic(img, face_cascade)

    resized_img = cv2.resize(img, (64, 64), interpolation=cv2.INTER_CUBIC)
    cv2.imwrite(pic, resized_img)

Actually I want to crop out faces from input images, and save them in a new folder.实际上我想从输入图像中裁剪出人脸,并将它们保存在一个新文件夹中。 I don't know how to implement this.我不知道如何实现这一点。 I run this piece of code and get error as follows:我运行这段代码并得到如下错误:

Traceback (most recent call last):
  File "/Users/oysdfx/Desktop/Undergraduate_Thesis/Face_Anti-Spoofing/crop_face.py", line 86, in <module>
    crop = fetch_face_pic(img, face_cascade)
  File "/Users/oysdfx/Desktop/Undergraduate_Thesis/Face_Anti-Spoofing/crop_face.py", line 70, in fetch_face_pic
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/opencv-suite_1535558719691/work/modules/imgproc/src/color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'CvtHelper'


Process finished with exit code 1

This has nothing to do with your IDE, PyCharm.这与您的 IDE PyCharm 无关。 Your image path is probably wrong.您的图像路径可能是错误的。 Check if img is None before moving forward.在继续之前检查img是否为None If so, print the path ( pic ) and you will see it is wrong.如果是这样,打印路径( pic ),你会发现它是错误的。

# [...]

for file in os.listdir(path_read):
    pic = os.path.join(path_read, file)
    img = cv2.imread(pic)
    if img is None:
        raise Exception('Invalid image: {}'.format(pic))
    crop = fetch_face_pic(img, face_cascade)

# [...]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python OpenCV(3.4.2)错误:(-215:断言失败) - python OpenCV(3.4.2) error: (-215:Assertion failed) cv2.error: OpenCV(4.5.2).error: (-215:Assertion failed)._src:empty() in function 'cv::cvtColor' - cv2.error: OpenCV(4.5.2) .error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' cv2.error: OpenCV(4.5.2) resize.cpp:4051: error: (-215:Assertion failed).ssize:empty() in function 'cv::resize' - cv2.error: OpenCV(4.5.2) resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' OpenCV(4.2.0),imwrite() 到子文件夹会破坏模块; cv2.error: (-215:Assertion failed).ssize:empty() in function 'cv::resize'`' - OpenCV(4.2.0),imwrite() to a subfolder breaks the module; cv2.error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'`' cv2.error: OpenCV(4.0.0) 错误: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function &#39;cvShowImage&#39; - cv2.error: OpenCV(4.0.0) error: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage' OpenCV(3.4.2): error: (-215:Assertion failed) with Template Matching Method - OpenCV(3.4.2): error: (-215:Assertion failed) with Template Matching Method cv2.error:(-215:Assertion failed) !_src.empty() in function &#39;cv::cvtColor&#39; - cv2.error:(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' small_frame = cv2.resize(frame, (128,128)) cv2.error: OpenCV(3.4.5) error: (-215:Assertion failed) !ssize.empty() in function &#39;cv::resize&#39; - small_frame = cv2.resize(frame, (128,128)) cv2.error: OpenCV(3.4.5) error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' cv2.error: OpenCV(4.5.5) D:\a\opencv-python\imgproc\src\color.cpp:182: 错误: (-215:Assertion failed) !_src.empty() in function &#39;cv:: cvt颜色&#39; - cv2.error: OpenCV(4.5.5) D:\a\opencv-python\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 如何修复 Opencv arcLength function 错误:(-215:断言失败) - How to fix Opencv arcLength function error: (-215:Assertion failed)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM