当程序无法使用python OpenCV识别人脸时删除人脸识别矩形

时间:2020-03-04 18:17:21

标签: python opencv deep-learning

我是根据YouTube上的一个教程制作的 我不知道我要改变什么 导入cv2 将numpy导入为np 进口泡菜

face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
eye_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_eye.xml')
profile_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_profileface.xml')

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainner.yml")

labels = {}
with open("labels.pickle",'rb') as f:
    og_labels = pickle.load(f)
    labels = {v:k for k,v in og_labels.items()}

cap = cv2.VideoCapture(0)

while(True):
    #frame by frame
    ret,frame = cap.read()
    gray  = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray,scaleFactor=1.5,minNeighbors=5)

我不知道下面的线何时不再出现?

    for(x,y,w,h) in faces:
        print(x,y,w,h)
    roi_gray = gray[y:y+h,x:x+w]
    roi_color = frame[y:y+h,x:x+w]

    id_,conf = recognizer.predict(roi_gray)
    if conf >= 45 and conf <= 85:
        print(id_)
        print(labels[id_])
        font = cv2.FONT_HERSHEY_SIMPLEX
        name = labels[id_]
        color = (0,0,0) # black
        stroke = 2
        cv2.putText(frame,name,(x,y), font, 2, color, stroke,cv2.LINE_AA)

    img_item = "my-image.png"
    cv2.imwrite(img_item,roi_gray)

    #draw on the frame 
    color = (0,0,255) #BGR
    stroke = 2
    end_cord_x  = x +w
    end_cord_y = y + h
    #while :

在框架中没有人脸的情况下,我找不到删除矩形的条件

    cv2.rectangle(frame,(x,y),(end_cord_x,end_cord_y),color,stroke)
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(255,255,255),2)
    profiles = profile_cascade.detectMultiScale(roi_gray)
    for (px,py,pw,ph) in eyes:
        cv2.rectangle(roi_color,(px,py),(px+pw,py+ph),(0,255,0),2)


    #display
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

#when all done
cap.release()
cv2.destroyAllWindows()

0 个答案:

没有答案