人工智能之基于face_recognition的人臉檢測與識別

不久乘高鐵出行,看見高鐵火車站已經實現了“刷臉進站”,而且效率很高,很感興趣,今天抽時間研究一下,其實沒那么復雜。

我基本上是基于https://github.com/ageitgey/face_recognition上的資料和源碼做一些嘗試和試驗。

首先,需要配置我們的python環境,我懸著的python27(比較穩定),具體過程不多說了。

然后,需要安裝這次的主角face_recognition庫,這個的安裝花了我不少時間,需要注意一下幾點(按照本人的環境):

  1,首先,安裝visual studio 2015,因為vs2015默認只安裝c#相關組件,所以需要安裝c++相關組件。

    ps:vs2015安裝c++相關組件的方法:在vs2015中新建c++項目,出現下面場景

    

    選擇第二項,確定后就會自動安裝。

    為什么需要安裝c++,因為安裝face_recognition時會先安裝dlib,dlib是基于c++的一個庫。

  2,安裝cmake(一個跨平臺編譯工具),然后需要將cmake的安裝路徑加入到系統環境變量path中去。

最后,就可以直接在dos中執行安裝命令了(需要切換到python目錄下的Script目錄下):pip install? face_recognition,命令會自動幫你安裝好需要的dlib庫。 

到此為止,我們完成了face_recognition安裝工作。

?

---------------------------------------------------------------分割線----------------------------------------------------------------------------------

下面給出幾個實例來逐步了解“人臉識別”:

1.一行代碼實現“人臉識別”

?

在Python目錄中新建兩個文件夾:分別表示“已知姓名的人”和“未知姓名的人”,圖片以額、人名命名,如下:

?

?接下來,我們通過“認識的人”來識別“不認識的人”:

結果表明:1.jpg不認識,3.jpg是obama,unkown.jpg中有兩個人,一個是obama,另一個不認識

結果還挺準確的!很給力!!

?

2.識別圖片中所有的人臉,并顯示出來

import Image
import face_recognition
image = face_recognition.load_image_file('F:/Python27/Scripts/all.jpg')
face_locations = face_recognition.face_locations(image)#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:top,right,bottom,left = face_locationprint('A face is located at pixel location Top:{},Left:{},Bottom:{},Right:{}'.format(top,right,bottom,left))face_image = image[top:bottom,left:right]pil_image=Image.fromarray(face_image)pil_image.show()
View Code

避坑指南:import Image需要先安裝PIL庫,在pycharm中安裝的時候會報錯(因為pil沒有64位的版本),這時我們安裝Pillow-PIL就好了。

我們的all.jpg如下:

?

?執行以下,看看結果:

沒有錯,總共12個人臉都被識別出來了!!!

?

3.給照片“美顏”

face_recognition可以識別人像的下巴,眼睛,鼻子,嘴唇,眼球等區域,包含以下這些個特征:

  facial_features = [?'chin',?'left_eyebrow',?'right_eyebrow',?'nose_bridge',?'nose_tip',?'left_eye',?'right_eye',?'top_lip',?'bottom_lip' ]

? ? ? ?利用這些特征屬性,可以輕松的給人像“美顏”

from PIL import Image, ImageDraw
face_recognition
import face_recognitionimage = face_recognition.load_image_file("F:/Python27/Scripts/known_people/obama.jpg")#查找圖像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)for face_landmarks in face_landmarks_list:pil_image = Image.fromarray(image)d = ImageDraw.Draw(pil_image, 'RGBA')#讓眉毛變成了一場噩夢d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)#光澤的嘴唇d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)#閃耀眼睛d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))#涂一些眼線d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)pil_image.show()
View Code

執行下看看結果:

有點辣眼睛!!!!

?

4.利用筆記本攝像頭識別人像

回到前面說的高鐵站的“刷臉”,其實就是基于攝像頭的“人像識別”。

這里要調用電腦的攝像頭,而且涉及一些計算機視覺系統的計算,所以我們要先安裝opencv庫,

安裝方法:

pip install --upgrade setuptools
pip install numpy Matplotlib
pip install opencv-python

 ps:如果報錯:EnvironmentError:?[Errno 13]?Permission denied: 在install后加上--user即可

? ? ? ? ?小技巧:可以在python命令行中用?import site; site.getsitepackages()來確定當前的python環境的site-packages目錄的位置

目的:這里我們需要用攝像頭識別自己,那么首先需要有一張自己的照片,我將我的照片命名為mike.jpg,然后使用攝像頭來識別我自己。

?看看代碼:

import face_recognition
import cv2# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of 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)# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("F:/Python27/Scripts/known_people/obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("F:/Python27/Scripts/known_people/mike.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [obama_face_encoding,biden_face_encoding
]
known_face_names = ["Barack Obama","mike"
]# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:# Grab a single frame of videoret, frame = video_capture.read()# Resize frame of video to 1/4 size for faster face recognition processingsmall_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)rgb_small_frame = small_frame[:, :, ::-1]# Only process every other frame of video to save timeif process_this_frame:# Find all the faces and face encodings in the current frame of videoface_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 = "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]face_names.append(name)process_this_frame = not process_this_frame# Display the resultsfor (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/4 sizetop *= 4right *= 4bottom *= 4left *= 4# Draw a box around the facecv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# Draw a label with a name below the facecv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Display the resulting imagecv2.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()
View Code

只想看看結果:

?

看來,我被識別成功了。看起來有點小激動呢。

?

?

?

通過上面四個小例子基本了解face_recognition的用法,這只是小試牛刀,具體在現實中的應用要復雜很多,

我們需要大量的人臉數據,會涉及到機器學習和數學算法等等,而且根據應用場景的不同也會出現很多不同的要求。

這里只是一起學習分享,期待后續關于"人工智能"的內容。

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/254578.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/254578.shtml
英文地址,請注明出處:http://en.pswp.cn/news/254578.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

iOS 升級https的方案選擇

我的選擇是將UIWebView統一替換為WKWebView WKWebView AFN SDWebImage https的支持之前的博客都有涉及轉載于:https://www.cnblogs.com/Jusive/p/6867531.html

預處理指令(C#)

目錄預處理指令簡介#define、#undef#if、#elif、#else、#endif#warning、#error#region、#endregion#line、#line default#pragma預處理指令簡介 微軟對預處理指令解釋鏈接 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/preprocessor-directives/index…

NSWindowController的初始化創建代碼

-(PRAboutWindowController*)aboutCtrl{ if(_aboutCtrl nil){ _aboutCtrl [[PRAboutWindowController alloc]initWithWindowNibName:"PRAboutWindowController"]; } return _aboutCtrl ; } 轉載于:https://www.cnblogs.com/PJXWang/p/5816675.html

對CMMI3的學習和思考

原文出處: http://tech.it168.com/m/2007-08-02/200708020957750.shtml本文請勿轉載。近來筆者所在公司正在為過CMMI3做各種準備,對公司的員工進行了一些相關的培訓,作為項目管理人員的我,在學習CMMI3的過程中,也有了自…

Python3抓取糗百、不得姐

?點擊關注 異步圖書,置頂公眾號 每天與你分享 IT好書 技術干貨 職場知識 重要提示1:本文所列程序均基于Python3.6,低于Python3.6的Python版本可能無法運行.重要提示2:因所抓取的網站可能隨時更改展示內容,因此程序也需及時跟進.重要提示3:本程序僅供學習,不能拿去做…

halcon邊緣檢測的方法及各種方法的適用范圍

目錄一、邊緣提取二、BLOB分析檢測三、贓物檢測一、邊緣提取 1、設置ROI興趣區域 2、快速二值化,并連接相鄰區域。 這樣做的目的是進一步減少目標區域,通過二值化將目標區域大概輪廓提取出來 3、提取最接近目標區域的輪廓 常用函數有boundary&#xff0…

Oracle優化-表設計

前言  絕大多數的Oracle數據庫性能問題都是由于數據庫設計不合理造成的,只有少部分問題根植于Database Buffer、Share Pool、Redo Log Buffer等內存模塊配置不合理,I/O爭用,CPU爭用等DBA職責范圍上。所以除非是面對一個業已完成不可變更的系…

Win10遠程桌面 出現 身份驗證錯誤,要求的函數不受支持,這可能是由于CredSSP加密Oracle修正 解決方法...

升級至win10 最新版本10.0.17134,遠程桌面連接Window Server時報錯信息如下: 出現身份驗證錯誤,要求的函數不正確,這可能是由于CredSSP加密Oracle修正。 解決方法: 運行 gpedit.msc 本地組策略: 計算機配置…

CMM2

原文出處:http://hi.baidu.com/seaweaver/blog/item/e80e7af427f674d9f2d3854a.html CMM2的六個KPA 1、需求管理 (RM,Requirement Management) 2、軟件項目計劃 (SPP,Software Project Planning&#…

查看linux系統核數

查看linux系統核數: grep ^processor /proc/cpuinfo | wc -l轉載于:https://www.cnblogs.com/myyan/p/5822368.html

Rsyslog 日志相關內容

[rootserver vusers_home]# rpm -ql rsyslog|more ###.so結尾為模塊,模塊有分im為輸入模塊,om 為輸出模塊/etc/logrotate.d/syslog/etc/pki/rsyslog/etc/rc.d/init.d/rsyslog/etc/rsyslog.conf/etc/rsyslog.d/etc/sysconfig/rsyslog/lib64/rsyslog…

MFC導出對話框類DLL的實現

1.新建基于對話框的應用程序 2.新建MFC DLL工程 3.選擇MFC DLL 4.選擇擴展Dll選項(重要!!!) 5.為Dll工程添加一個MFC類,基類為CDialogEx 6.Dll新建的MFC 類中添加resource.h防止編譯出錯…

中國如何引進CMM評估,促進軟件產業發展

北京軟件行業協會 (本文轉載自軟件工程專家網www.21cmm.com) 一、CMM的含義及作用   CMM(軟件能力成熟度模型:Capability Maturity Model For Software)是由美國卡內基梅 隆大學的軟件工程研究所(SEI&a…

關于游戲平衡性——王者榮耀英雄傷害數值參考

收集王者榮耀各個英雄的裝備對技能增加的百分比,這樣的主要目的為保證游戲的平衡性。對于技能主要包括:血量、物理攻擊、法術攻擊、物理穿透、法術穿透、暴擊等。關于各個裝備,已經列成一張excel表格,在這里不再詳細描述表格。在這…

Swift-setValuesForKeysWithDictionary

重寫 setValuesForKeysWithDictionary 那么字典中可以有的字段在類中沒有對應屬性 class Person : NSObject {var age :Int 0 // 重寫 setValuesForKeysWithDictionary 那么字典中可以有的字段在類中沒有對應屬性override func setValuesForKeysWithDictionary(keyedValues…

hdu 1269 迷宮城堡(trajan判環)

題目鏈接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1269 題意&#xff1a;略 題解&#xff1a;trajan模版直接求強連通分量。 #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N 1e4 10; const i…

Arithmetic圖像處理halcon算子持續更新

目錄abs_diff_imageabs_imageacos_imageadd_imageasin_imageatan2_imageatan_imagecos_imagediv_imageexp_imagegamma_imageinvert_imagelog_imagemax_imagemin_imagemult_imagepow_imagescale_imagesin_imagesqrt_imagesub_imagetan_imageabs_diff_image 功能&#xff1a;計算…

身于“亂世”,我們程序員應該如何打算?

今天看了這篇文章&#xff0c; 發現自己也有點生處亂世&#xff0c;不平之感&#xff0c;但是文章的樸實卻讓我有了一個良好的反省&#xff0c;特此轉載 分類&#xff1a; 項目管理 2011-09-04 00:58 770人閱讀 評論(12) 收藏 舉報 不僅要低頭拉車&#xff0c;還要抬頭看路。…

Activity的啟動流程

Activity的啟動流程 努力工作 自己平時工作接觸的frameworks代碼比較多&#xff0c;但真正理解的很有限&#xff0c;一直在努力分析。。我主要還是用補丁的形式來看 core/java/android/app/Activity.java | 6 core/java/android/app/ActivityManagerNative.jav…

es6--箭頭函數

基本用法 ES6允許使用“箭頭”&#xff08;>&#xff09;定義函數。 var f v > v; 上面的箭頭函數等同于&#xff1a; var f function(v) {return v; }; 如果箭頭函數不需要參數或需要多個參數&#xff0c;就使用一個圓括號代表參數部分。 var f () > 5; // 等同于…