调整具有精​​确面部位置的非均匀图像的大小

问题描述

我在一家处理学校照片的工作室里工作,我们正在尝试编写脚本以消除将每张照片裁剪为模板的工作。我们使用的照片相当统一,但分辨率和头部位置略有不同。我花了很多力气,试图用我相当有限的Python知识来编写脚本,并通过大量的试验和错误以及在线资源,我认为我已经掌握了大部分方法

目前,我正在尝试找出从NumPy数组中裁剪出想要的图像的最佳方法,但我找不到合适的灵活解决方案。对于姿势1和姿势2,头部的位置需要稍有不同,因此它需要易于随时更改(可能将实现某种简单的GUI来输入类似的内容,但是现在我可以更改代码)。

我还需要能够更改照片的输出分辨率,以便它们都是统一的(2000x2500)。有人有什么想法吗?

这是我当前的代码,它只是保存检测到的脸部正方形:

import cv2
import os.path
import glob

# Cascade path
cascPath = 'haarcascade_frontalface_default.xml'

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

#Check for output folder and create if its not there
if not os.path.exists('output'):
    os.makedirs('output')

# Read Images
images = glob.glob('*.jpg')
for c,i in enumerate(images):
    image = cv2.imread(i,1)

    # Convert to grayscale
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

    # Find face(s) using cascade
    faces = faceCascade.detectMultiScale(
        gray,scaleFactor=1.1,# size of groups
        minNeighbors=5,# How many groups around are detected as face for it to be valid
        minSize=(500,500)  # Min size in pixels for face
    )

    # Outputs number of faces found in image
    print('Found {0} faces!'.format(len(faces)))

    # Places a rectangle on face
    for (x,y,w,h) in faces:
        imgCrop = image[y:y+h,x:x+w]

    if len(faces) > 0:
        #Saves Images to output folder with OG name
        cv2.imwrite('output/'+ i,imgCrop)

我可以像这样使用它来裁剪

    # Crop Padding
    left = 300
    right = 300
    top = 400
    bottom = 1000
    
    for (x,h) in faces:
        imgCrop = image[y-top:y+h+bottom,x-left:x+w+right]

但这会输出相当随机的分辨率并根据图像分辨率进行更改

解决方法

TL; DR

  • 要使用尺寸设置新的分辨率,可以使用cv2.resize。可能会有像素损失,因此您可以使用插值方法。

  • 新调整大小的图像可能为BGR格式,因此您可能需要转换为RGB格式。

cv2.resize(src=crop,dsize=(2000,2500),interpolation=cv2.INTER_LANCZOS4)
crop = cv2.cvtColor(crop,cv2.COLOR_BGR2RGB)  # Make sure the cropped image is in RGB format
cv2.imwrite("image-1.png",crop)

建议:

  • 一种方法是使用python的face-recognition库。

  • 该方法使用两个样本图像进行训练。

  • 根据训练图像预测下一幅图像。

例如,以下是训练图像:

enter image description here enter image description here

我们要在下图中预测人脸:

enter image description here

当我们获得训练图像的面部编码并应用于下一张图像时:

import face_recognition
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image,ImageDraw

# Load a sample picture and learn how to recognize it.
first_image = face_recognition.load_image_file("images/ex.jpg")
first_face_encoding = face_recognition.face_encodings(first_image)[0]

# Load a second sample picture and learn how to recognize it.
second_image = face_recognition.load_image_file("images/index.jpg")
sec_face_encoding = face_recognition.face_encodings(second_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    first_face_encoding,sec_face_encoding
]

print('Learned encoding for',len(known_face_encodings),'images.')

# Load an image with an unknown face
unknown_image = face_recognition.load_image_file("images/babes.jpg")

# Find all the faces and face encodings in the unknown image
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image,face_locations)

# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library
# See http://pillow.readthedocs.io/ for more about PIL/Pillow
pil_image = Image.fromarray(unknown_image)
# Create a Pillow ImageDraw Draw instance to draw with
draw = ImageDraw.Draw(pil_image)

# Loop through each face found in the unknown image
for (top,right,bottom,left),face_encoding in zip(face_locations,face_encodings):
    matches = face_recognition.compare_faces(known_face_encodings,face_encoding)
    face_distances = face_recognition.face_distance(known_face_encodings,face_encoding)
    best_match_index = np.argmin(face_distances)
    draw.rectangle(((left,top),(right,bottom)),outline=(0,255),width=5)

# Remove the drawing library from memory as per the Pillow docs
del draw

# Display the resulting image
plt.imshow(pil_image)
plt.show()

输出将是:

enter image description here

以上是我的建议。当您使用当前图像创建新的分辨率时,会有像素损失。因此,您需要使用 插值 方法。

例如:找到面部位置后,在原始图像中选择坐标。

# Add after draw.rectangle function.
crop = unknown_image[top:bottom,left:right]

设置大小为2000 x 2500的新分辨率,并使用CV2.INTERN_LANCZOS4进行插值。

可能的问题:为什么选择CV2.INTERN_LANCZOS4

当然,您可以选择任何内容,但建议使用in this post CV2.INTERN_LANCZOS4

cv2.resize(src=crop,interpolation=cv2.INTER_LANCZOS4)

保存图像

crop = cv2.cvtColor(crop,crop)

输出约为4.3 MB,因此我无法在此处显示。

从最终结果中,我们可以清楚地看到并识别出面孔。该库可在图像中精确找到人脸。

您可以做什么:

  • 您可以使用自己设置的训练图像,也可以使用上面的示例。

  • 使用经过训练的脸部位置为每张图像应用脸部识别功能,并将结果保存在目录中。

,

这就是我想要的裁剪方式,将其添加到“输出面部数量”功能的下方

const [darkState,setDarkState] = useState(Boolean(!cookies.darkTheme));

一团糟,但它的工作方式完全符合我的要求。 最终由于速度原因而选择了openCV而不是切换到python-Recognition,但是如果我可以让多线程在python-recognition中工作,我可能会切换。