def getDataFromTxt(txt,data_path, with_landmark=True):
"""
Generate data from txt file
return [(img_path, bbox, landmark)]
bbox: [left, right, top, bottom]
landmark: [(x1, y1), (x2, y2), ...]
"""
with open(txt, 'r') as fd: #打开txt路径对应的文档
lines = fd.readlines() #读取文档内容
result = [] #先定义一个元组
for line in lines: #读取每一行的内容
line = line.strip() #去掉每一行首尾的换行符
components = line.split(' ') #以空格为分割符分开每一行的元素
img_path = os.path.join(data_path, components[0]).replace('\\','/')
# 将\\替换为/后把图片的路径储存起来
# bounding box, (x1, y1, x2, y2)
#bbox = (components[1], components[2], components[3], components[4])
bbox = (components[1], components[3], components[2], components[4])
#将bbox重新排序变成(x1,x2,y1,y2)的形式
bbox = [float(_) for _ in bbox] #先统一转换坐标为浮点型数据
bbox = list(map(int,bbox)) #再将数据统一转换为整数型
# landmark
if not with_landmark: #with_landmark传入此函数时默认为True
result.append((img_path, BBox(bbox))) #在result列表中加入路径和人脸框的坐标,BBox是一个类,BBox(bbox)返回了一个对象地址
continue
landmark = np.zeros((5, 2)) #先将landmark初始化为5行2列的全0数组
for index in range(0, 5): #获取5个landmark点坐标
rv = (float(components[5+2*index]), float(components[5+2*index+1]))
landmark[index] = rv
#normalize
'''
for index, one in enumerate(landmark):
rv = ((one[0]-bbox[0])/(bbox[2]-bbox[0]), (one[1]-bbox[1])/(bbox[3]-bbox[1]))
landmark[index] = rv
'''
result.append((img_path, BBox(bbox), landmark))
return result
【tensorflow】MTCNN网络基本函数getDataFromTxt
最新推荐文章于 2023-05-26 21:36:44 发布