【问题标题】:dlib/cv2 Working with hundred thousand of picturesdlib/cv2 处理十万张图片
【发布时间】:2019-05-02 22:36:10
【问题描述】:

对于我的下一个大学项目,我将不得不教授卷积神经网络如何对人脸图片进行去噪,因此我开始挖掘我们的人脸数据集。我偶然发现了这个数据集 (CelebA),其中包含 20 万多张人物图片,我发现了最初的几个问题:图片太多,无法对它们进行基本计算。

我应该:

  1. 打开每个图像并从中创建一个 numpy 数组(dlib.load_rgb_image 很好)
  2. 找到一张脸,使用 5 点形状预测器找到眼睛并对齐它们
  3. 旋转图片,使眼睛在一条水平线上
  4. 裁剪面部并将其调整为 256x256(我可以选择 64x64,但它不会节省大量时间)
  5. 复制一份并添加人工噪音
  6. 将它们保存到两个不同的文件夹中

在大学给我的一台电脑上,我每分钟可以拍大约 40 张图像,每 24 小时大约可以拍 57k 张图像。

为了加速我尝试过的线程;每张图片一个线程,但加速比每分钟多 2-3 张图片。

这是我正在运行的代码:

### Out of the threads, before running them ###
def img_crop(img, bounding_box):
    # some code using cv2.copyMakeBorder to crop the image

MODEL_5_LANDMARK = "5_point.dat"
shape_preditor = dlib.shape_predictor(MODEL_5_LANDMARK)
detector = dlib.get_frontal_face_detector()


### Inside each thread ###
img_in = dlib.load_rgb_image("img_in.jpg")
dets = detector(img_in, 1)
shape = shape_preditor(img_in, dets[0])

points = []
for i in range(0, shape.num_parts):
    point = shape.part(i)
    points.append((point.x, point.y))

eye_sx = points[1]
eye_dx = points[3]

dy = eye_dx[1] - eye_sx[1]
dx = eye_dx[0] - eye_sx[0]
angle = math.degrees(math.atan2(dy, dx))

center = (dets[0].center().x, dets[0].center().y)
h, w, _ = img_in.shape
M = cv2.getRotationMatrix2D(center, angle + 180, 1)
img_in = cv2.warpAffine(img_in, M, (w, h))

dets = detector(img_in, 1)
bbox = (dets[0].left(), dets[0].top(), dets[0].right(), dets[0].bottom())
img_out = cv2.resize(imcrop(img_in, bbox), (256, 256))
img_out = cv2.cvtColor(img_out, cv2.COLOR_BGR2RGB)

img_noisy = skimage.util.random_noise(img_out, ....)
cv2.imwrite('out.jpg', img_out)
cv2.imwrite('out_noise.jpg', img_noisy)

我的编程语言是 Python3.6,我怎样才能加快速度?

另一个问题是将整个 200k 图像作为 numpy 数组加载到内存中,从我最初的测试开始,12k 图像将需要大约 80 秒,最终形状为 (12000, 256, 256, 3)。有没有更快的方法来实现这一点?

【问题讨论】:

    标签: python python-3.x image dlib cv2


    【解决方案1】:

    首先,请原谅我,因为我只熟悉 c++。请在下面找到我的建议,以加快 dlib 函数并在有帮助的情况下转换为您的 python 版本。

    1. 颜色对 dlib 无关紧要。因此,在继续之前将输入图像更改为灰色以节省时间。

    2. 我看到你两次调用下面的函数,目的是什么?它可以使耗时加倍。如果对齐后需要获取新的landmarks,可以尝试直接旋转landmarks点,而不是重新检测。 How to rotate points

      dets = detector(img_in, 1)
      
    3. 因为您只想为每张图片检测 1 张人脸。尝试将 pyramid_down 设置为 6(默认为 1 - 将图像留出空间以检测更多人脸)。您可以测试 1 - 6 的值

      dets = detector(img_in, 6)
      
    4. 开启 AVX 指令。

    注意:更多细节可以在这里找到Dlib Github

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多