-1

I want to crop faces which are boxed using cv2.rectangle.

I tried :

faces = face_cascade.detectMultiScale(gray_image, 1.25, 6)

but this code is detecting only 1 face for this image but when I used another code :

boxes = face_recognition.face_locations(rgb,model="hog")

it returned me 3 faces with values top,right,bottom,left but I dont know how to crop an image using these values(top,right,bottom,left). Any help will be appreciated.

I am using:

Python- 2.7

OpenCv- 3.1.0

SRJ577
  • 31
  • 2
  • 9
  • Your question is "how to crop an image" and has nothing to do with facial recognition, does it? – Jonathon Reinhart Sep 27 '18 at 11:38
  • Possible duplicate of [How to crop an image in OpenCV using Python](https://stackoverflow.com/questions/15589517/how-to-crop-an-image-in-opencv-using-python) – eshirima Sep 27 '18 at 11:44
  • You're right Jonathan! but it is part of "face_recognition". The output of my face recognition is an image with detected face and the recognized name is printed on the boxes but I need to crop the detected face and put the text. – SRJ577 Sep 27 '18 at 11:47

1 Answers1

0

In the question, boxes have the top, right, bottom, left values of the detected face, so to crop that area with the given top, right, bottom, left values I used PIL.Image.crop():

Then the code will be like this:

    from PIL import Image
    img = Image.open("path/to/file")       
    crop_pic = img.crop( ( left, top, right, bottom ) )        
    crop_pic.show()
SRJ577
  • 31
  • 2
  • 9