在Windows 10环境中,我们使用dlib19.17+face_recognition对家庭照片进行分类,速度可以达到4-6秒/张,相关内容请参考:
Windows 10+dlib19.17+face_recognition:使用人脸识别对家庭照片进行分类,4-6秒/张(一)单线程
我们对这样的速度不太满意,打算提高它的运行速度(或者说:缩短整个任务的运行时间),在上一篇文章中我们发现Python的多线程不能提高并行效率,但可以用同时运行多个程序来缩短任务运行时间:
Windows 10+dlib19.17+face_recognition:使用人脸识别对家庭照片进行分类,4-6秒/张(二)并行加速
(1)多进程并行加速
在本文中,我们尝试用Python多进程来提高任务执行效率,代码如下:
# -*- coding: UTF-8 -*-
import dlib
import face_recognition
import numpy as np
from multiprocessing import Process
from multiprocessing import Pool
from datetime import datetime
import os
import shutil
#全局变量
number_process = 3
#人脸检测进程,将文件名转化为整数,按整除的余数分类待处理文件,若命中则复制文件到指定目录
def face_check_and_copy(num):
# 从图片中加载已知的人脸并获得编码
steve_image = face_recognition.load_image_file("f:/images/steve.jpg")
steve_face_encoding = face_recognition.face_encodings(steve_image)[0]
lucy_image = face_recognition.load_image_file("f:/images/lucy.jpg")
lucy_face_encoding = face_recognition.face_encodings(lucy_image)[0]
known_face_encodings = [
steve_face_encoding,
lucy_face_encoding,
]
known_face_names = [
"Steve",
"Lucy",
]
count_checked, count_copied, count_failed = 0, 0, 0
# 遍历目录下的所有.jpg文件
f = os.walk("f:\images")
for path, d, filelist in f:
for filename in filelist:
if filename.endswith('jpg'):
#将文件名的字符转化为ASCII码,累计得到一个整数
only_name = os.path.splitext(filename)[0]
int_filename = 0
for every_char in only_name:
int_filename += ord(every_char)
if int_filename % number_process == num:
image_path = os.path.join(path, filename)
# 加载图片
try:
unknown_image = face_recognition.load_image_file(image_path)
except:
print('%s.jpg load_image_file() failed.' %only_name)
count_failed += 1
continue
# 找到图中所有人脸的位置
try:
face_locations = face_recognition.face_locations(unknown_image)
except:
print('%s.jpg face_location() failed.' %only_name)
count_failed += 1
continue
# face