簡體   English   中英

Python 使用 dlib 裁剪人臉圖像

[英]Python cropped face image using dlib

我想調整裁剪后的人臉圖像的邊距。 我當前的代碼可以檢測和裁剪人臉。 但是,裁剪后的圖像太緊,如下圖 output 圖像所示。

輸入圖像:

在此處輸入圖像描述

下面是我的代碼:

import face_recognition
import cv2

img = face_recognition.load_image_file("test.png")
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

face_locations = face_recognition.face_locations(img_rgb)

for top, right, bottom, left in face_locations:
    # Draw a box around the face
    cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)

    crop_img = img_rgb[top:bottom, left:right]
    cv2.imwrite('test_crop.png', crop_img)

更新:縮放后超出邊界框將無法正常工作。

代碼:

使用scale_factor控制新的矩形大小。 M也可以使用不同的公式。 可能不需要使用abs

import face_recognition
import cv2


img = face_recognition.load_image_file("test.png")
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_rgb_copy = img_rgb.copy()


## Define scale factor and window size
scale_factor = 1.1
sz1 = img_rgb.shape[1] * 2
sz2 = img_rgb.shape[0] * 2


face_locations = face_recognition.face_locations(img_rgb)

for top, right, bottom, left in face_locations:
    # Draw a box around the face
    #cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)

    crop_img = img_rgb[top:bottom, left:right]
    #cv2.imwrite('test_crop.png', crop_img)



    ## Calculate center points and rectangle side length
    width = right - left
    height = bottom - top
    cX = left + width // 2
    cY = top + height // 2
    M = (abs(width) + abs(height)) / 2


    ## Get the resized rectangle points
    newLeft = max(0, int(cX - scale_factor * M))
    newTop = max(0, int(cY - scale_factor * M))
    newRight = min(img_rgb.shape[1], int(cX + scale_factor * M))
    newBottom = min(img_rgb.shape[0], int(cY + scale_factor * M))


    ## Draw the circle and bounding boxes
    cv2.circle(img_rgb_copy, (cX, cY), radius=0, color=(0, 0, 255), thickness=2)
    cv2.rectangle(img_rgb_copy, (left, top), (right, bottom), (0, 0, 255), 2)
    cv2.rectangle(img_rgb_copy, (newLeft, newTop), (newRight, newBottom), (255, 0, 0), 2)


    ## Show the original image in window resized to double
    cv2.namedWindow('image', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('image', sz1, sz2)
    cv2.imshow("image", img_rgb_copy)
    cv2.waitKey(0)


cv2.destroyAllWindows()

圖片:

在此處輸入圖像描述

方法:

獲取給定區域的中心點(cX, cY)並通過從兩者中減去相同的值(cX - M,cY - M)從中獲得圖像的新左角。 所以右上角將是(cX + M, cY + M) 您可以使用比例因子,例如M * p代替M ,其中p將控制新區域的大小。

中心點:

width = right - left
height = bottom - top

centerX = left + (width / 2)
centerY = top + (height / 2)

M = (abs(width) + abs(height)) / 2

0 <= p < 1, for smaller crop than given in a side
p > 1, for larger crop margin

此外,新作物可能超出圖像范圍。 為了解決這個問題, newTop = max(0, newTop) , newRight = min(imageWidth, newRight)可以完成,其他人也可以這樣做。 你可以在這里找到一個演示,

https://www.desmos.com/calculator/bgn9dobkjt

在此處輸入圖像描述

DeepFace 封裝了許多人臉檢測庫。 此外,它在背景中應用了人臉 alignment。 您可以在一行代碼中檢測人臉。

#!pip install deepface
from deepface import DeepFace
backends = ['opencv', 'ssd', 'dlib', 'mtcnn']
for backend in backends:
   detected_face = DeepFace.detectFace("img.jpg", detector_backend = backend)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM