我正在尝试构建一个使用面部识别库实时检测面部的软件。 我使用网络摄像头进行了尝试,结果令人鼓舞,帧频也相当稳定,但是当我切换到.mp4视频时,在fps方面效果非常差。 我在OpenCV中使用Python 3.6,这是我正在使用的代码:

import face_recognition

import cv2

# Load a sample picture and learn how to recognize it.

totti_image = face_recognition.load_image_file("totti.jpg")

totti_face_encoding = face_recognition.face_encodings(totti_image)[0]

# Create arrays of known face encodings and their names

known_face_encodings = [

totti_face_encoding

]

known_face_names = [

"Francesco Totti"

]

def get_faces(frame):

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)

rgb_frame = frame[:, :, ::-1]

# Find all the faces and face enqcodings in the frame of video

face_locations = face_recognition.face_locations(rgb_frame)

face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

# Loop through each face in this frame of video

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):

# See if the face is a match for the known face(s)

matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.50)

name ="Unknown"

# If a match was found in known_face_encodings, just use the first one.

if True in matches:

first_match_index = matches.index(True)

name = known_face_names[first_match_index]

# Draw a box around the face

cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face

cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)

font = cv2.FONT_HERSHEY_DUPLEX

cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

return frame

在每帧的while循环内都会调用函数" get_faces",而我的性能约为0.5 fps。

如果有人有建议以便获得更好的fps输出,请告诉我,谢谢。

编辑:

我使用下面的示例(使其适应我的需求),并且一切工作得更好:

链接

最终代码:

import face_recognition

import cv2

# Load a sample picture and learn how to recognize it.

image = face_recognition.load_image_file("totti.jpg")

encoding = face_recognition.face_encodings(image)[0]

# Create arrays of known face encodings and their names

known_face_encodings = [

encoding

]

known_face_names = [

"Totti",

]

# Initialize some variables

face_locations = []

face_encodings = []

face_names = []

def get_faces(frame):

# Resize frame of video to 1/10 size for faster face recognition processing

small_frame = cv2.resize(frame, (0, 0), fx=0.1, fy=0.1)

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)

rgb_small_frame = small_frame[:, :, ::-1]

# Find all the faces and face encodings in the current frame of video

face_locations = face_recognition.face_locations(rgb_small_frame)

face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

face_names = []

for face_encoding in face_encodings:

# See if the face is a match for the known face(s)

matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

name ="Person"

# If a match was found in known_face_encodings, just use the first one.

if True in matches:

first_match_index = matches.index(True)

name = known_face_names[first_match_index]

face_names.append(name)

# Display the results

for (top, right, bottom, left), name in zip(face_locations, face_names):

# Scale back up face locations since the frame we detected in was scaled to 1/10 size

top *= 10

right *= 10

bottom *= 10

left *= 10

# Draw a box around the face

cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face

cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)

font = cv2.FONT_HERSHEY_DUPLEX

cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

return frame

要确定脚本的哪些部分运行时间最长,请使用探查器。 这将输出执行每个调用的时间,因此您可以更好地了解函数的哪些部分不是最佳的。 有关如何配置代码的示例,请参见Python Profilers。

从文档中:

SPEEDING UP FACE RECOGNITION

Face recognition can be done in parallel if you have a computer with

multiple CPU cores. For example if your system has 4 CPU cores, you

can process about 4 times as many images in the same amount of time by

using all your CPU cores in parallel. If you are using Python 3.4 or

newer, pass in a --cpus parameter:

face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/

You can also pass in --cpus -1 to use all CPU cores in your system.

使用一个,然后使用最大核数,在计算机上测试操作。 如果这可以显着缩短执行时间,那么最好的做法是在自己的脚本中实施多处理。

谢谢您的回答,但我已经知道问题出在以下几行:face_locations = face_recognition.face_locations(rgb_frame)face_encodings = face_recognition.face_encodings(rgb_frame,face_locations)但是我无法更改它们,因为这些是我需要的库函数。 这是我在网上找到的最好的处理方法,但是确实很慢,所以我的问题不是要找到运行时间最长的部分,而是是否有使用此代码的方法或我没有找到的其他方法 为了得到更快的分析。

@ J.Blackadar,我可以在我的python代码中添加face_recognition --cpus 4吗? 我正在使用pi 2进行人脸识别,但是速度很慢。 :(

@ M.D.P仅通过示例使用face_recognition本身就可以增加CPU。 在您的实际代码中,您需要实现多处理,以将计算负荷分配到多个内核。

python做人脸识别速度_Python人脸识别速度慢相关推荐

  1. 用 Python 做个电脑版人脸屏幕解锁神器

    作者:美图博客 https://www.meitubk.com/zatan/386.htm 前言 最近突然有个奇妙的想法,就是当我对着电脑屏幕的时候,电脑会先识别屏幕上的人脸是否是本人,如果识别是本人 ...

  2. 用python做自我介绍_python入门教程NO.2 用python做个自我介绍

    本文涉及的python基础语法为:数据类型等 数据类型 1. 字符串的拼接 我们在上一章中已经简单介绍了一下字符串的创建方式,这里我们简单学习一下字符串的运算和拼接. 字符串的运算 `字符串的加法` ...

  3. python做时序图_python如何做时间序列

    python做时间序列的方法:首先导入需要的工具包,输入"data.plot()","plt().show()"命令绘制时序图:然后由acf,pacf判断模型参 ...

  4. python实现人脸识别_python人脸识别代码实现丨内附代码

    Python在人脸识别方面功能很强大,程序语言简单高效,下面小编来编程实现一下如何实现人脸识别.如有错点,还望斧正 识别图片中的人脸位置 #人脸识别分类器路径tool_url = r'C:\Users ...

  5. python 语音识别机器人控制系统_python人脸识别+语音识别 的监控系统

    使用流程 ​添加成员-->训练模型-->监控 ​注:添加成员后,数据仅仅保存在后台,并没有在模型里.所以此时监控,就没有这次新添加的成员.只有训练模型后才会存入在模型里 主页 添加成员 ​ ...

  6. python中文相似度_python 人脸对比--百度API人脸相似度识别(超简单)-Go语言中文社区...

    说明:这篇是写使用百度人脸识别API进行人脸相似度识别对比,如 给两个人物照片,判断是否是同一个人.简单的4步完成. 1,获取百度人脸识别API的API Key和Secret Key.(10分钟内完成 ...

  7. python做图片美化_python图片美化

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 功能性是开发的第一要务每一个 python 图形界面库都有它自有的功能特性和界面 ...

  8. python做前端可视化_Python数据可视化的四种简易方法

    摘要: 本文讲述了热图.二维密度图.蜘蛛图.树形图这四种Python数据可视化方法. 数据可视化是任何数据科学或机器学习项目的一个重要组成部分.人们常常会从探索数据分析(EDA)开始,来深入了解数据, ...

  9. python做大型网站_Python中的大型Web应用:一个好的架构

    如果你着手使用关系型数据库在Python中编写大型应用程序,这篇长文正好满足你的需求.这里我分享下在一个大型团队中使用SQLAlchemy(Python语言中提供最先进ORM工具的软件)编写超过6个月 ...

最新文章

  1. 直接广播地址_计算机网络之网桥、冲突域、广播域是什么?
  2. Python基本语法(基于3.x)
  3. c语言填空三个数找中间大小,计算机文化基础复习题及答案(精华)
  4. python chm制作_生成chm文档的方法
  5. 宽屏企业网站源码中英php_宽屏版大气企业网站源码dede网站源码中英文网站模板SEO...
  6. vue介绍及环境安装
  7. 小米路由器青春版装linux,比较费心的折腾 篇二:小米路由器青春版折腾负载均衡...
  8. Python学习入门基础:注释、变量基本使用、变量的命名
  9. 这个功能是怎么实现的
  10. BZOJ2653middle——二分答案+可持久化线段树
  11. 【深度学习】深度学习的四大组件
  12. VMware vSphere 5.x 与 vSphere 6.0各版本功能特性对比
  13. PCA,ZCA,ICA,白化,稀疏编码和自编码器
  14. SCI等英文文献免费下载方法总结
  15. 扫描文件怎么设置到服务器,如何为扫描仪添加局域网功能
  16. ShadowGun Deadzone 放出 GM Kit Mod 包
  17. Spring Boot 接入支付宝,实战来了
  18. 云服务器多开账号,怎么用云服务器多开模拟器
  19. 更改浏览器默认的网址
  20. 【Captain America Sentinel of Liberty HD】美国队长:自由哨兵 v1.0.2

热门文章

  1. Connection Backoff Interop Test Descriptions
  2. NetApp ADP (Advanced drive partitioning) 介绍
  3. 2022-2028全球多层共烧基板行业调研及趋势分析报告
  4. 大数据可视化分析以及预测性分析方法
  5. 【YOLOv3从头训练 数据篇】
  6. 硕士学论文中的国内图书分类号和国际图书分类号
  7. Python常用数字处理基本操作汇总
  8. 快速复制文件地址——无任何安装
  9. Oracle快速复制一张表
  10. 计算机中操作系统的主要功能是什么,计算机中的操作系统的主要功能是什么