概述
人臉識(shí)別,是基于人的臉部特征信息進(jìn)行身份識(shí)別的一種生物識(shí)別技術(shù)。用攝像機(jī)或攝像頭采集含有人臉的圖像或視頻流,并自動(dòng)在圖像中檢測(cè)和跟蹤人臉,進(jìn)而對(duì)檢測(cè)到的人臉進(jìn)行臉部識(shí)別。
本項(xiàng)目中我們將實(shí)現(xiàn)攝像頭人臉識(shí)別效果,包含人名標(biāo)簽和識(shí)別框,并結(jié)合燈帶的亮燈狀態(tài)制作人臉識(shí)別門禁系統(tǒng)。
(最終效果視頻)
項(xiàng)目基礎(chǔ)
人臉識(shí)別
硬件準(zhǔn)備:
AI主控:LattePanda
輸入輸出設(shè)備:5英寸顯示屏、鍵盤、鼠標(biāo)、攝像頭
人臉信息錄入:
1、雙擊桌面上的“startpage.sh”,打開JupyterLab,切換到“home/lattepanda/桌面/LattePanda&AI項(xiàng)目實(shí)戰(zhàn)/”目錄下,如下圖,檢查一下項(xiàng)目必需的3個(gè)文件;
2、雙擊進(jìn)入“圖片拍攝”文件夾;
3、雙擊打開程序,修改拍攝的照片數(shù)量,例如for index in range(3);
樣例代碼:
4、選擇無其他人、無雜物的背景,正眼看著攝像頭,然后運(yùn)行程序,拍攝的圖片會(huì)在程序同目錄下依次出現(xiàn);
5、雙擊圖片查看拍攝效果;
6、選擇效果最好的一張,重命名為此人的姓名;
7、將照片復(fù)制到“LattePanda&AI-人臉識(shí)別門禁系統(tǒng)”文件夾下。
程序編寫:
1、雙擊打開“人臉識(shí)別.ipynb”;
樣例代碼:
復(fù)制代碼 隱藏代碼
#導(dǎo)入人臉識(shí)別模塊
from
faceRecognition import *
#人臉檢測(cè)與識(shí)別文件調(diào)用
faceDetectorPath
=
"face-detection-retail-0005.xml"
landmarksPath
=
"landmarks-regression-retail-0009.xml"
faceReidentificationPath
=
"face-reidentification-retail-0095.xml"
#調(diào)用訓(xùn)練模型文件
model
= Model()
model
.load(faceDetectorPath = faceDetectorPath,
landmarksPath
= landmarksPath,
faceReidentificationPath
= faceReidentificationPath)
#初始化攝像頭與窗口
camera
= Camera()
screen
= Screen(
"人臉識(shí)別門禁系統(tǒng)"
, (
0
,
0
,
0
))
#打開手寫數(shù)字交互窗口,按下“Q”鍵退出窗口
if_run
=
1
while
(if_run ==
1
):
#從攝像頭獲取圖片
image
= camera.read(flip = False)
#圖片剪裁
image
= model.clipResizeFrame(image)
screen
.clear()
#獲取人臉識(shí)別結(jié)果并在屏幕上顯示識(shí)別標(biāo)簽
results
= model.predict(image)
screen
.putImage(image,
80
,
0
,
640
,
480
)
for
roi, landmarks, identity in zip(*results):
x
, y = roi.position
w
, h = roi.size
screen
.putTag(identity, x+
80
, y, w, h, bg=(
0
,
255
,
0
))
#打開與顯示交互窗口,如果按下Q鍵,將無法進(jìn)入下一次while循環(huán)
if
screen.show():
if_run
=
0
screen
.quit()
復(fù)制代碼
2、運(yùn)行程序,當(dāng)執(zhí)行到最后一個(gè)單元格時(shí),會(huì)打開交互窗口。
未識(shí)別到人臉:
檢測(cè)到未知人臉:
識(shí)別到已知人臉,并顯示此人姓名:
3、按鍵盤上的“Q”鍵可退出交互窗口。
項(xiàng)目進(jìn)階
人臉識(shí)別門禁系統(tǒng)
如果讓人臉作為門禁系統(tǒng)的鑰匙,會(huì)使我們的生活更方便快捷。當(dāng)識(shí)別到主人的人臉時(shí),燈帶亮綠燈,表示準(zhǔn)許進(jìn)入;否則顯示紅燈。
硬件準(zhǔn)備:
主控:Arduino UNO、IO 傳感器擴(kuò)展板 V7.1
模塊:WS2812 RGB 全彩燈帶
硬件連接圖:
*WS2812上有7個(gè)RGB燈,程序中的np[0]表示第一個(gè)燈,程序中的np[1]表示第二個(gè)燈。
程序編寫:
雙擊打開“人臉識(shí)別_燈帶.ipynb”;
樣例代碼:
復(fù)制代碼 隱藏代碼
#導(dǎo)入人臉識(shí)別模塊
from
faceRecognition import *
import
time
from
pinpong.board import Board,Pin,NeoPixel
NEOPIXEL_PIN
= Pin.D
7
PIXELS_NUM
=
1
#燈數(shù),如果需要多個(gè)燈亮,請(qǐng)改此數(shù)值
#初始化,選擇板型和端口號(hào)
Board
(
"uno"
,
"/dev/ttyUSB0"
).begin()
np
= NeoPixel(Pin(NEOPIXEL_PIN), PIXELS_NUM) #np[
0
]表示第一個(gè)燈,np[
1
]表示第二個(gè)燈,以此類推
#人臉檢測(cè)與識(shí)別文件調(diào)用
faceDetectorPath
=
"face-detection-retail-0005.xml"
landmarksPath
=
"landmarks-regression-retail-0009.xml"
faceReidentificationPath
=
"face-reidentification-retail-0095.xml"
#調(diào)用訓(xùn)練模型文件
model
= Model()
model
.load(faceDetectorPath = faceDetectorPath,
landmarksPath
= landmarksPath,
faceReidentificationPath
= faceReidentificationPath)
#初始化攝像頭與窗口
camera
= Camera()
screen
= Screen(
"人臉識(shí)別門禁系統(tǒng)"
, (
0
,
0
,
0
))
#打開手寫數(shù)字交互窗口,按下“Q”鍵退出窗口
if_run
=
1
led
=
0
count
=
0
while
(if_run ==
1
):
#從攝像頭獲取圖片
image
= camera.read(flip = False)
#圖片剪裁
image
= model.clipResizeFrame(image)
screen
.clear()
#獲取人臉識(shí)別結(jié)果并在屏幕上顯示識(shí)別標(biāo)簽
results
= model.predict(image)
screen
.putImage(image,
80
,
0
,
640
,
480
)
for
roi, landmarks, identity in zip(*results):
x
, y = roi.position
w
, h = roi.size
screen
.putTag(identity, x+
80
, y, w, h, bg=(
0
,
255
,
0
))
#count>30,修改30可調(diào)節(jié)切換燈顏色的速度
if
identity !=
"未知人臉"
and count>
30
:
np
[
0
] = (
0
,
255
,
0
) #設(shè)置第一個(gè)燈亮綠色
#np[1] = (0, 255 ,0) #設(shè)置第二個(gè)燈亮綠色
count
=
0
elif
identity ==
"未知人臉"
and count>
30
:
np
[
0
] = (
255
,
0
,
0
) #設(shè)置第一個(gè)燈亮紅色
#np[1] = (255, 0 ,0) #設(shè)置第二個(gè)燈亮紅色
count
=
0
count
+=
1
#打開與顯示交互窗口,如果按下Q鍵,將無法進(jìn)入下一次while循環(huán)
if
screen.show():
if_run
=
0
screen
.quit()
復(fù)制代碼
運(yùn)行效果:
當(dāng)識(shí)別到已知人臉時(shí),燈帶的第一個(gè)燈亮綠色;
當(dāng)屏幕中沒有人臉或者是未知人臉時(shí),燈帶的第一個(gè)燈亮紅色。
本文轉(zhuǎn)載至:DF創(chuàng)客社區(qū)
原文鏈接:https://mc.dfrobot.com.cn/thread-306871-1-1.html
-
英特爾
+關(guān)注
關(guān)注
61文章
10007瀏覽量
172326 -
操作系統(tǒng)
+關(guān)注
關(guān)注
37文章
6892瀏覽量
123741 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5121瀏覽量
98189 -
Win10
+關(guān)注
關(guān)注
2文章
710瀏覽量
40146
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論