赞
踩
我们在进行人脸属性识别深度学习算法研究过程中除了使用开源带标签的数据以外,都会根据具体使用场景与需求用到大量自收集的图像数据(开源/爬虫/自拍等),然这些数据一般是没有人脸对应属性标注标签的。而我们在研究人脸各种检测算法时最终训练需要的数据就是图像+标签,所以如何快速标注这些特定数据便是数据收集工作的重点。本文主要讲一下如何通过python工具辅助标注人脸关键点数据,在此做一个分享。
version: 1
n_points: 68
{
x0 y0
x1 y1
......
x67 y67
}
# 设置显示图片
im = Image.open(img_path)
# 创建figure(绘制面板)、创建图表(axes)
self.fig, self.ax = plt.subplots()
plt.imshow(im)
# 设置标题
self.ax.set_title(img_path + "\n" + 'Click and drag a point to move it, this will update the ax txt!')
# 设置坐标轴范围
self.img_size = im.size
self.ax.set_xlim(0, self.img_size[0]) # 图片像素宽度
self.ax.set_ylim(0, self.img_size[1]) # 图片像素高度
self.ax.xaxis.set_ticks_position('top') # 将x轴的位置设置在顶部
self.ax.invert_yaxis() # y轴反向
def getFacePoit(self, img_path): """ 根据标签文件中的坐标在人脸图片上绘制关键点 :param img_path: 待标注图片路径 :param txt_path: 标注文件路径 :return: """ x = [] y = [] self.txt_path = "" # 根据图片路径求得标签文件路径 if os.path.splitext(img_path)[1] == '.jpg' or os.path.splitext(img_path)[1] == '.png': fileName = os.path.splitext(img_path)[0] self.txt_path = fileName + ".pts" # 判断标签文件后是否存在 if not os.path.exists(self.txt_path): with open(self.txt_path, 'a') as fw: fw.write("version: 1" + "\n") fw.write("n_points: 68" + "\n") fw.write("{" + "\n") fw.close() # 调用模型输出并写入预测关键点坐标 try: landmarks, x1, y1 = PFLD.getLandMarks(img_path) # if landmarks is not None: for (x0, y0) in landmarks.astype(np.float32): with open(self.txt_path, 'a') as fw: fw.write('%.7s' % str(x0+x1) + " " + '%.7s' % str(y0+y1) + "\n") fw.close() x.append(float('%.7s' % str(x0+x1))) y.append(float('%.7s' % str((y0+y1)))) with open(self.txt_path, 'a') as fw: fw.write("}") fw.close() except Exception: os.remove(self.txt_path) print("该图片未检测到人脸,无法输出标签数据") else: # 直接读取标签文件获取关键点信息 try: num_of_line = 1 with open(self.txt_path, 'r') as f: while True: line = f.readline() print(line) if num_of_line <= 4: print("非坐标行") elif num_of_line > 4 and num_of_line < 73: num = list(map(float, line.strip().split())) # 坐标变换 x.append(float('%.7s' % str(num.__getitem__(0)))) y.append(float('%.7s' % str(num.__getitem__(1)))) else: break num_of_line = num_of_line + 1 except Exception: os.remove(self.txt_path) print("该标签数据不完整,请重新运行本程序写入") return x, y
# 设置关键点初始值
self.x, self.y = self.getFacePoit(img_path) # 调用模型或读取标签文件中关键点坐标信息
# 绘制2D的点到图像中
self.line = Line2D(self.x, self.y, ls="", marker='.', markersize=2, markerfacecolor='g', animated=True)
self.ax.add_line(self.line)
# 标志值设为none
self._ind = None
# 设置画布,方便后续画布响应事件
canvas = self.fig.canvas
self.fig.canvas.mpl_connect('draw_event', self.draw_callback)
self.fig.canvas.mpl_connect('button_press_event', self.button_press_callback)
self.fig.canvas.mpl_connect('button_release_event', self.button_release_callback)
self.fig.canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
self.canvas = canvas
plt.show()
至此,我们的人脸关键点标注工具便开发完成了,完美解决了人脸关键点信息标注难的问题,极大提升了标注工作的效率,不知各位大佬是否还有其他更好的方法,欢迎评论区交流讨论。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。