python - Matplotlib - 绘制没有背景图像的人脸地标
问题描述
下面的代码提供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()
像这样:
但是,我需要训练一个“从人脸标志到人脸”的模型,因此我只需要一张带有人脸标志的图像。
如何调整上面的代码以绘制仅具有面部标志的图像,没有背景?
解决方案
也许你可以尝试这样的事情:
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)
推荐阅读
- python - Escaping spaces in prefix argument in the boto3's list_objects method
- linux - .NET Core Web API 更改默认端口 (5000)
- google-sheets - 带有 TEXTJOIN 的 ArrayFormula - 在同一行/单元格上合并两列的数据
- javascript - 添加新选项卡时,Angular Element 在 mat-tab-group 上被破坏
- elasticsearch - elasticsearch bool 查询添加逻辑
- python - 如何修复python中的“IndexError:字符串索引超出范围”错误
- android-studio - Flutter 用户应该运行 `flutter packages get` 而不是 `pub get`
- python - 避免在 python 中搜索合适的类型
- variables - 如何使用 Boto3 通过 Lambda 在 EC2 创建中将变量传递给 USERDATA
- wordpress - 如何使用自定义 HTML 小部件放置两个不同的短代码