Now that I have a Raspberry Pi that can take pictures and turn electrical sockets on and off, as well as a trained image classifier that knows my cats, it was time to stitch everything together.
The Lola Detector
The following steps were taken to construct a small program that would scan an image on demand and identify Lola (or not):
- Install TensorFlow on the Raspberry Pi as a Python library.
- Refactor the example image label Python script to start TensorFlow, load the model, and wait.
- Use Flask to create a URL that would kick off the image analysis function, and return a JSON object with the Lola/Maddie label probabilities.
- Keep this program running.
The code for the Lola Detector can be found in a Gist.
Please note, this is the first Python script I’ve ever written. Feedback (maybe) appreciated.
The Lola Feeder
Rather than use Python for the whole app (like a sane person), I opted for Node to do the rest of the stuff. A big, convoluted Node script does the following:
- Takes a picture with raspistill and saves it as rpicam.jpg.
- Sends an http request to the Lola Detector service, and waits for a response.
- Checks the response for a high Lola probability.
- If Lola is NOT found, go back to step 1.
- If Lola is found, send a signal via the FM transmitter to power on the cat feeder.
- Send a tweet with the recently taken picture to a hidden Twitter account.
- Send a message to IFTTT which sends me a push notification.
- Wait 60 seconds and send another signal to power off the feeder.
- Wait 90 minutes and go back to step 1.
Additionally, I have a small lamp attached to another RF power socket, and have a “cron job” that turns the lamp on for a few hours in the evening, and a few hours in the early morning.
Here’s the code for the RF thing. I also have a white noise machine, another lamp, and some Christmas lights hooked up to other RF outlets. Unfortunately, the Xmas lights outlet stopped working :(
To run and monitor both scripts, I use PM2. It’s awesome. You should use it, too.