我正在使用 Mtcnn 網路(https://towardsdatascience.com/face-detection-using-mtcnn-a-guide-for-face-extraction-with-a-focus-on-speed-c6d59f82d49)來檢測面部和頭部. 為此,我使用經典的線條代碼進行人臉檢測:我得到了人臉的 bouding-box 左上角的坐標 (x,y) 框的高度和寬度 (h,w) ,然后我展開框以獲取我的作物頭部:
import mtcnn
img = cv2.imread('images/' path_res)
faces = detector.detect_faces(img)# result
for result in faces:
x, y, w, h = result['box']
x1, y1 = x w, y h
x, y, w, h = result['box']
x1, y1 = x w, y h
if x-100>=0:
a=x-100
else:
a=0
if y-150 >=0:
b=y-150
else:
b=0
if x1 100 >= w:
c=x1 100
else:
c=w
if y1 60 >= h:
d=y1 60
else:
d=h
crop=img[b:d,a:c] #<--- final crop of the head
問題是這個解決方案適用于一些影像,但對于許多其他影像,在我的裁剪中,我得到了目標人物的肩膀和脖子。我認為,這是因為,每個影像中的像素/英寸(即一個影像中的 150 像素在另一個影像中是不同的)。因此,我該怎么做才能正確提取頭部?非常感謝
uj5u.com熱心網友回復:
您可以對檢測到的人臉周圍的邊距使用相對大小而不是絕對大小。例如,上、下、左、右各 50%:
import mtcnn
img = cv2.imread('images/' path_res)
faces = []
for result in detector.detect_faces(img):
x, y, w, h = result['box']
b = max(0, y - (h//2))
d = min(img.shape[0], (y h) (h//2))
a = max(0, x - (w//2):(x w))
c = min(img.shape[1], (x w) (w//2))
face = img[b:d, a:c, :]
faces.append(face)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/380449.html
