admin管理员组

文章数量:1122850

Multi

数据集下载链接:

.html

github地址:

 

Multi-Task Facial Landmark (MTFL人脸数据库),这个数据库包括了12,995张人脸图片,每张图片都被做了一些标注。包括
(1)5个人脸特征点,包括左眼、右眼、鼻子、左嘴角、右嘴角。
(2)标记性别、微笑、眼镜、姿态。


标记数据格式

数据库被分成两个部分:training和tesing,标记的信息全部存放在txt文件中(traning和testing文件)。
文件中的每一行为一张人脸图片的标记信息,他的格式如下:
image path x1...x5,y1..y5 gender smile wearing glasses head pose
--x1...x5,y1...y5: 标记了左眼、右眼、鼻子、左嘴角、右嘴角的坐标位置。
--gender: 1代表男, 2代表女
--smile: 1代表微笑, 2代表不微笑。
--glasses: 1代表戴眼镜, 2代表没戴眼镜。
--head pose: 1 for left profile,2 for left, 3 for frontal, 4 for right, 5 for right profile


对数据预处理文件的解读

  1. from PIL import Image
  2. import os
  3. import numpy as np
  4. #数据集存放地址
  5. folder = os.path.abspath(os.path.join("./", os.pardir)+"/MTFL") + "/"
  6. #最终要将图全部resize成这个大小150*150
  7. finalSize = 150
  8. counter = 0
  9. infoFiles = ["training.txt", "testing.txt"]
  10. validation_counter = 0
  11. #建立一个validation.txt文件用于存放验证数据信息
  12. val_file = open(folder + "validation.txt","a")
  13. #"training.txt", "testing.txt"中的信息进行处理
  14. for idx in range(len(infoFiles)):
  15. info = infoFiles[idx]
  16. #当idx=0时读取training.txt,当idx=1时读取testing.txt
  17. f = open(folder+info,"r")
  18. #打开一个新的文件,用于写入信息
  19. fnew = open("tmp", 'a')
  20. #当读取training.txt信息时,创建aug_training.txt文件
  21. if idx == 0:
  22. fnew_augmented = open(folder+"aug_"+info,'a')
  23. lines = f.readlines()
  24. for line in lines:
  25. line = line.strip("\n ").split(" ")
  26. #获取到图片名称
  27. imgName = line[0].replace("\\", "/")
  28. if imgName == "":
  29. break
  30. #打开图片,读取图片的宽高
  31. img = Image.open(folder+imgName, mode='r')
  32. originalWidth = img.width
  33. originalHeight = img.height
  34. if (originalWidth != originalHeight):
  35. # Skip non-square images,若高宽不一致则跳过
  36. img.close()
  37. continue
  38. # Resize and check RGB,将图片resize到150*150
  39. if(originalWidth != finalSize):
  40. img = img.resize((finalSize, finalSize), Image.ANTIALIAS) # Image.ANTIALIAS代表高质量
  41. # Check if img is RGB or greyscale,若图片不是彩色图,则跳过
  42. pixels = list(img.getdata())
  43. width, height = img.size
  44. pixelsArray = np.asarray([pixels[i * width:(i + 1) * width] for i in range(height)])
  45. if len(pixelsArray.shape) != 3:
  46. # img is greyscale, skip it
  47. img.close()
  48. continue
  49. coords = []
  50. # 图片的缩放比例,若大小没有被调整该系数为1,若图像大小调整,同比缩放坐标数据,记录在coords中line[i][1]-line[i][10]
  51. # 对应x1,x2,x3,x4,x5,y1,y2,y3,y4,y5: 标记了左眼、右眼、鼻子、左嘴角、右嘴角的坐标位置
  52. coordsScaleFactor = float(finalSize) / float(originalWidth)
  53. for i in range(1,11):
  54. coords.append(float(line[i])*coordsScaleFactor)
  55. #属性gender,smile,glasses,head pose分别对应每行line[i][11]-line[i][14]
  56. attributes = np.array([int(line[i]) for i in range(11,15)])
  57. #对testing.txt进行读取,生成1000个用于验证的图片信息于validation.txt中
  58. if(idx == 1 and validation_counter <= 1000):
  59. #line[i][0]
  60. val_file.write(imgName)
  61. #写入特征点信息
  62. for coord in coords:
  63. val_file.write(" " + str(coord))
  64. #写入属性信息
  65. for attribute in attributes:
  66. val_file.write(" " + str(attribute - 1)) # Subtract 1 for better indexing
  67. val_file.write("\n")
  68. validation_counter += 1
  69. # '''
  70. # --gender: 1代表男, 2代表女
  71. # --smile: 1代表微笑, 2代表不微笑。
  72. # --glasses: 1代表戴眼镜, 2代表没戴眼镜。
  73. # --head pose: 1 for left profile,2 for left, 3 for frontal, 4 for right, 5 for right profile
  74. # attribute - 1:是为了使索引从0开始,方便训练
  75. # '''
  76. #剩下的写入fnew中用于训练
  77. else:
  78. # Write resized img to file
  79. fnew.write(imgName)
  80. for coord in coords:
  81. fnew.write(" " + str(coord))
  82. for attribute in attributes:
  83. fnew.write(" " + str(attribute - 1)) # Subtract 1 for better indexing
  84. fnew.write("\n")
  85. # Mirror the image if it's not part of test data
  86. if idx == 0:
  87. # Get the new img name,生成翻转图像的名称./MTFL/lfw_5590/Aaron_Eckhart_0001.jpg-->./MTFL/lfw_5590/Aaron_Eckhart_0001_transl.jpg
  88. splitName = imgName.split('.')
  89. imgNameTransp = splitName[0] + '_transl.' + splitName[1]
  90. # Mirror the image and save it
  91. #左右翻转图像,存图
  92. imgTransp = img.copy().transpose(Image.FLIP_LEFT_RIGHT)
  93. imgTransp.save(folder+imgNameTransp)
  94. imgTransp.close()
  95. #生成对应翻转图像特征点及属性特征
  96. coordsTransp = [0 for i in range(10)]
  97. # Translate x-coords for eyes, nose, and mouth
  98. coordsTransp[0] = 150 - coords[1] #左眼睛x1
  99. coordsTransp[1] = 150 - coords[0] #右眼睛x2
  100. coordsTransp[2] = 150 - coords[2] #鼻子x3
  101. coordsTransp[3] = 150 - coords[4] #左嘴角X4
  102. coordsTransp[4] = 150 - coords[3] #右嘴角x5
  103. # Translate y-coords for eyes, nose, and mouth
  104. coordsTransp[5] = coords[6] #左眼睛y1
  105. coordsTransp[6] = coords[5] #右眼睛y2
  106. coordsTransp[7] = coords[7] #鼻子y3
  107. coordsTransp[8] = coords[9] #左嘴角y4
  108. coordsTransp[9] = coords[8] #右嘴角y5
  109. # Translate attributes 属性变换gender,smile,glasses,head pose
  110. attributesTransp = np.array([int(line[i]) for i in range(11,15)])
  111. attributesTransp[3] = 6 - attributesTransp[3] # Translate head:1 for left profile,2 for left, 3 for frontal, 4 for right, 5 for right profile
  112. # Write resized old img to augmented file,写入原图的图像信息
  113. fnew_augmented.write(imgName)
  114. for coord in coords:
  115. fnew_augmented.write(" " + str(coord))
  116. for attribute in attributes:
  117. fnew_augmented.write(" " + str(attribute - 1))
  118. fnew_augmented.write("\n")
  119. # Write mirrored img to augmented file,写入翻转后的图像信息
  120. fnew_augmented.write(imgNameTransp)
  121. for coord in coordsTransp:
  122. fnew_augmented.write(" " + str(coord))
  123. for attribute in attributesTransp:
  124. fnew_augmented.write(" " + str(attribute - 1))
  125. fnew_augmented.write("\n")
  126. # Save resized img 存的都是resize到150*150的图片
  127. img.save(folder+imgName)
  128. img.close()
  129. counter = counter + 1
  130. if counter % 1000 == 0:
  131. print(counter,"files processed")
  132. #对"training.txt"/"testing.txt"中的每一行读完之后,就关闭该文件
  133. f.close()
  134. #对"training.txt"/"testing.txt"中的每一行数据进行操作完成,写入完毕后,就关闭该"tmp"文件
  135. fnew.close()
  136. #关闭aug_training.txt
  137. if idx == 0:
  138. fnew_augmented.close()
  139. #删除之前的"training.txt"/"testing.txt"
  140. os.remove(folder+info)
  141. #将tmp重命名为"training.txt"/"testing.txt"
  142. os.rename("tmp",folder+info)
  143. #都循环完毕后,关闭validation.txt
  144. val_file.close()

 

本文标签: Multi