python - 没有检测到人脸时cv2人脸检测问题

标签 python opencv flask

我有一个应用程序,我尝试检测人脸并打印嗨!当检测到脸部时。我的问题是,即使面孔(人)从可见区域移开,消息也不会改变。在这种情况下,我想打印“找不到面孔”。
当未检测到脸部时我该如何采取行动,无论是以声音形式打印内容还是以其他形式的通知。
到目前为止我编写的代码:

import cv2
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
cap = cv2.VideoCapture(0)

while 1:

    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, 1.3, 5)
    if len(faces) == 0:
        print
        "No faces found"
    for (x,y,w,h) in faces:
        re = cv2.rectangle(img,(x,y),(x+w,y+h),(200,200,0),2)
        if 're' in locals():
            print('Hi')
        else:
            print('No faces found')
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
    cv2.imshow('Hi',img)

    # Wait for Esc key to stop
    k = cv2.waitKey(30)
    if k == 13:
        break
cap.release()
cv2.destroyAllWindows()

最佳答案

这是一个非常简单的错误

print
"No faces found"
是导致错误的原因,请尝试将其更改为
print("No faces found")
您正在做的是调用打印函数,只是定义一个字符串而不使用它
要播放声音,您可以playsound

关于python - 没有检测到人脸时cv2人脸检测问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64134429/

相关文章:

html - 数据库错误: The first argument to execute must be a string or unicode query in python

python - 光束 : AfterProcessingTime cause 'NoneType' object has no attribute 'time'

python - 当映射不是一对一时,ctypes 如何将 C 结构体成员映射到 Python 类 _fields_?

opencv - 给定一组点,如何计算相似变换(平移、缩放、旋转)?

python - 将连接的组件分离到多个图像

python - 在docker上运行python网站

python - sqlalchemy 同时从 2 个表中计数

python - 使用不同长度的数组列表索引一维数组

python - 如何修复函数 'detectMultiScale' 中的 opencv/modules/objdetect/src/cascadedetect.cpp :1698: error: (-215:Assertion failed) ! empty() ^?

Python REST API 发布列表