.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)

face,Pool的更多相关文章

  1. Process类,Thread类,Pool类,gevent类,ProcessPoolExecutor,ThreadPoolExecutor的用法比较

    一 Process类 multiprocessing模块下的一个类 创建子进程. 有两种方法 方法一 from multiprocessing import Process import os def ...

  2. 0704 Process继承实现多进程、Pool进程池,进程间通过队列通信,Pool实现多进程实现复制文件

    通过继承的方式,实现Process多进程 from multiprocessing import Process import time class MyNewProcess(Process): de ...

  3. socket阻塞与非阻塞,同步与异步,select,pool,epool

    概念理解 一.与I/O相关的五个重要概念 1. 第一个概念:用户空间与内核空间 1. 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方) 2. ...

  4. 创建,查看,删除pool,查看,修改pool参数命令总结

    标签(空格分隔): ceph,ceph运维,pool 1. 创建pool命令: ceph的pool有两种类型,一种是副本池,一种是ec池,创建时也有所区别 1.1 创建副本池: $ sudo ceph ...

  5. 黑马程序员-autorelease pool

    Autorelease:可以延迟给对象发送release消息.发送一个autorelease消息给对象,证明该对象在一定时间内有效,一定时间后会对该对象进行释放,进行一次release. 一个auto ...

  6. 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  7. retain,copy,assign及autorelease ,strong,weak

    一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...

  8. redis pool config的配置参数

    .获取jedis实例时,实际上可能有两类错误.一类是pool.getReource(),得不到可用的jedis实例:另一类是jedis.set/get时出错也会抛出异常:为了实现区分,所以根据inst ...

  9. (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

随机推荐

  1. 51Nod 1079:中国剩余定理

    1079 中国剩余定理  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % ...

  2. java安装教程及环境配置

    下载JDK(网上有很多,可以百度) 首先在非系统盘(系统盘通常是C盘)中,新建一个Java(可以是其他名称,必须是英文且名称中没有空格)文件夹,并在这个文件夹中创建JDK,JRE两个文件夹. 双击已下 ...

  3. Mybatis中parameterType、resultMap、statementType等等配置详解(标签中基本配置详解)

    一.(转自:https://blog.csdn.net/majinggogogo/article/details/72123185) 映射文件是以<mapper>作为根节点,在根节点中支持 ...

  4. UVa 10410 树重建

    Tree Reconstruction Problem Description You have just finished a compiler design homework question w ...

  5. 转-spring boot web相关配置

    spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...

  6. grandstack 基于graphql&&react&& apollo&& neo4j 的全栈开发工具

    grandstack是一个基于graphql&&react&& apollo&& neo4j 的全栈开发工具. 有篇关于graphql 的5个常见问题的 ...

  7. stty(set tty)

    tty [ -a ] [ -g ] [ Options ]   stty(set tty)命令用于显示和修改当前注册的终端的属性. UNIX系统为键盘的输入和终端的输出提供了重要的控制手段,可以通过s ...

  8. 产品思维&技术思维&工程思维

    产品思维 产品思维的起源是用户(或客户)价值.用户价值是通过技术手段以产品或服务的形态去解决用户的痛点,或带去爽点.毫无疑问,工程师在日常工作中应时刻关注并理清自己的工作与用户(或客户)价值的联系,并 ...

  9. python 之 python3内置函数

    一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内 ...

  10. transient解释

    http://www.cnblogs.com/lanxuezaipiao/p/3369962.html