数据集:
这是带有 SPIGA 个地标注释的 Microsoft FaceSynthetics dataset 的副本。要获取没有额外注释的原始FaceSynthetics数据集的副本,请参考 pcuenq/face_synthetics 。
请参考原始的 许可证 ,我们在本仓库中复制了该许可证。 SPIGA注释由Hugging Face Inc.创建,并在MIT许可下分发。
使用以下代码准备了这个数据集。它通过遍历数据集执行SPIGA进行地标检测,并创建特征的可视化。使用Matplotlib进行可视化,将其渲染到内存缓冲区。
import numpy as np
from datasets import load_dataset
from spiga.inference.config import ModelConfig
from spiga.inference.framework import SPIGAFramework
dataset_name = "pcuenq/face_synthetics"
faces = load_dataset(dataset_name)
faces = faces["train"]
# ## Obtain SPIGA features
processor = SPIGAFramework(ModelConfig("300wpublic"))
# We obtain the bbox from the existing landmarks in the dataset.
# We could use `dlib`, but this should be faster.
# Note that the `landmarks` are stored as strings.
def parse_landmarks(landmarks_str):
landmarks = landmarks_str.strip().split('\n')
landmarks = [k.split(' ') for k in landmarks]
landmarks = [(float(x), float(y)) for x, y in landmarks]
return landmarks
def bbox_from_landmarks(landmarks_str):
landmarks = parse_landmarks(landmarks_str)
landmarks_x, landmarks_y = zip(*landmarks)
x_min, x_max = min(landmarks_x), max(landmarks_x)
y_min, y_max = min(landmarks_y), max(landmarks_y)
width = x_max - x_min
height = y_max - y_min
# Give it a little room; I think it works anyway
x_min -= 5
y_min -= 5
width += 10
height += 10
bbox = (x_min, y_min, width, height)
return bbox
def spiga_process(example):
image = example["image"]
image = np.array(image)
# BGR
image = image[:, :, ::-1]
bbox = bbox_from_landmarks(example["landmarks"])
features = processor.inference(image, [bbox])
landmarks = features["landmarks"][0]
example["spiga"] = landmarks
return example
# For some reason this map doesn't work with num_proc > 1 :(
# TODO: run inference on GPU
faces = faces.map(spiga_process)
# ## "Segmentation"
# We use bezier paths to draw contours and areas.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Path
import PIL
def get_patch(landmarks, color='lime', closed=False):
contour = landmarks
ops = [Path.MOVETO] + [Path.LINETO]*(len(contour)-1)
facecolor = (0, 0, 0, 0) # Transparent fill color, if open
if closed:
contour.append(contour[0])
ops.append(Path.CLOSEPOLY)
facecolor = color
path = Path(contour, ops)
return patches.PathPatch(path, facecolor=facecolor, edgecolor=color, lw=4)
# Draw to a buffer.
def conditioning_from_landmarks(landmarks, size=512):
# Precisely control output image size
dpi = 72
fig, ax = plt.subplots(1, figsize=[size/dpi, size/dpi], tight_layout={'pad':0})
fig.set_dpi(dpi)
black = np.zeros((size, size, 3))
ax.imshow(black)
face_patch = get_patch(landmarks[0:17])
l_eyebrow = get_patch(landmarks[17:22], color='yellow')
r_eyebrow = get_patch(landmarks[22:27], color='yellow')
nose_v = get_patch(landmarks[27:31], color='orange')
nose_h = get_patch(landmarks[31:36], color='orange')
l_eye = get_patch(landmarks[36:42], color='magenta', closed=True)
r_eye = get_patch(landmarks[42:48], color='magenta', closed=True)
outer_lips = get_patch(landmarks[48:60], color='cyan', closed=True)
inner_lips = get_patch(landmarks[60:68], color='blue', closed=True)
ax.add_patch(face_patch)
ax.add_patch(l_eyebrow)
ax.add_patch(r_eyebrow)
ax.add_patch(nose_v)
ax.add_patch(nose_h)
ax.add_patch(l_eye)
ax.add_patch(r_eye)
ax.add_patch(outer_lips)
ax.add_patch(inner_lips)
plt.axis('off')
fig.canvas.draw()
buffer, (width, height) = fig.canvas.print_to_buffer()
assert width == height
assert width == size
buffer = np.frombuffer(buffer, np.uint8).reshape((height, width, 4))
buffer = buffer[:, :, 0:3]
plt.close(fig)
return PIL.Image.fromarray(buffer)
def spiga_segmentation(example):
landmarks = example["spiga"]
example['spiga_seg'] = conditioning_from_landmarks(landmarks)
return example
faces = faces.map(spiga_segmentation, num_proc=16)
faces.push_to_hub(f"{dataset_name}_spiga")