def extract_data(path):
""" extract all data for feeding placeholder.
Args:
path -- the txt file each line with format
image_dir bounding box labeled points
Return:
images -- image cropped around face range and resized to 39*39
points -- points extracted from txt
factors -- scale factor
crds -- crop box
widths -- bounding box width
"""
images = []; points = []; factors = []; crds = []; widths = []
with open(path, "r") as f:
lines = f.readlines()
for line in lines:
a = line.split(); impath = a[0]; a = a[1:]
aa = []
for i in range(len(a)): aa.append(string.atof(a[i]))
bbox_width = aa[1] - aa[0]; bbox_height = aa[3] - aa[2]
widths.append(bbox_width)
left = int(bbox_width * ranges[0] + aa[0])
top = int(bbox_height * ranges[2] + aa[2])
if bbox_height >= bbox_width:
bottom = int(bbox_height * ranges[3] + aa[2])
height = bottom - top; diff = bbox_height - bbox_width
left = int(left - 0.5 * diff); right = left + height
factor = 39 / height
else:
right = int(bbox_width * ranges[1] + aa[0])
width = right - left; diff = bbox_width - bbox_height
top = int(top - 0.5*diff); bottom = top + width
factor = 39 / width
factors.append([factor])
box = (left, right, top, bottom); crds.append([left, top])
im = Image.open(impath); image = im.crop(box)
images.append(np.array(image.resize((39, 39))) / 255)
point_raw = aa[4:]; points.append(point_raw)
print(points[0])
return images, points, factors, crds, widths
评论列表
文章目录