訪問【W(wǎng)RITE-BUG數(shù)字空間】_[內(nèi)附完整源碼和文檔]
樹莓派加上攝像頭之后就可以拍照、錄視頻等各種功能了,這樣做一個樹莓派相機已經(jīng)是非常簡單的事情了。我們在這里做一個簡單的人臉區(qū)域檢測的功能實驗,然后我們在下一個實驗讓樹莓派來控制風(fēng)扇轉(zhuǎn)動。發(fā)現(xiàn)有人臉了,就開始轉(zhuǎn)動風(fēng)扇。這也是生活中的一個場景,當(dāng)然加入實驗3的溫度檢測根據(jù)溫度和人臉一起決定是否吹風(fēng)扇會更加精確化。
raspberry4
樹莓派之人臉識別與智能家居
樹莓派加上攝像頭之后就可以拍照、錄視頻等各種功能了,這樣做一個樹莓派相機已經(jīng)是非常簡單的事情了。我們在這里做一個簡單的人臉區(qū)域檢測的功能實驗,然后我們在下一個實驗讓樹莓派來控制風(fēng)扇轉(zhuǎn)動。發(fā)現(xiàn)有人臉了,就開始轉(zhuǎn)動風(fēng)扇。這也是生活中的一個場景,當(dāng)然加入實驗 3 的溫度檢測根據(jù)溫度和人臉一起決定是否吹風(fēng)扇會更加精確化。
實驗材料準(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
測試人臉識別代碼
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)文章來源:http://www.zghlxwxcb.cn/news/detail-503553.html
Save the result image
cv2.imwrite(‘result.jpg’,image)
cv2.imshow(‘face_detect’, image)
c = cv2.waitKey(0)
cv2.destroyAllWindows()文章來源地址http://www.zghlxwxcb.cn/news/detail-503553.html
到了這里,關(guān)于樹莓派之人臉識別與智能家居的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!