下面的代码将face-aligment
提供给我输入的任何照片。
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()
像这样:
但是,我需要训练“从人脸地标到人脸”的模型,因此我只需要带有人脸地标的图像。
如何调整上面的代码,以绘制仅具有脸部界标且没有背景的图像?
答案 0 :(得分:0)
也许您可以尝试类似的事情:
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)