第1步:主要项目必需
raspberry pi zero& sd卡
raspberry pi摄像机
继电器
555计时器。..。(如果您的555计时器未到达,则为arduino和另一个继电器)
电磁阀
洒水器
电子产品的某种外壳
愿意用6吨大锤打一个隐喻钉子
分辨率很低的相机你几乎看不到水,但仍然可以看到猫跑来掩护
第2步:系统
1,pi相机检测到一个猫大小的物体移动几个相机帧(下一步骤中已经开始)
2,pi启动喷水器
3,cat跑步封面
4,视频自动上传到youtube以便查看乐趣
第3步:编码时间
使用opencv使用帧减法,您可以找到随时间变化的帧的区域,使用一些漂亮的函数,您可以确定这些变化有多大以及它们是否会持续存在,最重要的是找出它们是否是猫大小。
有相当的af关于帧减法的新教程,如果你进行快速谷歌搜索,会详细介绍。
代码工作原理概述
1,相机不断拍摄帧并将它们与最后
2,如果检测到猫大小的形状,则注意到
3,如果猫大小的变化持续超过4帧,则pi使用其gpio为继电器供电以启动arduino
4,arduino发送信号给第二个继电器供电5秒钟,激活电磁阀
5,电磁阀通电时允许水进入喷水器
6,当喷水器处于活动状态时,摄像头停止检测并记录视频
7,视频上传到youtube
8,静止图像上传到保管箱以进行微调系统
注意 - 为什么我最终使用2个继电器和一个arduino打开电磁阀5秒钟。..。..。
1,pi录制视频时pi不能启动和停止电磁阀scrips暂停直到视频结束,因此需要arduino(或555计时器)允许当视频仍在录制时,电磁铁打开和关闭独立于脚本。
2,第一个继电器和arduino可以用555定时器替换,但是没有及时发布这个项目,555将节省大量的时间和金钱和步骤。
3,pi不能直接触发螺线管,因为pi gpio工作在3.3v和51ma最大值,螺线管需要5v和更多触发时不超过51ma。
4,可以裁剪每个帧以去除不需要的区域中的运动检测,例如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,false)
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步:结果
什么时候有效
第6步:误报
如果没有,它会喷射猫影,你的妻子和你的女儿。
pro -tip - 在门旁边放一个开关,暂停运动检测程序。..。.然后忘记使用它并在放入垃圾箱时弄湿。
Zeland IE3D在3D结构的电磁场仿真优化中的计算案例
深度学习与神经网络推动AI芯片市场以约40%的年成长率持续扩张
佳能Pixma CD磁盘托盘的制作
15KHZ-20KHZ超声波数字焊接电源发生器
纳米位移台在扫描系统中的应用
如何制作计算机视觉猫洒水器
电子元器件连接板制作教程
汽车NICD电池充电器电路图
超细间距推拉力机,有哪些技术规格和特点?尺寸、重量、焊接强度
走进重庆力帆专场活动 多家供应商展现技术亮点
谈谈电路设计时所需要的小经验
800V电驱动系统助力新能源汽车产业快速发展
智能立体网,仅有小基站设备是远远不够的
华为上诉后,瑞典暂时取消对华为和中兴通讯公司的禁令
Allegro中尺寸标注参数的设置
什么是list?
柔宇科技柔性显示面板业务与科创板定位的天然契合性
IC观察:FPGA市场大有可为
数控车床中滚珠螺母的维护保养方法
C2530(zigbee)入门开发: 低功耗模式