【发布时间】:2019-05-02 22:36:10
【问题描述】:
对于我的下一个大学项目,我将不得不教授卷积神经网络如何对人脸图片进行去噪,因此我开始挖掘我们的人脸数据集。我偶然发现了这个数据集 (CelebA),其中包含 20 万多张人物图片,我发现了最初的几个问题:图片太多,无法对它们进行基本计算。
我应该:
- 打开每个图像并从中创建一个 numpy 数组(dlib.load_rgb_image 很好)
- 找到一张脸,使用 5 点形状预测器找到眼睛并对齐它们
- 旋转图片,使眼睛在一条水平线上
- 裁剪面部并将其调整为 256x256(我可以选择 64x64,但它不会节省大量时间)
- 复制一份并添加人工噪音
- 将它们保存到两个不同的文件夹中
在大学给我的一台电脑上,我每分钟可以拍大约 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