Removing Face Recognition Rectangle When The Program Doesn't Recognize A Face With Python OpenCV
Removing Face Recognition Rectangle When The Program Doesn't Recognize A Face With Python OpenCV
I made this one from a tutorial on YouTube I can't figure out what I have to change import cv2
import numpy as np import pickle
0
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)
I don't know when the following cords are not presenting a face anymore?
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)
https://stackoverflow.com/questions/60532368/removing-face-recognition-rectangle-when-the-program-doesnt-recognize-a-face-wi 1/2
3/5/2020 removing face recognition rectangle when the program doesn't recognize a face with python OpenCV - Stack Overflow
I can't find a condition that will remove the rectangle in case there's not a face in the frame
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
New contributor
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.
https://stackoverflow.com/questions/60532368/removing-face-recognition-rectangle-when-the-program-doesnt-recognize-a-face-wi 2/2