极品馒头泬19p,国产精品亚洲一区二区三区,狠狠色噜噜狠狠狠7777奇米,国产精品视频一区二区三区无码,国产欧美日韩久久久久

【E2142】樹莓派之人臉識別與智能家居

2021-11-03 16:11:45      索煒達電子      1040     

項目編號:E2142

文件大?。?K

源碼說明:帶中文注釋

開發(fā)環(huán)境:Python

簡要概述:

樹莓派之人臉識別與智能家居

樹莓派加上攝像頭之后就可以拍照、錄視頻等各種功能了,這樣做一個樹莓派相機已經(jīng)是非常簡單的事情了。我們在這里做一個簡單的人臉區(qū)域檢測的功能實驗,然后我們在下一個實驗讓樹莓派來控制風(fēng)扇轉(zhuǎn)動。發(fā)現(xiàn)有人臉了,就開始轉(zhuǎn)動風(fēng)扇。這也是生活中的一個場景,當然加入實驗3的溫度檢測根據(jù)溫度和人臉一起決定是否吹風(fēng)扇會更加精確化。

實驗材料準備:原裝樹莓派800萬像素CSI攝像頭。

軟件:rasbian系統(tǒng)、opencv

【E2142】樹莓派之人臉識別與智能家居

環(huán)境配置:


使能camera模塊:


sudo raspi-config

【E2142】樹莓派之人臉識別與智能家居

【E2142】樹莓派之人臉識別與智能家居

安裝必要的依賴庫:


安裝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)

#Save the result image

cv2.imwrite('result.jpg',image)

cv2.imshow('face_detect', image)

c = cv2.waitKey(0)

cv2.destroyAllWindows()

運行成功圖

【E2142】樹莓派之人臉識別與智能家居

**代碼原理說明:**我們使用與樹莓派原裝攝像頭匹配的picamera程序庫來獲取圖片信息然后利用opencv的人臉庫來識別是否有人臉,其中haarcascade_frontalface_alt.xml,就是opencv自帶的人臉模型庫,我們就是利用這個識別出人臉的。這個路徑下還有眼睛、鼻子、人體等模型庫,你也可以換成相應(yīng)的模型做相應(yīng)的識別。


6、樹莓派智能風(fēng)扇


本實驗通過實驗5的人臉檢測系統(tǒng)來判斷是否有人臉,當有人臉的時候樹莓派控制風(fēng)扇轉(zhuǎn)動,當沒有人臉時停止風(fēng)扇轉(zhuǎn)動。


下面我們首先加入風(fēng)扇控制系統(tǒng):


由于實驗條件的限制我們采用小功率風(fēng)扇做例子,例如:普通usb改裝風(fēng)扇、樹莓派散熱風(fēng)扇等直接由正負極控制的風(fēng)扇。


樹莓派的8號引腳連接到了usb改裝風(fēng)扇的正極,樹莓派的一根地線接改裝風(fēng)扇的負極,當檢測到人臉時,GPIO引腳輸出低電平,可以開啟風(fēng)扇,當沒有人臉時GPIO引腳輸出高電平,可以關(guān)閉風(fēng)扇。


測試代碼

#!/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)

如果一切順利的話我們會看到,當攝像頭發(fā)現(xiàn)人臉的時候,風(fēng)扇開啟了轉(zhuǎn)動、當沒有人臉的時候,風(fēng)扇停止了轉(zhuǎn)動。

目錄│文件列表:

 └ raspberry4

    │ face_detect.py

    └ face_fan.py

TAG樹莓派
  • 6 次
  • 1 分