接着系统一,继续开始我们face_recognition.

Python 模块:face_recognition

在Python中,你可以导入face_recognition模块,调用丰富的API接口,用几行代码就可以轻松玩转各种人脸识别功能!

API 接口文档: https://face-recognition.readthedocs.io(可以参考这个)

在图片中定位人脸的位置关键代码
  1. import face_recognition
  2.  
  3. image = face_recognition.load_image_file("my_picture.jpg")
  4. face_locations = face_recognition.face_locations(image)
  5.  
  6. # face_locations is now an array listing the co-ordinates of each face!

    看定位鞠婧祎的脸:knowe_people文件夹中创建find_faces_in_picture.py文件并写入以下代码:

    from PIL import Image
    import face_recognition

    # Load the jpg file into a numpy array
    image = face_recognition.load_image_file("鞠婧祎.jpeg")

    # Find all the faces in the image using the default HOG-based model.
    # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
    # See also: find_faces_in_picture_cnn.py
    face_locations = face_recognition.face_locations(image)

    print("I found {} face(s) in this photograph.".format(len(face_locations)))

    for face_location in face_locations:

        # Print the location of each face in this image
        top, right, bottom, left = face_location
        print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

        # You can access the actual face itself like this:
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.show()

    然后终端切换到knowe_people目录下,输入以下命令,弹出窗口如下:

可以看到终端已经找到鞠婧祎人脸坐标分别是:Top: 297, Left: 553, Bottom: 759, Right: 1016,并输出人脸。继续测试

  1. 将第二段第二行改为image = face_recognition.load_image_file("特朗普.jpg"),终端输出如下:

你也可以使用深度学习模型达到更加精准的人脸定位。

注意:这种方法需要GPU加速(通过英伟达显卡的CUDA库驱动),你在编译安装dlib的时候也需要开启CUDA支持。

  1. import face_recognition
  2.  
  3. image = face_recognition.load_image_file("my_picture.jpg")
  4. face_locations = face_recognition.face_locations(image, model="cnn")
  5.  
  6. # face_locations is now an array listing the co-ordinates of each face!

    看案例:利用卷积神经网络深度学习模型定位鞠婧祎的人脸
  1. knowe_people文件夹中创建find_faces_in_picture_cnn.py文件并写入以下代码:
    from PIL import Image
    import face_recognition

    # Load the jpg file into a numpy array
    image = face_recognition.load_image_file("鞠婧祎.jpg")

    # Find all the faces in the image using a pre-trained convolutional neural network.
    # This method is more accurate than the default HOG model, but it's slower
    # unless you have an nvidia GPU and dlib compiled with CUDA extensions. But if you do,
    # this will use GPU acceleration and perform well.
    # See also: find_faces_in_picture.py
    face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

    print("I found {} face(s) in this photograph.".format(len(face_locations)))

    for face_location in face_locations:

        # Print the location of each face in this image
        top, right, bottom, left = face_location
        print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

        # You can access the actual face itself like this:
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.show()
  1. 然后终端切换到knowe_people目录下,输入以下命令,弹出窗口如下:

如果你有很多图片需要识别,同时又有GPU,那么你可以参考这个例子:
案例:利用卷积神经网络深度学习模型批量识别人脸照片
  1. knowe_people文件夹中创建find_faces_in_batches.py文件并写入以下代码:

    import face_recognition
    import cv2

    # This code finds all faces in a list of images using the CNN model.
    #
    # This demo is for the _special case_ when you need to find faces in LOTS of images very quickly and all the images
    # are the exact same size. This is common in video processing applications where you have lots of video frames
    # to process.
    #
    # If you are processing a lot of images and using a GPU with CUDA, batch processing can be ~3x faster then processing
    # single images at a time. But if you aren't using a GPU, then batch processing isn't going to be very helpful.
    #
    # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read the video file.
    # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
    # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

    # Open video file
    video_capture = cv2.VideoCapture("short_hamilton_clip.mp4")

    frames = []
    frame_count = 0

    while video_capture.isOpened():
        # Grab a single frame of video
        ret, frame = video_capture.read()

        # Bail out when the video file ends
        if not ret:
            break

        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        frame = frame[:, :, ::-1]

        # Save each frame of the video to a list
        frame_count += 1
        frames.append(frame)

        # Every 128 frames (the default batch size), batch process the list of frames to find faces
        if len(frames) == 128:
            batch_of_face_locations = face_recognition.batch_face_locations(frames, number_of_times_to_upsample=0)

            # Now let's list all the faces we found in all 128 frames
            for frame_number_in_batch, face_locations in enumerate(batch_of_face_locations):
                number_of_faces_in_frame = len(face_locations)

                frame_number = frame_count - 128 + frame_number_in_batch
                print("I found {} face(s) in frame #{}.".format(number_of_faces_in_frame, frame_number))

                for face_location in face_locations:
                    # Print the location of each face in this frame
                    top, right, bottom, left = face_location
                    print(" - A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

            # Clear the frames array to start the next batch
            frames = []

    注意到:这个例子需要安装openCV.这个例子需要GPU cuda加速,否则运行卡顿。我就试了几次,电脑死机,先跳过,以后再说。

    案例:使用卷积神经网络深度学习模型把来自网络摄像头视频的人脸高斯模糊。
  1. knowe_people文件夹中创建blur_faces_on_webcam.py文件并写入以下代码:

    import face_recognition
    import cv2

    # This is a demo of blurring faces in video.

    # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
    # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
    # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

    # Get a reference to webcam #0 (the default one)
    video_capture = cv2.VideoCapture(0)

    # Initialize some variables
    face_locations = []

    while True:
        # Grab a single frame of video
        ret, frame = video_capture.read()

        # Resize frame of video to 1/4 size for faster face detection processing
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(small_frame, model="cnn")

        # Display the results
        for top, right, bottom, left in face_locations:
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4

            # Extract the region of the image that contains the face
            face_image = frame[top:bottom, left:right]

            # Blur the face image
            face_image = cv2.GaussianBlur(face_image, (99, 99), 30)

            # Put the blurred face region back into the frame image
            frame[top:bottom, left:right] = face_image

        # Display the resulting image
        cv2.imshow('Video', frame)

        # Hit 'q' on the keyboard to quit!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release handle to the webcam
    video_capture.release()
    cv2.destroyAllWindows()
  1. 然后终端切换到knowe_people目录下,输入以下命令,弹出窗口如下:(把自己宿舍卖了,希望室友不要介意,嘻嘻)


    好了,今天就到这吧,今天主要实现4个功能(如下),更多功能见系列三。
  1. 案例一:定位鞠婧祎的脸
  1. 案例二:利用卷积神经网络深度学习模型定位鞠婧祎的人脸
  1. 案例三:利用卷积神经网络深度学习模型批量识别人脸照片
  1. 案例四:使用卷积神经网络深度学习模型把来自网络摄像头视频的人脸高斯模糊。




  1.  


  1.  
  1.  




基于Python与命令行人脸识别项目(系列二)的更多相关文章

  1. 基于Python与命令行人脸识别项目(系列一)

    Face Recognition 人脸识别 摘要:本项目face_recognition是一个强大.简单.易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,方便大家使用.对于本项目可以使用 ...

  2. 基于卷积神经网络的人脸识别项目_使用Tensorflow-gpu+dilib+sklearn

    https://www.cnblogs.com/31415926535x/p/11001669.html 基于卷积神经网络的人脸识别项目_使用Tensorflow-gpu+dilib+sklearn ...

  3. [Android] 基于 Linux 命令行构建 Android 应用(二):命令行管理项目

    创建 Android 项目 在命令行创建 Android 项目需要用到 android 工具(该工具由 Android SDK 提供,位于 <sdk>/tools/ 目录下.).它能自动生 ...

  4. Github开源人脸识别项目face_recognition

    Github开源人脸识别项目face_recognition 原文:https://www.jianshu.com/p/0b37452be63e 译者注: 本项目face_recognition是一个 ...

  5. 关于运行“基于极限学习机ELM的人脸识别程序”代码犯下的一些错误

    代码来源 基于极限学习机ELM的人脸识别程序 感谢文章主的分享 我的环境是 win10 anaconda Command line client (version 1.6.5)(conda 4.3.3 ...

  6. 10分钟手把手教你运用Python实现简单的人脸识别

    欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 前言:让我的电脑认识我 我的电脑只有认识我,才配称之为我的电脑! 今天,我们用Python实现高大上的人脸识别技术! Python里,简单的 ...

  7. opencv基于PCA降维算法的人脸识别

    opencv基于PCA降维算法的人脸识别(att_faces) 一.数据提取与处理 # 导入所需模块 import matplotlib.pyplot as plt import numpy as n ...

  8. 命令行创建Maven项目卡住以及出错解决办法。

    第一次通过命令行创建maven项目.结果,果不其然啊,还是出问题了,不过出问题比没有出问题强,知道哪里有问题并学会解决也是一种收获. 遇到的第一个问题,在从仓库下载东西的时候会卡住,我开始以为是网速问 ...

  9. 学习笔记-使用cmd命令行创建nodejs项目

    使用cmd命令行创建nodejs项目 1)在DOS下cd 进入到你想要创建项目的路径. 2)mkdir sing (创建一个sing文件夹) 3)cd sing  4) npm init (初始化工程 ...

随机推荐

  1. 【读书】Into The Air:进入空气稀薄地带

    珠穆朗玛峰,世界第一高峰,北部在中国境内,南部在尼泊尔境内.喜欢户外运动的人,曾经在20多岁的时候曾经"大言不惭"说这一辈子一定要去一次珠峰.<Into the Air> ...

  2. Redis学习(四)redis发布订阅

    文章更新时间:2020/04/22 一.简介 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. ...

  3. 解决npm被墙的问题

    npm存储包文件的服务器在国外,有时候会被墙,速度很慢,所以我们需要解决这个问题. http://npm.taobao.org/  淘宝的开发团队把npm在国内做了一个备份. 安装淘宝的cnpm np ...

  4. 用Docker swarm快速部署Nebula Graph集群

    用Docker swarm快速部署Nebula Graph集群 一.前言 本文介绍如何使用 Docker Swarm 来部署 Nebula Graph 集群. 二.nebula集群搭建 2.1 环境准 ...

  5. 智慧矿山-选矿工艺数字 3D 可视化

    前言 现代科技和工业的发展对矿物原料的要求越来越高,直接开采的原矿石往往达不到标准,而原矿通过选矿加工后则可以满足要求.选矿技术在冶金.煤炭.化工.建材和环保等部门都得到应用,对国民经济的发展意义重大 ...

  6. Java知识系统回顾整理01基础05控制流程01if

    一.if if(表达式1){ 表达式2: } 如果表达式1的值是true, 就执行表达式2 public class HelloWorld { public static void main(Stri ...

  7. python数据结构之二叉树的建立实例

    先建立二叉树节点,有一个data数据域,left,right 两个指针域 # coding:utf-8 class TreeNode(object): def __init__(self,left=N ...

  8. C++ | 继承(基类,父类,超类),(派生类,子类)

    转载:https://blog.csdn.net/Sherlock_Homles/article/details/82927515 文章参考:https://blog.csdn.net/war1111 ...

  9. ISP-OB, pedestal 以及ISP概述

    网上的直接参考资料 1. https://zhuanlan.zhihu.com/p/36896537 2. https://blog.csdn.net/m0_38049850/article/deta ...

  10. 【题解】[JSOI2007]字符加密

    Link \(\text{Solution:}\) 后缀数组第一题祭-- 观察一下,这个是让求一个环形的原字符串的后缀,我们可以考虑一下断环为链. 对于\(aba\)我们扩展成\(abaaba,\)则 ...