.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. 使用Nginx实现灰度发

     灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到 ...

  2. Robot Framework 自定义库

    进入  python安装路径\Lib\site-packages 创建文件夹,库名 创建py文件,myclass.py 创建py文件,__init__.py 导入自定义库 遇到的问题: python版 ...

  3. 批处理(bat)命令学习的一些总结

    这篇笔记是我对批处理学习的一些总结,能在系统帮助里找到的内容我就不写了,太偏门的也不写,只写些个人感觉很好用的技巧,大部分属于整理 一.set 篇: 1.set(无开关) set .=test set ...

  4. greasemonkey修改网页内指定函数

    greasemonkey replace function? 方法1:编写GM代码 alert("hello2"); var mydiv =document.getElementB ...

  5. webpack 的插件 DllPlugin 和 DllReferencePlugin

    在项目中,引入了比较多的第三方库,导致项目大,而每次修改,都不会去修改到这些库,构建却都要再打包这些库,浪费了不少时间.所以,把这些不常变动的第三方库都提取出来,下次 build 的时候不再构建这些库 ...

  6. Mfs+drbd+keepalived实现mfs系统高可用

    http://blog.sina.com.cn/s/blog_53c654720102wo1k.html Moosefs分布式文件系统是一个易用的系统,但其只有在Pro版中提供了master的高可用方 ...

  7. SQLServer2008开启远程连接

    1.查看sqlserver brower协议是否启动 2.对象资源管理器 右键属性->选择-> 方面->服务器配置->Remoteaccess ->True 3.对象资源 ...

  8. java编码-多重(乱码)

    一.1,UTF编码 - 2,ISO解码 - 3,UTF编码 - 4,ISO解码 String ISO = "ISO-8859-1"; String UTF = "UTF- ...

  9. SEO之H1,H2,H3,H4....STRONG使用方法

    作为一个SEO从业人员,我们不仅仅是要懂得如何通过网站内容和外链等SEO手段,其实一个优秀的SEOER在从事一个SEO案例时,首先着手的是如何从网站程序本身来打好网站SEO基础. 在平时和很多朋友的交 ...

  10. java.net.SocketTimeoutException: Read timed out 错误解决

    这两天项目在测试环境下通过URLConnection 做数据传递时,出现了如下错误 java.net.SocketTimeoutException: Read timed out 经过查找研究,原因是 ...