[PYTHON] Pet monitoring with Rekognition and Raspberry pi

happy New Year. I'm Fujisaki, a dog idiot. I heard that Amazon Rekognition, a service that can recognize images, was released at the end of last year, so I played with it.

Motivation for creation

Surveillance cameras using motion sensors and Raspberry Pi have been made for a long time, but since they take garbage data with a little movement, only limited images such as dogs and family members are selected and left. After that, I wish I could erase it.

Preparation

Make a surveillance camera with motion sensor & Raspberry Pi

material

[* Raspberry Pi *] It's summer! Let's make a cicada sound using a motion sensor I was allowed to refer to. Thank you very much. KIMG1860.JPG

Use fswebcam to link the Raspberry Pi and the USB webcam. I referred to the official page here.

Use os.system to call this from Python.

RekogDogCamera.py


import time
from time import gmtime,strftime
import RPi.GPIO as GPIO

INTERVAL = 3
SLEEPTIME = 5
SENSOR_PIN = 25

GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)

st = time.time()-INTERVAL
while True:
        if( GPIO.input(SENSOR_PIN) == GPIO.HIGH ) and (st + INTERVAL < time.time() ):
                st = time.time()
                strf = strftime("%Y%m%d-%H:%M:%S")
                strfile = strf + '.jpg'
                print("movement detected " + strf)
                os.system('fswebcam --no-banner /home/pi/myphoto/'+ strfile)
              
        time.sleep(SLEEPTIME)
GPIO.cleanup()

Give the captured image to S3

I used Boto3. After setting ~ / .aws / credentials etc. according to the document, add the part to upload to S3 in the loop of the above code.

RekogDogCamera.py


import boto3
bucket_name = 'mybucket'
s3_client = boto3.client('s3')
s3_client.upload_file('/home/pi/myphoto/' + strfile ,bucket_name, strfile)

Now you are ready to go.

Let's play with new ones!

Use Rekognition to leave only a photo of my child

I tried the Rekognition demo. This time I'm interested in Object and scene detection. rekognitiondemo.JPG

Follow the Download SDK to read the documentation. Amazon Rekognition Developer Guide There was something I wanted on the 28th page of the PDF, so I will try it from the console once. Postscript: The HTML version was here. → [Exercise 1: Detect Labels in an Image (API)] (http://docs.aws.amazon.com/ja_jp/rekognition/latest/dg/get-started-exercise-detect-labels.html)

$ aws rekognition detect-labels --image '{"S3Object":{"Bucket":"mybucket", "Name":"mydog.jpg "}}' --region us-east-1 --profile default

The photo I used in the demo earlier returned a response like this.

result


 {
    "Labels": [
        {
            "Confidence": 98.25569915771484,
            "Name": "Animal"
        },
        {
            "Confidence": 98.25569915771484,
            "Name": "Canine"
        },
        {
            "Confidence": 98.25569915771484,
            "Name": "Dog"
        },
        {
            "Confidence": 98.25569915771484,
            "Name": "Husky"
        },
        {
            "Confidence": 98.25569915771484,
            "Name": "Mammal"
        },
        {
            "Confidence": 98.25569915771484,
            "Name": "Pet"
        },
        {
            "Confidence": 95.74263763427734,
            "Name": "Eskimo Dog"
        },

(* Aside: Not an Eskimo dog or a husky, my child is a Kishu dog ...)

Try calling from the Python code above. Since os.system is used easily, I will do my best to escape quotes.

RekogDogCamera.py


result = os.system("aws rekognition detect-labels --image \'{\"S3Object\":{\"Bucket\":\""+bucket_name+"\", \"Name\":\""+ strfile +"\"}}\' --region us-east-1 --profile default > temp.json")
                

Loop the return response and report "Canine found" if "Name" has "Canine" and "Confidence" is 55 or higher. ..

RekogDogCamera.py


with open('temp.json') as json_data:
    data = json.load(json_data)
    for d in data["Labels"]:
        if (d["Name"] == "Canine" and d["Confidence"] > 55.0):
            print ("Canine found: " + str(d["Confidence"]))
        break
                     

(* At first, I set Confidence to about 95, but I set it to 55 because I don't go over 60 in a dark room photo.)

Then, the photos that the dog was not detected are excluded from S3.

RekogDogCamera.py


        else:
            s3_client.delete_object(Bucket=bucket_name, Key=strfile) 

I will do it. KIMG1858 (1).jpg

$ python3 RekogDogCamera.py --- Opening /dev/video0... Trying source module v4l2... /dev/video0 opened. No input was specified, using the first. Adjusting resolution from 384x288 to 352x288. --- Capturing frame... Captured frame in 0.00 seconds. --- Processing captured image... Disabling banner. Writing JPEG image to '/home/pi/myphoto/20170103-00:08:15.jpg'. Canine found: 71.327880859375 20170102-23-57-58.jpg

I was able to take it!


The final file looks like this.

RekoDogCamera.py


import time
from time import gmtime,strftime
import RPi.GPIO as GPIO
import json
import os
import boto3

INTERVAL = 3
SLEEPTIME = 5
SENSOR_PIN = 25
#Specify the bucket to use
bucket_name = 'mybucket'

GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)

st = time.time()-INTERVAL
s3_client = boto3.client('s3')
while True:
         #If there is data inflow from the dog feeling sensor and the time interval is more than the specified time ...
        if( GPIO.input(SENSOR_PIN) == GPIO.HIGH ) and (st + INTERVAL < time.time() ):
                st = time.time()
                strf = strftime("%Y%m%d-%H:%M:%S")
                strfile = strf + '.jpg'
                #take a photo
                os.system('fswebcam --no-banner /home/pi/myphoto/'+ strfile)
                #Move the photos you took to your S3 bucket.
                s3_client.upload_file('/home/pi/myphoto/' + strfile ,bucket_name, strfile)
                #Ask Rekognition if there is a dog in the picture.
                result = os.system("aws rekognition detect-labels --image \'{\"S3Object\":{\"Bucket\":\""+bucket_name+"\", \"Name\":\""+ strfile +"\"}}\' --region us-east-1 --profile default > temp.json")
                #If there is a dog in the Json data returned from Rekognition ...
                with open('temp.json') as json_data:
                    data = json.load(json_data)
                    for d in data["Labels"]:
                        if (d["Name"] == "Canine" and d["Confidence"] > 55.0):
                                #There was a dog! And Confident
                                print ("Canine found: " + str(d["Confidence"]))
                                break
                        else:
                                
                                #If not, it will be mercilessly deleted.
                s3_client.delete_object(Bucket=bucket_name, Key=strfile)

        time.sleep(SLEEPTIME)
# Clean Up
GPIO.cleanup()
os.system('rm /home/pi/myphoto/* temp.json')

Recommended Posts

Pet monitoring with Rekognition and Raspberry pi
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Raspberry Pi system monitoring
MQTT RC car with Arduino and Raspberry Pi
Get temperature and humidity with DHT11 and Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Mutter plants with Raspberry Pi
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Easy IoT to start with Raspberry Pi and MESH
Detect mask wearing status with OpenCV and Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Easy introduction to home hack with Raspberry Pi and discord.py
Create a web surveillance camera with Raspberry Pi and OpenCV
Python beginner opens and closes interlocking camera with Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
I tried connecting Raspberry Pi and conect + with Web API
Production of temperature control system with Raspberry Pi and ESP32 (1)
Measure and compare temperature with Raspberry Pi and automatically generate graph
[Raspberry Pi] Stepping motor control with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
MQTT on Raspberry Pi and Mac
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
Graph display of household power consumption with 3GPI and Raspberry Pi
Measure SIM signal strength with Raspberry Pi
[Raspberry Pi] Add a thermometer and a hygrometer
Hello World with Raspberry Pi + Minecraft Pi Edition
Build a Tensorflow environment with Raspberry Pi [2020]
Get BITCOIN LTP information with Raspberry PI
Try fishing for smelt with Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Improved motion sensor made with Raspberry Pi
Try Object detection with Raspberry Pi 4 + Coral
Traffic monitoring with Kibana, ElasticSearch and Python
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Infer Custom Vision model with Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
Inkbird IBS-TH1 value logged with Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
RabbitMQ message notification app in Python with Growl ~ with Raspberry Pi and Julius ~
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
Simple VPN construction of IPsec gateway with Ubuntu 20.04 and Raspberry Pi ―― 1. StrongSwan introduced
Raspberry Pi 3 x Julius (reading file and grammar file)