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.
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.
[* 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.
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()
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.
I tried the Rekognition demo. This time I'm interested in Object and scene detection.
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.
$ 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
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