百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

face_recognition

> 编程语言
开源

适用于 Python 和命令行的全球最简单的面部识别 API

56.6K stars0 点赞3 次浏览
访问官网GitHub

工具介绍

适用于 Python 和命令行的全球最简单的面部识别 API

Face Recognition

You can also read a translated version of this file in Chinese 简体中文版 or in Korean 한국어 or in Japanese 日本語.

Recognize and manipulate faces from Python or from the command line with the world's simplest face recognition library.

Built using dlib's state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark (see the LFW paper (PDF)).

This also provides a simple face_recognition command line tool that lets you do face recognition on a folder of images from the command line!

Features

Find faces in pictures

Find all the faces that appear in a picture:

python
import face_recognition
image = face_recognition.load_image_file("your_file.jpg")
face_locations = face_recognition.face_locations(image)

Find and manipulate facial features in pictures

Get the locations and outlines of each person's eyes, nose, mouth and chin.

python
import face_recognition
image = face_recognition.load_image_file("your_file.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)

Finding facial features is super useful for lots of important stuff. But you can also use it for really stupid stuff like applying digital make-up (think 'Meitu'):

Identify faces in pictures

Recognize who appears in each photo.

python
import face_recognition
known_image = face_recognition.load_image_file("biden.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")

biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

You can even use this library with other Python libraries to do real-time face recognition:

See this example for the code.

Online Demos

User-contributed shared Jupyter notebook demo (not officially supported):

Installation

Requirements

  • Python 3.3+ or Python 2.7
  • macOS or Linux (Windows not officially supported, but might work)

Installation Options:

Installing on Mac or Linux

First, make sure you have dlib already installed with Python bindings:

  • How to install dlib from source on macOS or Ubuntu

Then, make sure you have cmake installed:

brew install cmake

Finally, install this module from pypi using pip3 (or pip2 for Python 2):

bash
pip3 install face_recognition

Alternatively, you can try this library with Docker, see this section.

If you are having trouble with installation, you can also try out a pre-configured VM.

Installing on an Nvidia Jetson Nano board

  • Jetson Nano installation instructions
    • Please follow the instructions in the article carefully. There is current a bug in the CUDA libraries on the Jetson Nano that will cause this library to fail silently if you don't follow the instructions in the article to comment out a line in dlib and recompile it.

Installing on Raspberry Pi 2+

  • Raspberry Pi 2+ installation instructions

Installing on FreeBSD

bash
pkg install graphics/py-face_recognition

Installing on Windows

While Windows isn't officially supported, helpful users have posted instructions on how to install this library:

  • @masoudr's Windows 10 installation guide (dlib + face_recognition)

Installing a pre-configured Virtual Machine image

  • Download the pre-configured VM image (for VMware Player or VirtualBox).

Usage

Command-Line Interface

When you install face_recognition, you get two simple command-line programs:

  • face_recognition - Recognize faces in a photograph or folder full for photographs.
  • face_detection - Find faces in a photograph or folder full for photographs.

face_recognition command line tool

The face_recognition command lets you recognize faces in a photograph or folder full for photographs.

First, you need to provide a folder with one picture of each person you already know. There should be one image file for each person with the files named according to who is in the picture:

Next, you need a second folder with the files you want to identify:

Then in you simply run the command face_recognition, passing in the folder of known people and the folder (or single image) with unknown people and it tells you who is in each image:

bash
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/

/unknown_pictures/unknown.jpg,Barack Obama
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person

There's one line in the output for each face. The data is comma-separated with the filename and the name of the person found.

An unknown_person is a face in the image that didn't match anyone in your folder of known people.

face_detection command line tool

The face_detection command lets you find the location (pixel coordinatates) of any faces in an image.

Just run the command face_detection, passing in a folder of images to check (or a single image):

bash
$ face_detection  ./folder_with_pictures/

examples/image1.jpg,65,215,169,112
examples/image2.jpg,62,394,211,244
examples/image2.jpg,95,941,244,792

It prints one line for each face that was detected. The coordinates reported are the top, right, bottom and left coordinates of the face (in pixels).

Adjusting Tolerance / Sensitivity

If you are getting multiple matches for the same person, it might be that the people in your photos look very similar and a lower tolerance value is needed to make face comparisons more strict.

You can do that with the --tolerance parameter. The default tolerance value is 0.6 and lower numbers make face comparisons more strict:

bash
$ face_recognition --tolerance 0.54 ./pictures_of_people_i_know/ ./unknown_pictures/

/unknown_pictures/unknown.jpg,Barack Obama
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person

If you want to see the face distance calculated for each match in order to adjust the tolerance setting, you can use --show-distance true:

bash
$ face_recognition --show-distance true ./pictures_of_people_i_know/ ./unknown_pictures/

/unknown_pictures/unknown.jpg,Barack Obama,0.378542298956785
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person,None
More Examples

If you simply want to know the names of the people in each photograph but don't care about file names, you could do this:

bash
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2

Barack Obama
unknown_person
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 <number_of_cpu_cores_to_use> parameter:

bash
$ 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.

Python Module

You can import the face_recognition module and then easily manipulate faces with just a couple of lines of code. It's super easy!

API Docs: https://face-recognition.readthedocs.io.

Automatically find all the faces in an image
python
import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image)

# face_locations is now an array listing the co-ordinates of each face!

See this example to try it out.

You can also opt-in to a somewhat more accurate deep-learning-based face detection model.

Note: GPU acceleration (via NVidia's CUDA library) is required for good performance with this model. You'll also want to enable CUDA support when compliling dlib.

python
import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image, model="cnn")

# face_locations is now an array listing the co-ordinates of each face!

See this example to try it out.

If you have a lot of images and a GPU, you can also find faces in batches.

Automatically locate the facial features of a person in an image
python
import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)

# face_landmarks_list is now an array with the locations of each facial feature in each face.
# face_landmarks_list[0]['left_eye'] would be the location and outline of the first person's left eye.

See this example to try it out.

Recognize faces in images and identify who they are
…

See this example to try it out.

Python Code Examples

All the examples are available here.

Face Detection

  • Find faces in a photograph
  • Find faces in a photograph (using deep learning)
  • Find faces in batches of images w/ GPU (using deep learning)
  • Blur all the faces in a live video using your webcam (Requires OpenCV to be installed)

Facial Features

  • Identify specific facial features in a photograph
  • Apply (horribly ugly) digital make-up

Facial Recognition

  • Find and recognize unknown faces in a photograph based on photographs of known people
  • [Identify and draw boxes around each person in a photo](https://github.com/ageitgey/face_recognition/blob/master/examples/identify_and_draw_boxes

Issues· 832 开放

查看全部 Issues在 GitHub 打开
  • #1634

    显示错误: 在使用 `face_recognition` 之前,请使用以下命令安装 `face_recognition_models`。

    更新于 2026年9月24日
  • #1547

    Windows 安装

    更新于 2026年8月14日
  • #1678

    CLI 图像文件夹正则表达式缺少结束锚点,导致检测到非图像文件 (foo.jpg.bak, notes.jpegdoc)

    更新于 2026年8月1日
  • #1669

    诚实反馈

    更新于 2026年7月31日
  • #1665

    由于工作进程中的直接打印调用,多进程输出可能会交错/混乱

    更新于 2026年7月2日
  • #1638

    安装错误

    更新于 2026年6月23日
  • #1449

    在高分辨率图像上运行 HOG 检测时进程总是崩溃

    更新于 2026年5月20日
  • #1639

    为什么我会收到此错误?

    更新于 2026年5月11日
  • #608

    请安装 `face_recognition_models`

    更新于 2026年5月9日
  • #1668

    AiSoul

    更新于 2026年1月28日

> 标签

Pythonface-detectionface-recognitionmachine-learningpython

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月29日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言