当前位置:   article > 正文

Multi-task Convolutional Neural Network 训练人脸识别和关键点检测 (python+tensoflow实现代码 算法步骤)---数据集处理_多数据集多任务学习人脸识别

多数据集多任务学习人脸识别

1.数据处理

将用于PFLD的数据集处理成只有嘴巴包围盒+标注的数据集

首先通过matplotlib库函数画出所有的标记,在通过删减点的个数来确定关键点标记在嘴巴的位置

例如使用IBUG人脸标记数据集做测试,每张图片有106个标记点位置,其中第84-104 有20个标记点是嘴巴

  1. # 将数据集处理成嘴巴标记和嘴巴包围盒的数据
  2. import cv2
  3. import os
  4. import matplotlib
  5. #将人脸图片画上标记输出展示效果
  6. picture_set = []
  7. #读取原图片目录
  8. picture_path = "./traindata/IBUG/picture" #原图片目录
  9. for item in os.listdir(picture_path):
  10. if ('jpg' not in item):
  11. continue
  12. picture_set.append(os.path.join(picture_path, item))
  13. save_path = "./output_landmark" #输出图像目录
  14. if not os.path.exists(save_path):
  15. os.makedirs(save_path)
  16. image = cv2.imread(picture_set[0])
  17. land_path = "./traindata/IBUG/landmark"
  18. land_path = os.path.join(land_path, (picture_set[0] + ".txt").split("/")[-1])
  19. print(land_path)
  20. count = 0
  21. # 33 个面部轮廓点 < 35
  22. # 最后两个关键点是眼睛的中心 106 107 去掉
  23. # 85 -105 行 是嘴巴关键点的标注
  24. with open(land_path) as read_file:
  25. for line in read_file:
  26. count = count + 1
  27. if(count < 85) :continue
  28. pos = line.split()
  29. x=int(float(pos[0])*1000000)
  30. y=int(float(pos[1])*1000000)
  31. x1=int(float(pos[0]))
  32. x2=int(float(pos[1]))
  33. #画出标记
  34. print(count)
  35. cv2.circle(image,(x1,x2),2,(0,0,255),-1)
  36. cv2.imwrite(save_path + "/%s.jpg" % (land_path.split("/")[-1]), image)

所以将每个数据集的关键点位置文本标记只留下第84-104行 的坐标位置,并且计算出包围盒数据另外输出在一个txt文件中

--输出嘴巴关键点标记:

  1. # 将数据集处理成嘴巴标记和嘴巴包围盒的数据
  2. import cv2
  3. import os
  4. #将人脸图片画上标记输出展示效果
  5. picture_set = []
  6. dirpath="IBUG" #图片所在文件夹名字
  7. #读取原图片目录
  8. picture_path = "./traindata/"+dirpath+"/picture" #原图片目录
  9. for item in os.listdir(picture_path):
  10. if ('jpg' not in item):
  11. continue
  12. picture_set.append(os.path.join(picture_path, item))
  13. save_path = "./output_landmark" #输出图像目录
  14. if not os.path.exists(save_path):
  15. os.makedirs(save_path)
  16. pic_num=0
  17. for imagepath in picture_set:
  18. count = 0
  19. land_path = "./traindata/"+dirpath+"/landmark"
  20. land_path = os.path.join(land_path, (imagepath + ".txt").split("/")[-1])
  21. with open(save_path + "/%s" % (land_path.split("/")[-1]),'w') as input_file: #打开输入文件
  22. with open(land_path) as read_file:
  23. for line in read_file:
  24. count = count + 1
  25. if (count < 85 or count > 105): continue
  26. input_file.write(line)
  27. # 画出标记
  28. pic_num=pic_num+1
  29. print("%d done" % pic_num)
  30. # 33 个面部轮廓点 < 35
  31. # 最后两个关键点是眼睛的中心 106 107 去掉
  32. # 85 -105 行 是嘴巴关键点的标注

----计算嘴巴包围盒数据并保存在另一个文件夹box中

  1. # 将数据集处理成嘴巴标记和嘴巴包围盒的数据
  2. import cv2
  3. import os
  4. save_path = "./box" #输出图像目录
  5. if not os.path.exists(save_path):
  6. os.makedirs(save_path)
  7. source_set=[] #源目录文件列表
  8. source_path="./landmark"
  9. for item in os.listdir(source_path):
  10. if ('txt' not in item):
  11. continue
  12. source_set.append(os.path.join(source_path, item))
  13. test=[]
  14. test.append(source_set[0])
  15. pic_num=0
  16. for path in source_set:
  17. count = 0
  18. with open(save_path + "/%s" % (path.split("/")[-1]),'w') as input_file: #打开输入文件
  19. max_x = 0.00
  20. max_y = 0.00
  21. min_x = 9999.9
  22. min_y = 9999.9
  23. with open(path) as read_file:
  24. for line in read_file:
  25. pos = line.split()
  26. x = float(pos[0])
  27. y = float(pos[1])
  28. if(x>max_x):max_x=x
  29. if(x<min_x):min_x=x
  30. if(y>max_y):max_y=y
  31. if(y<min_y):min_y=y
  32. box="%f %f %f %f" %(min_x,min_y,max_x-min_x,max_y-min_y)
  33. input_file.write(box)
  34. # 画出标记
  35. pic_num=pic_num+1
  36. print("%d done" % pic_num)
  37. #测试包围盒是否准确
  38. # image = cv2.imread("./traindata/IBUG/picture/IBUG_image_003_1_0.jpg")
  39. #
  40. # with open("./box/IBUG_image_003_1_0.jpg.txt") as read_file:
  41. # for line in read_file:
  42. # pos = line.split()
  43. # x1=int(float(pos[0]))
  44. # x2=int(float(pos[1]))
  45. # width=int(float(pos[2]))
  46. # height=int(float(pos[3]))
  47. # #画出标记
  48. # print("%d %d %d %d" %(x1,x2,width,height))
  49. # cv2.rectangle(image,(x1,x2),(x1+width,x2+height),(0,0,255),3)
  50. #
  51. #
  52. # cv2.imwrite("out.jpg", image)

///***整合包围盒数据,标记点数据,格式化数据

  1. import os
  2. source_set=[]
  3. source_path = "./box"
  4. pic_path="./picture/"
  5. land_path="./landmark/"
  6. for item in os.listdir(source_path):
  7. if ('txt' not in item):
  8. continue
  9. source_set.append(os.path.join(source_path, item))
  10. with open("mouth_train.txt",'a') as input: #写入包围盒数据
  11. for path in source_set:
  12. with open(path) as elem:
  13. content=elem.readline()
  14. filename= path.split("/")[-1]
  15. filename=filename.strip(".txt")
  16. input.write(pic_path+filename+" ")
  17. input.write(content+"\n")
  18. with open("mouth_train_box.txt",'a') as input: #整合包围合数据
  19. for path in source_set:
  20. with open(path) as elem:
  21. content=elem.readline()
  22. filename= path.split("/")[-1]
  23. filename=filename.strip(".txt")
  24. input.write(pic_path+filename+"\n")
  25. input.write("1\n")
  26. input.write(content+"\n")
  27. with open("trainImageList.txt",'a') as input: #整合关键点数据
  28. for path in source_set:
  29. with open(path) as elem:
  30. content=elem.readline()
  31. filename= path.split("/")[-1]
  32. filename=filename.strip(".txt")
  33. input.write(pic_path+filename+" ")
  34. input.write(content+" ")
  35. filename = path.split("/")[-1]
  36. filename=land_path + filename
  37. with open(filename) as f:
  38. for line in f:
  39. line=line.strip('\n')
  40. input.write(line)
  41. input.write("\n")

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号