2021-11-03 16:11:45 索煒達(dá)電子 1042
項(xiàng)目編號(hào):E2142
文件大?。?K
源碼說(shuō)明:帶中文注釋
開(kāi)發(fā)環(huán)境:Python
簡(jiǎn)要概述:
樹(shù)莓派之人臉識(shí)別與智能家居
樹(shù)莓派加上攝像頭之后就可以拍照、錄視頻等各種功能了,這樣做一個(gè)樹(shù)莓派相機(jī)已經(jīng)是非常簡(jiǎn)單的事情了。我們?cè)谶@里做一個(gè)簡(jiǎn)單的人臉區(qū)域檢測(cè)的功能實(shí)驗(yàn),然后我們?cè)谙乱粋€(gè)實(shí)驗(yàn)讓樹(shù)莓派來(lái)控制風(fēng)扇轉(zhuǎn)動(dòng)。發(fā)現(xiàn)有人臉了,就開(kāi)始轉(zhuǎn)動(dòng)風(fēng)扇。這也是生活中的一個(gè)場(chǎng)景,當(dāng)然加入實(shí)驗(yàn)3的溫度檢測(cè)根據(jù)溫度和人臉一起決定是否吹風(fēng)扇會(huì)更加精確化。
實(shí)驗(yàn)材料準(zhǔn)備:原裝樹(shù)莓派800萬(wàn)像素CSI攝像頭。
軟件:rasbian系統(tǒng)、opencv
環(huán)境配置:
使能camera模塊:
sudo raspi-config
安裝必要的依賴庫(kù):
安裝OpenCV
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv
安裝PiCamera庫(kù):
sudo apt-get install python-pip
sudo apt-get install python-dev
sudo pip install picamera
測(cè)試人臉識(shí)別代碼
import io
import picamera
import cv2
import numpy
#Create a memory stream so photos doesn't need to be saved in a file
stream = io.BytesIO()
#Get the picture (low resolution, so it should be quite fast)
#Here you can also specify other parameters (e.g.:rotate the image)
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.capture(stream, format='jpeg')
#Convert the picture into a numpy array
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
#Now creates an OpenCV image
image = cv2.imdecode(buff, 1)
#Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')
#Convert to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#Look for faces in the image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
print "Found "+str(len(faces))+" face(s)"
#Draw a rectangle around every found face
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)
#Save the result image
cv2.imwrite('result.jpg',image)
cv2.imshow('face_detect', image)
c = cv2.waitKey(0)
cv2.destroyAllWindows()
運(yùn)行成功圖
**代碼原理說(shuō)明:**我們使用與樹(shù)莓派原裝攝像頭匹配的picamera程序庫(kù)來(lái)獲取圖片信息然后利用opencv的人臉庫(kù)來(lái)識(shí)別是否有人臉,其中haarcascade_frontalface_alt.xml,就是opencv自帶的人臉模型庫(kù),我們就是利用這個(gè)識(shí)別出人臉的。這個(gè)路徑下還有眼睛、鼻子、人體等模型庫(kù),你也可以換成相應(yīng)的模型做相應(yīng)的識(shí)別。
6、樹(shù)莓派智能風(fēng)扇
本實(shí)驗(yàn)通過(guò)實(shí)驗(yàn)5的人臉檢測(cè)系統(tǒng)來(lái)判斷是否有人臉,當(dāng)有人臉的時(shí)候樹(shù)莓派控制風(fēng)扇轉(zhuǎn)動(dòng),當(dāng)沒(méi)有人臉時(shí)停止風(fēng)扇轉(zhuǎn)動(dòng)。
下面我們首先加入風(fēng)扇控制系統(tǒng):
由于實(shí)驗(yàn)條件的限制我們采用小功率風(fēng)扇做例子,例如:普通usb改裝風(fēng)扇、樹(shù)莓派散熱風(fēng)扇等直接由正負(fù)極控制的風(fēng)扇。
樹(shù)莓派的8號(hào)引腳連接到了usb改裝風(fēng)扇的正極,樹(shù)莓派的一根地線接改裝風(fēng)扇的負(fù)極,當(dāng)檢測(cè)到人臉時(shí),GPIO引腳輸出低電平,可以開(kāi)啟風(fēng)扇,當(dāng)沒(méi)有人臉時(shí)GPIO引腳輸出高電平,可以關(guān)閉風(fēng)扇。
測(cè)試代碼
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import io
import picamera
import cv2
import numpy
import time
import RPi.GPIO as GPIO
#GPIO setting for fan control
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
# Set pin 8 to be an output pin and set initial value to high
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)
#get the pictures and found face
while True :
#Create a memory stream so photos doesn't need to be saved
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.capture(stream, format='jpeg')
#Convert the picture into a numpy array
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
#Now creates an OpenCV image
image = cv2.imdecode(buff, 1)
#Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')
#Convert to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#Look for faces in the image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
print "Found "+str(len(faces))+" face(s)"
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)
cv2.imwrite('result.jpg',image)
# if found face turn on the fan
if len(faces) > 0 :
GPIO.output(8, GPIO.HIGH) # Turn on
else :
GPIO.output(8, GPIO.LOW) # Turn off
time.sleep(1)
如果一切順利的話我們會(huì)看到,當(dāng)攝像頭發(fā)現(xiàn)人臉的時(shí)候,風(fēng)扇開(kāi)啟了轉(zhuǎn)動(dòng)、當(dāng)沒(méi)有人臉的時(shí)候,風(fēng)扇停止了轉(zhuǎn)動(dòng)。
目錄│文件列表:
└ raspberry4
│ face_detect.py
└ face_fan.py