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

时间:2019-04-01 05:36:26

标签: python opencv image-processing face-detection haar-classifier

我正在尝试使用OpenCV 4.0中的haar级联来检测表情,性别和年龄估计的面孔。有时,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

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

继续直到我终止。

如何解决该问题?

2 个答案:

答案 0 :(得分: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))

在辅助音符上,我自己干活的原因可能是由于雷电,我的相机确实找到了我的脸。因此,我建议您在使用视频之前先尝试使用图片。

答案 1 :(得分:0)

当我使用jpg格式时,我也会遇到类似的问题,但是主要问题始终是图像的格式,因为当我使用png时,它会自动为元组提供正确的值。

offset = 0
for field in ordered_fields:
    if field[1] > offset:
        num = field[1] - offset
        result.append(('', '|V%d' % num))
        offset += num
    elif field[1] < offset:
        raise ValueError(
            "dtype.descr is not defined for types with overlapping or "
            "out-of-order fields")

输出显示 [[87 114 361 361]]

相关问题