Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
88 views

Removing Face Recognition Rectangle When The Program Doesn't Recognize A Face With Python OpenCV

The document is a question posted on Stack Overflow asking how to remove the face recognition rectangle drawn by OpenCV when the program does not detect a face in the frame. The code provided detects faces, identifies them if they match a label, and draws rectangles around the faces and eyes/profiles. The asker wants to know how to add a condition to not draw the rectangle if no face is detected, but cannot find the right condition.

Uploaded by

achimedesx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
88 views

Removing Face Recognition Rectangle When The Program Doesn't Recognize A Face With Python OpenCV

The document is a question posted on Stack Overflow asking how to remove the face recognition rectangle drawn by OpenCV when the program does not detect a face in the frame. The code provided detects faces, identifies them if they match a label, and draws rectangles around the faces and eyes/profiles. The asker wants to know how to add a condition to not draw the rectangle if no face is detected, but cannot find the right condition.

Uploaded by

achimedesx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

3/5/2020 removing face recognition rectangle when the program doesn't recognize a face with python OpenCV - Stack

OpenCV - Stack Overflow

removing face recognition rectangle when the program doesn't


recognize a face with python OpenCV
Asked today Viewed 4 times

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)

#draw on the frame


color = (0,0,255) #BGR
stroke = 2
end_cord_x = x +w
end_cord_y = y + h
By using our site, you acknowledge
#while : 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 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

#when all done


cap.release()
cv2.destroyAllWindows()

python opencv deep-learning

asked 5 mins ago


Shir zlotnik
1

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

You might also like