Shoots automatically when an object crosses! Introducing a system that sends images to LINE.
** Is your cat eating properly during a one-night, two-day excursion? I was worried if I was drinking water, so I made it. ** **
It will be a simple system using Raspberry Pi, It is recommended because you can check the camera image on LINE while you are out, such as those who have pets.
Book "Gently Start Raspberry Pi" It's a very easy-to-understand book for beginners of Raspberry Pi, and I highly recommend it. About 80% of this system borrowed ideas from this book m (_ _) m
This is the GitHub repository. https://github.com/akiraseto/security_cam
Raspberry Pi (hereinafter referred to as Raspberry Pi) is used to connect the sensor and the camera.
Connect motion sensor with GPIO Detects when objects that are hotter than the surroundings, such as people and animals, move using infrared rays. I used the unmarked parts of the set for the motion sensor, but there is no problem in using it with the "SE-10" sold in Akizuki. I think. Connect the sensor OUTPUT to GPIO: No. 23 of Raspberry Pi Raspberry Pi GPIO: Connect LED anode to number 18
USB connection with webcam Just stick it in the USB terminal like a PC, and you don't need to do anything special.
Like this ↓ ↓
LINEnotify With the API provided by LINE, you can freely send various notifications to LINE by incorporating it into the program. It's quite convenient and seems to have various uses. https://notify-bot.line.me/ja/
This page is detailed. https://qiita.com/iitenkida7/items/576a8226ba6584864d95
Register and cooperate to issue an access token. The access token is displayed only once, so be careful not to forget to copy it.
security_cam/
├── README.md
├── config.py #LINE Notify token
└── security_cam.py
#Image storage directory
/media/pi/rasUSB/security_cam/img
As for the images, I don't want to overwhelm the capacity of the boot disk, so I save them separately on a USB memory.
Exaggeratedly saying the whole structure, the actual program file is only security_cam.py
.
security_cam.py
import RPi.GPIO as GPIO
from time import sleep
import datetime, requests, cv2, os, glob, config
#LINE Notify token
TOKEN = config.TOKEN
API = 'https://notify-api.line.me/api/notify'
#GPIO port settings
SENSOR_PORT = 23
LED_PORT = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PORT, GPIO.IN)
GPIO.setup(LED_PORT, GPIO.OUT)
#CV2 preparation
camera = cv2.VideoCapture(0)
IMG_SIZE = (600,400)
#Execute a photo shoot command(File name to date and time)
last_post = datetime.datetime(2000, 1, 1) #Initialize properly
def take_photo():
global last_post
#Take a photo
now = datetime.datetime.now()
fname = "/media/pi/rasUSB/security_cam/img/" + now.strftime('%Y-%m-%d_%H-%M-%S') + ".jpg "
#Delete old image
file_list = glob.glob("/media/pi/rasUSB/security_cam/img/*jpg")
dif_time = datetime.timedelta(days=3)
for file in file_list:
file_time = datetime.datetime.fromtimestamp(os.path.getatime(file))
if (file_time < now - dif_time):
print("remove:{0}".format(file))
os.remove(file)
#photograph
_, frame = camera.read()
img = cv2.resize(frame, IMG_SIZE)
ret = cv2.imwrite(fname, img)
if ret:
print('Captured image: ' + fname)
else:
print('Failed to write image.')
#Notify LINE
#However, 10 minutes will not be notified--- (*1)
sec = (now - last_post).seconds
if sec < 10 * 60: return
last_post = now
#Insert notifications into LINE--- (*2)
post_data = {'message': 'Nyans'}
headers = {'Authorization': 'Bearer ' + TOKEN}
files={'imageFile': open(fname,'rb')}
res = requests.post(API, data=post_data,
headers=headers,files=files)
print(res.text)
try:
sw = 0 #Continuous shooting prevention
#Get the value of the sensor repeatedly
while True:
v = GPIO.input(SENSOR_PORT)
if v == GPIO.HIGH:
GPIO.output(LED_PORT, GPIO.HIGH)
take_photo()
sw = 1
else:
GPIO.output(LED_PORT, GPIO.LOW)
sw = 0
sleep(10.0)
except KeyboardInterrupt:
pass
GPIO.cleanup()
--I am using the ʻopencvlibrary for camera shooting. There are several ways to install opencv on Raspberry Pi, but this is the easiest and quickest to recommend. https://karaage.hatenadiary.jp/entry/2018/10/05/073000 --Saved images are deleted more than 3 days ago. If you want to extend the number of days, change it to an appropriate number of days with L30:
dif_time = datetime.timedelta (days = 3)`.
This is a simple program.
To be honest, I use it only occasionally. Not only cats, but every time my family crossed, LINE notifications came bang bang. .. (´Д `) As a recommended usage It's time to leave home for a while, such as a one-night, two-day excursion or going out with your family from morning till night. It feels good to play an active part in checking the state of the cat.
How about those who are worried about eating and drinking water for their pets while they are out? Of course, it's fun to feel relaxed through LINE, such as "Did you come to drink water?" And "You eat too much food."
Recommended Posts