【问题标题】:Detectmultiscale() returns an empty tuple sometimesDetectmultiscale() 有时会返回一个空元组
【发布时间】:2019-08-22 05:40:06
【问题描述】:

我正在尝试使用 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)

返回假

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

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

一直持续到我终止。

我该如何解决这个问题?

【问题讨论】:

  • 我也遇到这个问题,请问您解决了吗?
  • 很遗憾,我做不到。目前,我正在尝试其他人脸检测选项,例如 Dlib 和 OpenCV 的 DNN

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


【解决方案1】:

这是我使用的代码的 sn-p。我删除了 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))

在第二点上,我自己工作的原因可能是因为我的相机确实因为闪电而找到了我的脸。所以我建议你在使用视频之前先用图片尝试一下。

【讨论】:

  • 尝试运行一个简单的人脸检测代码,并实时打印 faces 变量。你会看到它有时会返回一个空元组。反正我改用opencv-dnn,虽然慢,但是更准确
【解决方案2】:

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

classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")


# reading the image
img = cv2.imread('i.png')

# showing the image
#cv2.imshow('shaswat face detection ',img)


# making image to gray scale as black and white
grayscaled_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# cv2.imshow('shaswat face detection ',grayscaled_img)

# detecting the image
# return top left and bottom right points
faces = classifier.detectMultiScale(grayscaled_img)

print(faces)
#cv2.rectangle(img , face_coordinates[0] , face_coordinates[1] , (255,0,0) , 10)

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

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 2016-09-24
    • 1970-01-01
    • 2015-09-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多