Zombie Detecting Smart Security Owl (Deep Learning)
by t3chflicks in Circuits > Raspberry Pi
5580 Views, 71 Favorites, 0 Comments
Zombie Detecting Smart Security Owl (Deep Learning)
Hi everyone, welcome to T3chFlicks! In this Halloween tutorial, we’ll be showing you how we put a super spooky twist on a mundane household classic: the security camera.
How?! We’ve made a night vision owl which uses image processing to track people. Oh, and it hoots, just like the real thing!
We’ve been super excited about this project and we’ve been waiting to do it ever since the new Raspberry Pi 4 dropped. It’s got 4GB RAM, which opens the door to loads of really exciting possibilities, including doing some image processing with deep learning models in real time.
If you want to keep an eye out for approaching zombies on Halloween, or just check your garden the rest of the year round, this is the one for you. Security doesn’t have to be boring to be effective!
Supplies
Step 1: Decapitate
a. Pull the head off the owl (sometimes you just have to be brutal) by pulling hard on its head where it attaches to the spring.
b. The owl’s head connects to the body by a cylinder which sits on top of a large spring. Remove this cylinder by taking out the screw.
c. The cylinder you just removed is made of two parts, a plastic cup and a bearing which sits inside it. Remove the bearing from the cylinder using a screwdriver (or similar tool).
d. Using the screw which connected the cylinder to the spring, attach the servo to the cylinder.
e. Remove the spring by unscrewing the three screws that secure it to the body.
f. Make a hole in the top of the owl’s body which is large enough to fit some wires and the camera cable. We used an inelegant combination of a drill and a screwdriver to do this.
Step 2: Add Smart
a. 3D print the camera case and paint it to match the owl - we used some cheap acrylic paints. Painting isn’t a vital step, but it does dramatically improve the overall look!
b. With the owl’s head upside down, screw the top of the camera case into the inside of its head, where the beak protrudes.
c. Put the camera into the case and connect the camera cable.
d. Glue the servo to the top panel of the spring.
e. Connect long wires to the servo pins (5V, Gnd, signal)
f. Feed the camera cable and wires for the servo through the spring and through the hole you made in the top of the body so they are inside the owl’s hollow body.
Step 3: Fill Her Up
a. Remove the plug from the bottom of the owl and increase the size of this hole by cutting the plastic. Continue increasing the size until the Raspberry Pi and speaker can fit through into the body of the owl.
b. Once the hole is big enough for all the components to fit inside, pull the camera cable which you fed through the top of the owl out of the base and plug it into the Raspberry Pi.
c. Similarly, pull the servo wires through and plug them into the Raspberry Pi:
- +5v on servo => +5V on Pi
- Gnd servo => gnd Pi
- Signal servo => pin 12 Pi
d. Plug the USB speaker into the Pi.
e. Insert the SD card into the Pi.
f. Power Pi using portable power supply.
g. Insert the Pi, power supply and speaker into owl through the hole in the base.
Step 4: Setup the Pi
ALL THE CODE CAN BE FOUND AT https://github.com/sk-t3ch/cctv-owl !
a. Download Raspian and upload it to your SD card using Balena Etcher.
b. To access your pi remotely
- Add a file called ssh to your boot sd card
- Add a file called wpa_supplicant.conf and put your wifi credentials in
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="MySSID" psk="MyPassword" }
c. Insert the SD card in the pi and try an access via ssh.
Step 5: Moving the Head
Code tutorial for moving the head (controlling a servo with a raspberry pi) https://gitlab.com/t3chflicks/cctv-owl/tree/master/tutorials/1_move_the_head
To control a servo running on the Pi we are going to create script that controls the GPIO pins which the servo is connected to.
a. Connect the servo to the Pi:
- +5v on servo => +5V on Pi
- Gnd servo => gnd on Pi
- Signal servo => pin 12 on Pi
b. You must first set up the gpio pins to use PWM on the signal pin of the servo.
c. Then, it is as simple as selecting the duty cycle (explained here) of the signal pin to move the servo from 90 degrees with a duty cycle of 7.5 to 0 degrees when the duty cycle is 2.5 and to 180 degrees with a duty cycle of 12.5
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 50) p.start(7.5) try: while True: p.ChangeDutyCycle(7.5) # 90 degrees time.sleep(1) p.ChangeDutyCycle(2.5) # 0 degrees time.sleep(1) p.ChangeDutyCycle(12.5) # 180 degrees time.sleep(1) except KeyboardInterrupt: p.stop() GPIO.cleanup()
Step 6: Making It Hoot
Code tutorial for making the owl hoot (playing audio with a raspberry pi) https://gitlab.com/t3chflicks/cctv-owl/tree/master/tutorials/2_hoot
a. Plug in the USB speaker.
b. Download a sound - we chose a spooky hoot.
c. Play the sound by running this command: omxplayer -o alsa:hw:1,0 owl_sound.mp3
[d. If this doesn’t work, check what output your Pi is using and at what volume by using the command alsamixer - you will be greeted with the mixer screen where you can change the volume and select your media device. To increase the volume of your sound, do the command like this omxplayer -o alsa:hw:1,0 owl_sound.mp3 --vol 500 To play this sound using Python, have a look at our test script.]
import subprocess command = "omxplayer -o alsa:hw:1,0 owl_sound.mp3 --vol 500" player = subprocess.Popen(command.split(' '), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
Downloads
Step 7: Stream the Video From the Pi
Code tutorial creating a raspberry pi camera stream https://gitlab.com/t3chflicks/cctv-owl/tree/master/tutorials/3_stream_video
a. Run python app.py and view on your local network at http://raspberrypi.local:5000
b. This code was taken and slightly adapted from Miguel Grinberg https://blog.miguelgrinberg.com/post/flask-video-... he explains nicely how it’s done and his tutorials are great - deffo check him out! The basic concept is that we use threading and generators to improve the streaming speed.
Step 8: Body Detection
Code for body detection(ImageNetSSD on a video stream with raspberry pi) https://gitlab.com/t3chflicks/cctv-owl/tree/master/tutorials/4_object_detection
a. Since we're using the Raspberry Pi 4, we thought it was best to try out some deep learning models on it instead of the basic HaarCascade method we’ve been limited to so far.
b. We had a look at some of the pre-trained models out there, like YOLOv3 which looks super cool. YOLOv3 tiny weights, which would have been perfect for the Pi, but we couldn’t get it running :(
c. Instead, we opted for the MobileSSD model which we can run using openCVs DNN (deep neural net) module, as we learnt from this code: https://heartbeat.fritz.ai/real-time-object-detection-on-raspberry-pi-using-opencv-dnn-98827255fa60 and from the hero of image processing tutorials, Adrian Rosebrock: https://www.pyimagesearch.com/2017/09/11/object-detection-with-deep-learning-and-opencv/
d. However, as we are trying to stream this content and run models on every frame, this results in a laggy, fragmented video. We learnt again from Adrian Rosebrock https://www.pyimagesearch.com/2017/10/16/raspberry-pi-deep-learning-object-detection-with-opencv/ and used the Python multiprocessing module to put our images into queues where they can be processed without blocking the camera stream so heavily.
e. Try running the code yourself :) https://gitlab.com/t3chflicks/cctv-owl
Step 9: Sending Zombie Notifications
Code for sending a notification (python to phone) https://gitlab.com/t3chflicks/cctv-owl/tree/master/tutorials/5_send_notifications
a. We decided to use https://pushed.co notification service.
b. You can get a free account and download the app and really quickly get set up making mobile notifications. We created the notifications using a python script like this.
import requests payload = { "app_key": "APP_KEY", "app_secret": "APP_SECRET", "target_type": "app", "content": "Owl has detected a zombie." } r = requests.post("https://api.pushed.co/1/push", data=payload)
It's super simple and you can customise your notification name!
What a Hoot!
We hope you enjoyed our Smart Security Owl project! This has been a super fun make and I feel a whole lot safer knowing my house is being guarded by our trusty owl.
If you think this would be a fabulous Halloween addition to your smart home, please vote for us in the Instructables Halloween contest and as usual, please remember to like, comment and subscribe!
Check out T3chFlicks.org for more tech-focused educational content (YouTube, Instagram, Facebook, Twitter).