.a
在使用Python进行系统管理时,特别是同时操作多个文件目录或者远程控制多台主机,并行操作可以节约大量的时间。如果操作的对象数目不大时,还可以直接使用Process类动态的生成多个进程
,十几个还好,但是如果上百个甚至更多,那手动去限制进程数量就显得特别的繁琐,此时进程池就派上用场了。
Pool类可以提供指定数量的进程供用户调用,当有新的请求提交到Pool中时,如果池还没有满,就会创建一个新的进程来执行请求。如果池满,请求就会告知先等待,直到池中有进程结束,才会创建新的进程来执行这些请求。
import os
import face_recognition
from multiprocessing.dummy import Pool pool = Pool(8) # 启动进程池 def helloface(known_face_encodings, known_face_names, unknown_pic):
"""
单个图片识别
:param known_face_encodings: single known img's encodings
:param known_face_names: all names, >> list
:param unknown_pic: single unknown img's path
:return: face result >>name
"""
name = "Unknown"
unknown_image = face_recognition.load_image_file(unknown_pic)
face_locations = face_recognition.face_locations(unknown_image) # box size?
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.39)
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
else:
name = "Unknown"
return name def faceRecog(imglist, known_face_names, unknownpath):
"""
:param imglist: [list known img address]
:param known_face_names: all known names
:param unknownpath: single unknownpath
:return: The name of the identification And index in names
"""
## pool.map(func, iter)
known_face_encodings = pool.map(mutl_reg, imglist) # 返回已知图片encodings, list
name = "Unknown"
first_match_index = "notMatch"
unknown_image = face_recognition.load_image_file(unknownpath)
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.39)
# print(matches, "--------------------------")
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
return name, first_match_index
else:
return name, first_match_index def mutl_reg(imglist):
"""
:param imglist: known img paths, >> list
:return: known img's encodings, >> list
"""
obama_image = face_recognition.load_image_file(imglist)
known_face_encodings = face_recognition.face_encodings(obama_image)[0]
return known_face_encodings def get_all_path(path='./known/'):
"""
获取指定路径下的文件的url, note: path下最好没有其他的目录
:param path: 指定路径
:return: 改路径下的地址
"""
all_files = os.walk(path)
files_dir = []
for i, v, files in all_files:
for file in files:
files_dir.append(os.path.join(path, file))
return files_dir def faceRecogUnknownList(known_img_list, names, unknownpaths):
""" """
result = []
for unknownpath in unknownpaths:
name, index = faceRecog(known_img_list, names, unknownpath)
# if index != 'notMatch':
result.append({name: unknownpath, 'index': index})
result.append(name)
print('It is [%s] to identify the [%s] through a face' % (name, unknownpath)) return result if __name__ == '__main__': # known_face_pic = face_recognition.load_image_file('./known/Comi.jpg')
# known_face_encodings = face_recognition.face_encodings(known_face_pic)
# name = 'Comi'
# unknown_pic_path = './unknown/unknown1.jpg'
# name = helloface(known_face_encodings, [name,], unknown_pic_path)
# print(name) known_img_list = get_all_path('./known/')
names = ['Obama', 'Comi', 'Bidden']
unknownpaths = get_all_path('./unknown/') result = faceRecogUnknownList(known_img_list, names, unknownpaths)
print(result)