Detect faces from an image using openCV : haarcascades method

Viewed 28
img = cv2.imread('output.jpg')
# Read the input image
if(img is not None):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Load the cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.2,4)

# Draw rectangle around the faces and crop the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
    faces = img[y:y + h, x:x + w]
    cv2.imwrite('face.jpg', faces)
    cv2.imshow("face",faces)
    
      
# Display the output
cv2.imwrite('detcted.jpg', img)
cv2.imshow('img', img)
cv2.waitKey()

This is the detected face. Now the face is a bit zoomed in. Is there any way that we can increase the area of the square to our preference so that the cropped face has a better view?? (Profile Pic basically)

0 Answers
Related