【问题标题】:cv2 issue for face-detection algorithm人脸检测算法的 cv2 问题
【发布时间】:2019-10-07 15:59:36
【问题描述】:

我有一个人脸检测程序。 我尝试运行代码,但它不工作。

import cv2
import numpy as np

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainer/trainer.yml")
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);


cam = cv2.VideoCapture(0)
fontFace = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (255, 255, 255)
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.2,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)

        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        if(conf<50):
            if(Id==1):
                Id="chandra"
            elif(Id==2):
                Id="vamsi"

        else:
            Id="Unknown"
        cv2.putText(im,str(Id), (x,y+h),fontFace, 255)
    cv2.imshow('im',im) 
    if (cv2.waitKey(10) == ord('q')):
        break
cam.release()
cv2.destroyAllWindows()

我收到了这个错误:

(-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

我正在使用opencv2 和 python 3.7

【问题讨论】:

  • 可能是代码的语法在 python 3.X 版本中已经改变了。
  • 改进了文本格式并编辑了标题

标签: python python-3.x cv2


【解决方案1】:

也许这会有所帮助:

try:
    ret, im =cam.read()
except:
    continue

【讨论】:

    【解决方案2】:

    请使用下面的代码检查您的相机是否有任何图像:

    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()
    
        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
        # Display the resulting frame
        cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2012-02-25
      • 1970-01-01
      • 2020-05-31
      相关资源
      最近更新 更多