Detectmultiscale()有时会返回一个空元组

问题描述 投票:0回答:1

我试图在OpenCV 4.0中使用haar-cascade来检测情绪,性别和年龄估计的面部。有时,detectmultiscale()函数返回一个空元组,在后面的识别部分引发错误。

我尝试创建一个while循环,直到检测到面部,但是一旦未检测到面部,它似乎没有被再次检测到(在同一个捕获的帧中),我得到空元组返回。奇怪的是,有时程序运行完美无缺。正确加载检测模型,因为cv2.CascadeClassifier.empty(face_cascade)返回False。

捕获的帧似乎没有问题,因为我可以正确显示它。

在搜索之后,我发现detectmultiscale()确实在没有检测到面部时返回一个空元组。

Python OpenCV face detection code sometimes raises `'tuple' object has no attribute 'shape'`

face_cascade = cv2.CascadeClassifier(
        'C:\\Users\\kj\\Desktop\\jeffery 1\\trained_models\\detection_models\\haarcascade_frontalface_alt.xml')
 retval = cv2.CascadeClassifier.empty(face_cascade)
 print(retval)

返回False

def video_cap(out_queue):
        video_capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
        #video_capture.set(3, 768)
        #video_capture.set(4, 1024)
        while True:
                ret, bgr_image = video_capture.read()
                cv2.imshow('frame',bgr_image)
                cv2.waitKey(1000)
                cv2.destroyAllWindows()
                if video_capture.isOpened() == False :
                    video_capture.open(0)

                if(ret):
                    gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)  
                    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)  
                    faces = detect_faces(face_detection, gray_image)
                    ret_list = [gray_image, rgb_image, faces]
                    print("DEBUG: VIDEO_CAPTURE MODULE WORKING")
                    out_queue.put(ret_list)
                    return

video_cap函数是线程化的

def detect_faces(detection_model, gray_image_array):
    faces1 = detection_model.detectMultiScale(gray_image_array, scaleFactor= 2, minNeighbors=10,minSize=(64,64))
    while(len(faces1)== 0 ):
        faces1 = detection_model.detectMultiScale(gray_image_array, scaleFactor=2, minNeighbors=10, minSize=(64, 64))
        print(faces1)
        if(len(faces1)!=0):
            break
    return faces1

我得到输出:()()()()....

继续,直到我终止。

我该如何解决这个问题?

python opencv image-processing face-detection haar-classifier
1个回答
0
投票

这是我使用的代码片段。我删除了detectMultiScale()函数中的ARGUMENTS,它运行正常。

此外,请确保您具有xml文件的正确路径。

classifier = cv2.CascadeClassifier("../../../l-admin/anaconda3/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml")
img = cv2.imread('../Tolulope/Adetula Tolulope (2).jpg')
face = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = classifier.detectMultiScale(face)
print(type(faces), faces)
for (x, y, w, h) in faces:
  img = cv2.imwrite("facesa.png", cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3))

在次要说明中,我自己的工作原因可能是因为我的相机因闪电而确实找到了我的脸。因此,我建议您在使用视频之前首先尝试使用图片。

© www.soinside.com 2019 - 2024. All rights reserved.