SquirrelCam

Forums:

Squirrels! These ratty little buggers are really starting to aggravate me. Not only have they destroyed my boat, now they are starting to leave black walnut shells all over my deck. Acorns I can handle, but if you have ever seen a black walnut, they are coated in a pungent and high staining, yellow oil in their casings. They then turn to a dark black pigment as they rot which will also leave stain marks all over the deck. Nasty. The concept is somewhat simple.. spot a squirrel.. make him leave (temporarily or from this earth plane). This will be accomplished by setting up "squirrel recognition" using openCV on a raspberry pi with camera that is placed on the deck. The pi I am using (Raspberry Pi 3b) also has a gpio controlled relay, so I may make it mechanically do something, but for right now I am thinking audio. A quick 3d printed holder for my pellet gun would not be a far stretch though. It starts out with the standard, non-gui, buster image. I have followed the steps elsewhere in this forum topic to add OpenCV for the recognition. This however will be squirrel specific and not really pointed where someone could trigger it. The squirrel is leaving the shells on the far side of the deck which points out to a field so projectiles would not be an issue. There is also an outlet by this place I will put it, but I am still not sure if I will use an outlet or a battery pack, which on my pi demo for picup, I could stretch about 8 hours of use before it went to sleep. I have a 32G card in the pi and hoping that will suffice, but I also have a 64 and 128 G card if I need to step up the images. Haar Creation Ref:Haar Classifier I need to create Haar classifiers so that i can recognize the little buggers. I am using the link above. The thought is to create the negative images by setting the cam on the deck and loop through a set of pics without any squirrelage.

Negative Images

This requires: sudo apt-get install python3-picamera Using this simple script to create the "negative" images. I have updated it so that you can start where you left off by changing the variable "i". So if you stopped at 515 (in this case) then you will start at 516 for the next file name. You can also adjust the range(200) to how many after 515 you want to continue with. In this case it would end up with 716 images. Feel free to change them to global vars like "startat" and "numberofpics" or something similar. pic.py
#!/usr/bin/python from time import sleep import os # required for popen from picamera import PiCamera camera = PiCamera() camera.resolution = (500, 375) i=516 for x in range(200): camera.capture("{0}.jpg".format(i)) i=i+1 sleep(2) print("popped {0}".format(i))
It is recommended to get around 700 images. For this, I will move the python script pic.py (above) into the neg directory I created under opencv directory. This will take a little while, but I will move the camera around a bit so it gets shots from a wide array of angles. It is also an option to just do a video recording and then export the frames. This does however not give me the option to change the image size, which I am going to start out at 640x480. I may have to increase the resolution, but since it is only a pi, I am going to try lower first. I believe they will be about 300k per pic. It says to use images that are similar to the actual images but without the subject, so I am hoping that since this will be the actual background it will be able to find mr. squirrel. Output the negative image index file. Because this was a start/stop operation while I moved the camera around and I also know that these negative images are all .jpg files, I did not want to write to a file. Per instractions in the reference, I will simply create a negative image index file using find: find "*.jpg" > negatives.txt This should output a file called negatives.txt and the data should look similar to: ./48.jpg ./6.jpg ./138.jpg ./252.jpg ./308.jpg ./442.jpg
Image Samples Image 101 Image 501
I have also stopped at 515 at this point in time. Since it is a cloudy day, I will need to create some image negatives which contain full sun. This will change the pixels substantially on the comparisons. Possibly even some dusk, morning pics as well.

Positive Images

Next will be generating the actual squirrel images from googles. This task is accomplished by using wget. (replacing the site with images you have googled) *note.. the site must have valid hrefs to the pics and not some script generated image display. A simple shell script called getpics.sh
wget -nd -H -p -A jpg,jpeg,png,gif -e robots=off https://urlofsitewithpics.com
For my squirrel first start I used https://pixabay.com/images/search/squirrel/ as a test. Please be sure to support their website if you use them and give credit for their great image collection. I will let you figure out how to step through the image pages. :) This gave me approximate 33MB worth of what I am hoping are squirrelshots. resizing images - i500.py i500.py is a small python script I had written some time back to resize images to 500x? width. This way I did not have to upload gigantic images from my cell phone to the 247coding.com web pages. i500.py source pictickle.py So the next thing I need to do is to loop through the entire directory of files that were downloaded, rename them to a common naming format (by preference) and then run i500.py against them to reduce the image size to a common size format. PicTickle will accomplish this. I did however have to add some sleep() statements or it seemed to progress faster than the pi could keep up and lost some "tickles". You will see some STDOUT errors, but I believe this is the response from i500 getting dumped and is OK, it will still complete. 300+ pics will take a few minutes to complete, but in the end you should only have a new list of images 1_500.jpg, 2_500.jpg, etc. Each file processed should spit out a result that looks like this: **Warning: Before you "tickle" anything, it is advised to make a backup copy of all of your images using zip mypics.zip *.png *.jpg *.gif in the directory you are in. If you do not have zip, then do an apt-get install zip unzip to get them. (unzip may already be installed on base pi image) ** note that it only checks for image file assoc and will overwrite if you already have a file by the numeric name (i.e. 0.jpg, 1.jpg). You can adjust this to add a prefix if you'd like, but I am working in a specific directory, so it does not matter. pictickle.py script Source Code After running pictickle your directory of images should look akin to: You should also contain a txt file called picindex.txt. This will be the combined list of all the files in the directory. (I may need to add a ./ in front of the file names but will see if it works as is). This should skip the part of using "find *.jpg" in the url instructions listed above, pictickle will create the list for you.

Next up, putting it all together.

more to come soon...