樹莓派之人臉識(shí)別與智能家居
訪問【W(wǎng)RITE-BUG數(shù)字空間】_[內(nèi)附完整源碼和文檔]
樹莓派加上攝像頭之后就可以拍照、錄視頻等各種功能了,這樣做一個(gè)樹莓派相機(jī)已經(jīng)是非常簡單的事情了。我們?cè)谶@里做一個(gè)簡單的人臉區(qū)域檢測的功能實(shí)驗(yàn),然后我們?cè)谙乱粋€(gè)實(shí)驗(yàn)讓樹莓派來控制風(fēng)扇轉(zhuǎn)動(dòng)。發(fā)現(xiàn)有人臉了,就開始轉(zhuǎn)動(dòng)風(fēng)扇。這也是生活中的一個(gè)場景,當(dāng)然加入實(shí)驗(yàn)3的溫度檢測根據(jù)溫度和人臉一起決定是否吹風(fēng)扇會(huì)更加精確化。
raspberry4
樹莓派之人臉識(shí)別與智能家居
樹莓派加上攝像頭之后就可以拍照、錄視頻等各種功能了,這樣做一個(gè)樹莓派相機(jī)已經(jīng)是非常簡單的事情了。我們?cè)谶@里做一個(gè)簡單的人臉區(qū)域檢測的功能實(shí)驗(yàn),然后我們?cè)谙乱粋€(gè)實(shí)驗(yàn)讓樹莓派來控制風(fēng)扇轉(zhuǎn)動(dòng)。發(fā)現(xiàn)有人臉了,就開始轉(zhuǎn)動(dòng)風(fēng)扇。這也是生活中的一個(gè)場景,當(dāng)然加入實(shí)驗(yàn) 3 的溫度檢測根據(jù)溫度和人臉一起決定是否吹風(fēng)扇會(huì)更加精確化。
實(shí)驗(yàn)材料準(zhǔn)備:原裝樹莓派 800?萬像素 CSI 攝像頭。
軟件:rasbian?系統(tǒng)、opencv
安裝必要的依賴庫:
安裝 OpenCV
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv
安裝 PiCamera 庫:
sudo apt-get install python-pip
sudo apt-get install python-dev
sudo pip install picamera
測試人臉識(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()



