简体   繁体   English

Matplotlib - 绘制没有背景图像的人脸地标

[英]Matplotlib - plot face landmarks with no background image

The code bellow provides face-aligment to any photo I give as input.下面的代码为我作为输入提供的任何照片提供face-aligment

Face-aligment link: https://github.com/1adrianb/face-alignment Face-aligment链接: https : //github.com/1adrianb/face-alignment

import face_alignment
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from skimage import io

# Run the 3D face alignment on a test image, without CUDA.
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._3D, device='cpu', flip_input=False)

input = io.imread('datasets/cloud_faces/trainB/00000566.AN.jpg')
preds = fa.get_landmarks(input)[-1]

#TODO: Make this nice
fig = plt.figure(figsize=plt.figaspect(.5))
ax = fig.add_subplot(1, 2, 1)
ax.imshow(input)
ax.plot(preds[0:17,0],preds[0:17,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[17:22,0],preds[17:22,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[22:27,0],preds[22:27,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[27:31,0],preds[27:31,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[31:36,0],preds[31:36,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[36:42,0],preds[36:42,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[42:48,0],preds[42:48,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[48:60,0],preds[48:60,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)
ax.plot(preds[60:68,0],preds[60:68,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) 
ax.axis('off')

ax = fig.add_subplot(1, 2, 2, projection='3d')
surf = ax.scatter(preds[:,0]*1.2,preds[:,1],preds[:,2],c="cyan", alpha=1.0, edgecolor='b')
ax.plot3D(preds[:17,0]*1.2,preds[:17,1], preds[:17,2], color='blue' )
ax.plot3D(preds[17:22,0]*1.2,preds[17:22,1],preds[17:22,2], color='blue')
ax.plot3D(preds[22:27,0]*1.2,preds[22:27,1],preds[22:27,2], color='blue')
ax.plot3D(preds[27:31,0]*1.2,preds[27:31,1],preds[27:31,2], color='blue')
ax.plot3D(preds[31:36,0]*1.2,preds[31:36,1],preds[31:36,2], color='blue')
ax.plot3D(preds[36:42,0]*1.2,preds[36:42,1],preds[36:42,2], color='blue')
ax.plot3D(preds[42:48,0]*1.2,preds[42:48,1],preds[42:48,2], color='blue')
ax.plot3D(preds[48:,0]*1.2,preds[48:,1],preds[48:,2], color='blue' )

ax.view_init(elev=90., azim=90.)
ax.set_xlim(ax.get_xlim()[::-1])
plt.show()

Like so:像这样:

在此处输入图片说明

However, I need to train a model "from face landmarks to face", and thus I need an image only with face landmarks.但是,我需要训练一个“从面部标志到面部”的模型,因此我只需要一张带有面部标志的图像。

How do I tweak the code above in order to plot an image with face landmarks only, with no background?我如何调整上面的代码以绘制只有面部标志的图像,没有背景?

may be you can try something like that:也许你可以尝试这样的事情:

markersize = 3

output = np.zeros(shape=[width, height, 3], dtype=np.uint8) #black
#output = 255 * np.ones(shape=[512, 512, 3], dtype=np.uint8) # white

def DrawElement(input, points, color, isClosed = False, showHidden = False):

    if( showHidden == False):
        hide_threshold = -3.0
        hidden = []
        for i in range(0,len(points)):
            if( points[i][2] < hide_threshold ):    
                hidden.append(i)
        if( len(hidden) > 0):
            points = np.delete(points, hidden, axis=0)

    size = len(points)
    if( size > 0):    
        x_points = points[0:size,0]
        y_points = points[0:size,1]

        for i in range(0,len(x_points)):
            cv2.circle(input, (x_points[i], y_points[i]), 1, color, markersize)

        pts = np.array([np.column_stack((x_points,y_points))], np.int32)
        pts = pts.reshape((-1,1,2))
        cv2.polylines(input, [pts],isClosed,color)

def DrawFace(input, points):

    DrawElement(input, preds[0:17], (0,255,0)) # face

    DrawElement(input, preds[17:22], (255,0,0)) # eyebrows dx
    DrawElement(input, preds[22:27], (0,0,255)) # eyebrows sx

    DrawElement(input, preds[27:31], (255,255,0),False, True) # nose
    DrawElement(input, preds[31:36], (255,255,100)) # nostrils

    DrawElement(input, preds[36:42], (0,255,255), True) # eye dx
    DrawElement(input, preds[42:48], (0,128,255), True) # eye sx

    DrawElement(input, preds[48:60], (255,255,255), True) # mouth
    DrawElement(input, preds[60:68], (255,100,255), True) # mouth opening

DrawFace(output, preds)

cv2.imwrite('output.jpg',output)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM