第1步:主要項目必需
Raspberry Pi攝像機
繼電器
555計時器。..。(如果您的555計時器未到達,則為arduino和另一個繼電器)
電磁閥
灑水器
電子產(chǎn)品的某種外殼
愿意用6噸大錘打一個隱喻釘子
分辨率很低的相機你幾乎看不到水,但仍然可以看到貓跑來掩護
第2步:系統(tǒng)
1,Pi相機檢測到一個貓大小的物體移動幾個相機幀(下一步驟中已經(jīng)開始)
2,Pi啟動噴水器
3,Cat跑步封面
4,視頻自動上傳到y(tǒng)outube以便查看樂趣
第3步:編碼時間
使用openCV使用幀減法,您可以找到隨時間變化的幀的區(qū)域,使用一些漂亮的函數(shù),您可以確定這些變化有多大以及它們是否會持續(xù)存在,最重要的是找出它們是否是貓大小。
有相當?shù)腶f關(guān)于幀減法的新教程,如果你進行快速谷歌搜索,會詳細介紹。
代碼工作原理概述
1,相機不斷拍攝幀并將它們與最后
2,如果檢測到貓大小的形狀,則注意到
3,如果貓大小的變化持續(xù)超過4幀,則pi使用其GPIO為繼電器供電以啟動arduino
4,arduino發(fā)送信號給第二個繼電器供電5秒鐘,激活電磁閥
5,電磁閥通電時允許水進入噴水器
6,當噴水器處于活動狀態(tài)時,攝像頭停止檢測并記錄視頻
7,視頻上傳到Y(jié)outube
8,靜止圖像上傳到保管箱以進行微調(diào)系統(tǒng)
注意 - 為什么我最終使用2個繼電器和一個arduino打開電磁閥5秒鐘。..。..。
1,pi錄制視頻時pi不能啟動和停止電磁閥scrips暫停直到視頻結(jié)束,因此需要arduino(或555計時器)允許當視頻仍在錄制時,電磁鐵打開和關(guān)閉獨立于腳本。
2,第一個繼電器和arduino可以用555定時器替換,但是沒有及時發(fā)布這個項目,555將節(jié)省大量的時間和金錢和步驟。
3,pi不能直接觸發(fā)螺線管,因為Pi GPIO工作在3.3v和51mA最大值,螺線管需要5V和更多觸發(fā)時不超過51mA。
4,可以裁剪每個幀以去除不需要的區(qū)域中的運動檢測,例如neigbors garden。如果不這樣做會導致鄰居給你的花園帶來困惑的看法,因為每次他想進入他的棚子時灑水器就會熄火。
5,我可能錯過了一些明顯的東西,浪費了我的時間來設置它像這樣。
以下代碼
import cv2
import numpy as np
import argparse #cat
import time
import RPi.GPIO as GPIO
import os
import dropbox
from picamera.array import PiRGBArray
from picamera import PiCamera
#------------------------------------------------Upload to youtube---------------------------------------
def HDtoYoutube():
ctime = time.strftime(“_%H-%M-%S”)
cdate = time.strftime(“_%d-%m-%Y”)
vidname = ctime + cdate
#Trigger relay
GPIO.output(11,True)
time.sleep(.5)
GPIO.output(11,F(xiàn)alse)
print(“Taking Video”)
try:
#Take Video
os.system(‘raspivid -w 1640 -h 922 -o vid{0}.h264 -t 15000’.format(vidname))
#Upload to youtube
print(“Uploading to YouTube”)
os.system(‘sudo youtube-upload --title=“Cat Got Wet {0}” --client-secrets=client_secret.json vid{0}.h264’.format(vidname))
#Remove video file when done
os.remove(‘vid{0}.h264’.format(vidname))
print(“Video uploaded and removed from Pi”)
except:
pass
#------------------------------------------------Stills to dropbox---------------------------------------
def StillsToDropbox():
print(“Uploading Still To Dropbox Function”)
access_token = ‘Ah ah ah, you didn’t say the magic word.。.Ah ah ah, you didn‘t say the magic word’
ctime = time.strftime(“%H:%M:%S”)
cdate = time.strftime(“%d-%m-%Y”)
try:
filename = “/Motion/{0}/DetectedAt_{1}.jpg”.format(cdate, ctime)
print(filename)
client = dropbox.client.DropboxClient(access_token)
image = open(“ToDropbox.jpg”, ‘rb’)
client.put_file(filename, image)
image.close()
os.remove(“ToDropbox.jpg”)
except:
pass
#------------------------------------------------Detect motion-----------------------------------------
def DetectMotion():
#Define vars
min_area = 400
tolarance = 25 #change in pixel
bluramount = 21
timetoforget = 0.5
kernel = np.ones((5,5),np.uint8) #used for dialate
MotionCounter = 0
MinTargetArea = 600 #smallest size to detect
MaxTargetArea = 5000 #Largest size to detect
now = time.time()
then = time.time()
#initialise camera
camera = PiCamera()
camera.resolution = (640,480)
camera.framerate = 10
rawCapture = PiRGBArray(camera, size=(640,480))
#warmup camera
time.sleep(1)
#Grab first frame & prep it to go into cv2.acumulate weight
camera.capture(rawCapture, format=“bgr”)
avg = rawCapture.array
#Crop out unwanted region
PolyCrop = np.array( [[[362,480],[613,365],[628,161],[498,0],[640,0],[640,480]]], dtype=np.int32 )
cv2.fillPoly(avg, PolyCrop, 0,0,0)
#Process image
avg = cv2.cvtColor(avg, cv2.COLOR_BGR2GRAY)
avg = cv2.GaussianBlur(avg, (bluramount, bluramount), 0)
avg = avg.copy().astype(“float”)
rawCapture.truncate(0)
print(“Ready to detect”)
#capture frames
for frame in camera.capture_continuous(rawCapture, format=“bgr”, use_video_port=True):
#Pause Switch
loopgo = GPIO.input(PauseNow)
#print(loopgo)
while loopgo == 0:
#print(loopgo)
loopgo = GPIO.input(PauseNow)
time.sleep(1)
#grabs raw numpy array
currentframe = frame.array
key = cv2.waitKey(1) & 0xFF
#Crop out unwanted region
cv2.fillPoly(currentframe, PolyCrop, 0,0,0)
rawCapture.truncate(0) #Clear frame buffer for next loop
currentgray = cv2.cvtColor(currentframe, cv2.COLOR_BGR2GRAY)
currentgray = cv2.GaussianBlur(currentgray, (bluramount, bluramount), 0)
#make time average frame
cv2.accumulateWeighted(currentgray, avg, timetoforget)
#get difference in frame
frameDelta = cv2.absdiff(currentgray, cv2.convertScaleAbs(avg))
thresh = cv2.threshold(frameDelta, tolarance, 255, cv2.THRESH_BINARY)[1]
#Turn to blob
thresh = cv2.dilate(thresh, kernel, iterations = 10) #dilate
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel) #close holes
thresh = cv2.erode(thresh, kernel, iterations = 5) #erode
#contours
_, cnts, _= cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# loop over the contours
for c in cnts:
# if the contour is too small, ignore it
if cv2.contourArea(c) 《 min_area:
continue
# compute the bounding box for the contour, draw it on the frame,
# and update the textq
(x, y, w, h) = cv2.boundingRect(c)
#Too small : Red Box
if cv2.contourArea(c) 《 MinTargetArea:
cv2.rectangle(currentframe, (x, y), (x + w, y + h), (0, 0, 255), 2)
#MotionCounter = MotionCounter + 1 #Debug take all the pictures
print(“MotionDetected”)
#Just right : Green Box
if cv2.contourArea(c) 》= MinTargetArea and cv2.contourArea(c) 《= MaxTargetArea:
cv2.rectangle(currentframe, (x, y), (x + w, y + h), (0, 255, 0), 2)
MotionCounter = MotionCounter + 1 #Debug take all the pictures
print(“MotionDetected”)
#Too big : Blue Box
if cv2.contourArea(c) 》 MaxTargetArea:
cv2.rectangle(currentframe, (x, y), (x + w, y + h), (255, 0, 0), 2)
#MotionCounter = MotionCounter + 1 #Debug take all the pictures
print(“MotionDetected”)
#Keep now up to date
now = time.time()
#MotionCounterTimer
if (MotionCounter 》 0):
if (now - then 》 10):
MotionCounter = 0
then = time.time()
#Break loop on pressing Q
if key == ord(“q”):
break
#If motion persists save current frame and activate countermeasures
if MotionCounter 》= 4:
MotionCounter = 0
cv2.imwrite(‘ToDropbox.jpg’, currentframe)
camera.close()
return True
#------------------------------------------------Main---------------------------------------
try:
#Set Pins
GPIO.setmode(GPIO.BOARD)
PauseNow=12
GPIO.setup(11,GPIO.OUT)
GPIO.setup(PauseNow,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True:
MotionDetected = False
MotionDetected = DetectMotion()
if MotionDetected == True:
HDtoYoutube()
StillsToDropbox()
except KeyboardInterrupt:
print(“Keyboard Interupt”)
except:
print(“Other Error”)
finally:
GPIO.cleanup()
#HowToTriggerRealProgrammersWithBadCode
第4步:將它放在一起
將電器塞入防水外殼,將物品擰入墻壁并使用大量膠帶和熱膠
第5步:結(jié)果
什么時候有效
第6步:誤報
如果沒有,它會噴射貓影,你的妻子和你的女兒。
Pro -tip - 在門旁邊放一個開關(guān),暫停運動檢測程序。..。.然后忘記使用它并在放入垃圾箱時弄濕。
-
計算機視覺
+關(guān)注
關(guān)注
8文章
1699瀏覽量
46050 -
智能灑水器
+關(guān)注
關(guān)注
0文章
2瀏覽量
1090
發(fā)布評論請先 登錄
相關(guān)推薦
評論