dlib / cv2处理十万张图片 [英] dlib/cv2 Working with hundred thousand of pictures

查看:125
本文介绍了dlib / cv2处理十万张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For my next university-project i will have to teach a Convoluted Neural Network how to denoise a picture of a face so i started digging the we for datasets of faces. I stumbled upon this dataset (CelebA) with 200k+ pictures of people and i found the first few problems: there are too many pictures to do basic computation on them.

我应该:


  1. 打开每个图像,并制作一个numpy数组(dlib.load_rgb_image很好)

  2. 找到一张脸,使用5点形状预测器找到眼睛并对齐它们

  3. 旋转图片,使眼睛成一直线。

  4. 裁剪面部并将其大小调整为256x256(我可以选择64x64,但它不会节省很多时间)

  5. 制作副本并为其添加人工噪声

  6. 将它们都保存到两个不同的文件夹中

  1. Open each image and make a numpy array out of it (dlib.load_rgb_image is fine)
  2. Find a face it, use the 5 point shape predictor to find the eyes and align them
  3. Rotate the picture so that the eyes are in a straight horizontal line
  4. Crop the face and resize it to 256x256 (i could choose 64x64 but its not a huge time saver)
  5. Make a copy and add artificial noise to it
  6. Save them both to two different folder

在大学给我的电脑上,我每分钟可以处理约40张图片,每24小时可以处理约57k张图片。

On a pc that the university gave me i could do about 40ish image each minute, around 57k images every 24hours.

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

To speedup thing i have tried threads; one thread for each pictures but the speedup is about 2-3 images more per-minute.

这是我正在运行的代码:

This is the code i'm running:

### 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,如何提高速度?

My programming language is Python3.6, how i can speedup things?

另一个问题是将整个200k图像作为numpy数组加载到内存中,根据我的初始测试,12k图像将花费大约80秒的时间,最终形状为(12000、256、256、3)。

Another problem will be loading the whole 200k images into memory as numpy array, from my initial testing 12k images will take around 80seconds with a final shape of (12000, 256, 256, 3). Is there a faster way to achieve this?

推荐答案

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

First of all, forgive me because I am familiar with c++ only. Please find below my suggestion to speed up dlib functions and convert to your python version if it is helpful.


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

  1. Color does not matter to dlib. Hence, change input image to gray before proceeding to save time.

我看到您两次调用了以下函数,目的是什么?它可以使耗时增加一倍。如果需要在对齐后获取新的地标,请尝试直接旋转地标点,而不是重新检测。 如何旋转点

I saw you call the below function twice, what is the purpose? it could double the consuming time. If you need to get the new landmarks after alignment, try to rotate landmarks points directly instead of re-detecting. How to rotate points

dets = detector(img_in, 1)


  • 因为您只想每个图像检测1张脸。尝试将pyramid_down设置为6(默认情况下为1-为图像留出空间以检测更多人脸)。您可以测试1-6中的值

  • Because you just want to detect 1 face per image only. Try to set pyramid_down to 6 (by default it is 1 - room out the image to detect more face). You can test value from 1 - 6

    dets = detector(img_in, 6)
    


  • 打开AVX指令。

  • Turn on AVX instruction.

    注意:更多详细信息,请参见 Dlib Github

    Note: more detail could be found here Dlib Github

    这篇关于dlib / cv2处理十万张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆