.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. VS2015 Release 生成调试信息

    - C/C++ > 常规 > 调试信息格式 - C/C++ > 优化 > 优化 > 禁止(/Od) - 链接器 > 调试 > 生成调试信息 > 是 (/ ...

  2. L3-017 森森快递 (30 分)

    森森开了一家快递公司,叫森森快递.因为公司刚刚开张,所以业务路线很简单,可以认为是一条直线上的N个城市,这些城市从左到右依次从0到(编号.由于道路限制,第i号城市(,)与第(号城市中间往返的运输货物重 ...

  3. HDU5373 The shortest problem (YY)

    http://acm.hdu.edu.cn/showproblem.php?pid=5373 YY题,模拟下计算过程就好了,计算中并不要保存实际数(这个数会非常大),只要保存到目前为止的数字位上的和 ...

  4. test20181004 苹果树

    题意 分析 对每个点维护子树所能达到的dfn最大值.最小值.次大值.次小值,然后就可以计算原树中每个点与父亲的连边对答案的贡献. 如果子树中没有边能脱离子树,断掉该边与任意一条新加的边都成立,答案就加 ...

  5. Android中logcat和日志打印

     一.logcat对日志过滤 1.# logcat --help # logcat --help Usage: logcat [options] [filterspecs] options inclu ...

  6. some ideas

    1. 3d camera h/w: 单反+projector s/w: 抓图 -> 3d成像 -> 3d显示 -->  3d编辑(?具体要那些功能)--> to 3d prin ...

  7. PHP 短连接生成算法

    短连接生成类: <?php #短连接生成算法 class Short_Url { #字符表 public static $charset = "0123456789ABCDEFGHIJ ...

  8. linux $* $@ 特定位置参数

    举例说:脚本名称叫test.sh 入参三个: 1 2 3运行test.sh 1 2 3后$*为"1 2 3"(一起被引号包住)$@为"1" "2&qu ...

  9. php 非常简单的导入sql文件

    在网上找了很多,都是写了一个类 做了各种处理.还真是累哦 当然之前也做了各种尝试 source 客户端命令 mysql_query()是不支持的 load_file  在where 之后执行可以,但是 ...

  10. ASP.NET Web Pages:简介

    ylbtech-.Net-ASP.NET Web Pages:简介 ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种 ...