赞
踩
将用于PFLD的数据集处理成只有嘴巴包围盒+标注的数据集
首先通过matplotlib库函数画出所有的标记,在通过删减点的个数来确定关键点标记在嘴巴的位置
例如使用IBUG人脸标记数据集做测试,每张图片有106个标记点位置,其中第84-104 有20个标记点是嘴巴
- # 将数据集处理成嘴巴标记和嘴巴包围盒的数据
-
- import cv2
- import os
- import matplotlib
-
- #将人脸图片画上标记输出展示效果
- picture_set = []
-
- #读取原图片目录
- picture_path = "./traindata/IBUG/picture" #原图片目录
- for item in os.listdir(picture_path):
- if ('jpg' not in item):
- continue
- picture_set.append(os.path.join(picture_path, item))
-
-
- save_path = "./output_landmark" #输出图像目录
- if not os.path.exists(save_path):
- os.makedirs(save_path)
-
- image = cv2.imread(picture_set[0])
- land_path = "./traindata/IBUG/landmark"
- land_path = os.path.join(land_path, (picture_set[0] + ".txt").split("/")[-1])
- print(land_path)
- count = 0
- # 33 个面部轮廓点 < 35
- # 最后两个关键点是眼睛的中心 106 107 去掉
- # 85 -105 行 是嘴巴关键点的标注
- with open(land_path) as read_file:
- for line in read_file:
- count = count + 1
- if(count < 85) :continue
- pos = line.split()
- x=int(float(pos[0])*1000000)
- y=int(float(pos[1])*1000000)
- x1=int(float(pos[0]))
- x2=int(float(pos[1]))
- #画出标记
- print(count)
- cv2.circle(image,(x1,x2),2,(0,0,255),-1)
-
- cv2.imwrite(save_path + "/%s.jpg" % (land_path.split("/")[-1]), image)
-
-
-
-
-
-

所以将每个数据集的关键点位置文本标记只留下第84-104行 的坐标位置,并且计算出包围盒数据另外输出在一个txt文件中
--输出嘴巴关键点标记:
- # 将数据集处理成嘴巴标记和嘴巴包围盒的数据
-
- import cv2
- import os
-
-
- #将人脸图片画上标记输出展示效果
- picture_set = []
-
- dirpath="IBUG" #图片所在文件夹名字
-
- #读取原图片目录
- picture_path = "./traindata/"+dirpath+"/picture" #原图片目录
- for item in os.listdir(picture_path):
- if ('jpg' not in item):
- continue
- picture_set.append(os.path.join(picture_path, item))
-
-
- save_path = "./output_landmark" #输出图像目录
- if not os.path.exists(save_path):
- os.makedirs(save_path)
-
-
- pic_num=0
- for imagepath in picture_set:
- count = 0
- land_path = "./traindata/"+dirpath+"/landmark"
- land_path = os.path.join(land_path, (imagepath + ".txt").split("/")[-1])
- with open(save_path + "/%s" % (land_path.split("/")[-1]),'w') as input_file: #打开输入文件
- with open(land_path) as read_file:
- for line in read_file:
- count = count + 1
- if (count < 85 or count > 105): continue
- input_file.write(line)
- # 画出标记
- pic_num=pic_num+1
- print("%d done" % pic_num)
-
- # 33 个面部轮廓点 < 35
- # 最后两个关键点是眼睛的中心 106 107 去掉
- # 85 -105 行 是嘴巴关键点的标注

----计算嘴巴包围盒数据并保存在另一个文件夹box中
- # 将数据集处理成嘴巴标记和嘴巴包围盒的数据
-
- import cv2
- import os
-
- save_path = "./box" #输出图像目录
- if not os.path.exists(save_path):
- os.makedirs(save_path)
-
- source_set=[] #源目录文件列表
- source_path="./landmark"
- for item in os.listdir(source_path):
- if ('txt' not in item):
- continue
- source_set.append(os.path.join(source_path, item))
-
- test=[]
- test.append(source_set[0])
-
- pic_num=0
- for path in source_set:
- count = 0
- with open(save_path + "/%s" % (path.split("/")[-1]),'w') as input_file: #打开输入文件
- max_x = 0.00
- max_y = 0.00
- min_x = 9999.9
- min_y = 9999.9
- with open(path) as read_file:
- for line in read_file:
- pos = line.split()
- x = float(pos[0])
- y = float(pos[1])
- if(x>max_x):max_x=x
- if(x<min_x):min_x=x
- if(y>max_y):max_y=y
- if(y<min_y):min_y=y
- box="%f %f %f %f" %(min_x,min_y,max_x-min_x,max_y-min_y)
- input_file.write(box)
-
- # 画出标记
- pic_num=pic_num+1
- print("%d done" % pic_num)
-
- #测试包围盒是否准确
- # image = cv2.imread("./traindata/IBUG/picture/IBUG_image_003_1_0.jpg")
- #
- # with open("./box/IBUG_image_003_1_0.jpg.txt") as read_file:
- # for line in read_file:
- # pos = line.split()
- # x1=int(float(pos[0]))
- # x2=int(float(pos[1]))
- # width=int(float(pos[2]))
- # height=int(float(pos[3]))
- # #画出标记
- # print("%d %d %d %d" %(x1,x2,width,height))
- # cv2.rectangle(image,(x1,x2),(x1+width,x2+height),(0,0,255),3)
- #
- #
- # cv2.imwrite("out.jpg", image)
-
-
-

///***整合包围盒数据,标记点数据,格式化数据
- import os
-
-
- source_set=[]
- source_path = "./box"
-
- pic_path="./picture/"
- land_path="./landmark/"
-
- for item in os.listdir(source_path):
- if ('txt' not in item):
- continue
- source_set.append(os.path.join(source_path, item))
-
- with open("mouth_train.txt",'a') as input: #写入包围盒数据
- for path in source_set:
- with open(path) as elem:
- content=elem.readline()
- filename= path.split("/")[-1]
- filename=filename.strip(".txt")
- input.write(pic_path+filename+" ")
- input.write(content+"\n")
-
- with open("mouth_train_box.txt",'a') as input: #整合包围合数据
- for path in source_set:
- with open(path) as elem:
- content=elem.readline()
- filename= path.split("/")[-1]
- filename=filename.strip(".txt")
- input.write(pic_path+filename+"\n")
- input.write("1\n")
- input.write(content+"\n")
-
-
- with open("trainImageList.txt",'a') as input: #整合关键点数据
- for path in source_set:
- with open(path) as elem:
- content=elem.readline()
- filename= path.split("/")[-1]
- filename=filename.strip(".txt")
- input.write(pic_path+filename+" ")
- input.write(content+" ")
-
- filename = path.split("/")[-1]
- filename=land_path + filename
- with open(filename) as f:
- for line in f:
- line=line.strip('\n')
- input.write(line)
- input.write("\n")

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。