使用矩形的左上角和右下角裁剪矩形人脸检测框内的区域

Crop the region inside the rectangular face detection box using the top-left corner and bottom-right corner of rectangle

我想裁剪使用 cv2.rectangle 加框的面孔。

我试过了:

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

但是此代码仅检测到此 image 的 1 张面孔,但是当我使用另一个代码时:

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

它返回给我 3 张面孔,值分别为上、右、下、左,但我不知道如何使用这些值(上、右、下、左)裁剪图像。任何帮助将不胜感激。

我正在使用:

Python- 2.7

OpenCv- 3.1.0

在问题中,方框具有检测到的人脸的上、右、下、左值,因此要使用我使用的给定上、右、下、左值裁剪该区域 PIL.Image.crop():

那么代码就是这样:

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