How to do Raspberry Pi Zero Surveillance with Ruby

in #ruby6 years ago (edited)

I've had my Raspberry Pi Zero for over a month, all powered and connected with its camera ready to go, but I had a problem positioning the camera because the ribbon cable I had was too short. So I finally got the extended ribbon cable and mounted it properly today. Prior to this, I was able to capture stills and also do broadcasts on YouTube (private feed). This was helpful to get it focused and pointing where I wanted it.

But what I really wanted was a way to capture an image and compare it to the previous image so that it could send it off to Dropbox only when there was a significant change. Here are the scripts I'm using:

Gemfile

source 'https://rubygems.org'

gem 'phashion'

camera.sh

#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H%M%S")

raspistill --rotation 90 -q 10 -w 640 -h 480 -o $HOME/stills/$DATE.jpg
echo $DATE

find_non_dupe.rb

require 'rubygems'
require 'bundler/setup'

Bundler.require

now = Time.now
one_minute_ago = now - 60
pattern1 = one_minute_ago.strftime('%Y-%m-%d_%H%M')
pattern2 = now.strftime('%Y-%m-%d_%H%M')
files = Dir.glob("~/stills/#{pattern1}*.jpg")
files += Dir.glob("~/stills/#{pattern2}*.jpg")
last_img = nil
unique_dir = '~/unique'

files.each do |f|
  if File.exist?(File.join(unique_dir, File.basename(f)))
    puts "Skipping: #{f}"
    next
  end

  this_img = Phashion::Image.new f

  if !!last_img &&
    if this_img.duplicate? last_img
      puts "Duplicate: #{f}"
    else
      puts "Non duplicate: #{f}"
      FileUtils.cp f, unique_dir
    end
  end

  last_img = this_img
end

Then I use Dropbox-Uploader to send only the changes. Here's what it can capture and upload to Dropbox:


Update: Here's how I mounted it.

2018-01-28 10.04.03.jpg

Sort:  

Gyah, this reminds me. I really need to quit letting my pi3 rot away on a shelf. I AM ASHAMED OF MYSELF. It just sits there with retropie slapped on it, wasting away.

At least power it up and run mosh + tmux so you have shell sessions with years of uptime.

banjo:~ $ uptime
 20:59:49 up 100 days,  1:09,  3 users,  load average: 0.44, 0.35, 0.37

The only reason that pi reboots is because I crash it from running too many blockchain streams. :D

hah dont worry matt, your raspberry pi is just lucky to get to know you

That rasberry pi had an opportunity to hold a few million bitcoin LOL

You once had millions of BTC man, its so col, you could have been a billionaire today :D hah i love it! imagine!

There are many tools out there, just takes a bit of looking. Someone might be wondering how this relates to Raspberry Pi,That's how this relates to Pi.

Good point. I didn't really explain how this was specific to the raspberry pi.

There are a bunch of off-the-shelf solutions, but they are overkill if you're looking to do it yourself. Many of the "turn-key" products send an HD stream with audio to a server, which utilizes a bunch of bandwidth. I don't really like that idea, even if my internet wasn't metered.

These scripts are optimized for the pi zero hardware. Instead of capturing a 4MB image by default, which takes 30 seconds to compare, this solution captures a 150k image. That way I can get it to compare images once every 3 seconds or so.

You just had to post details. I have wanted a camera (or two) around here for ages. We had a shooting about 500 yards from our back door a few months ago while my son was standing outside. You could say the neighborhood is not ideal.

I also like the idea of using one as an ad blocker using pi-hole . Tom was telling me he runs it with great success. Incidentally, he was here to help my brother move...he flew over from England....to help him move. Crazy Brit.

You really can get away with mounting one of these to the side of a wall:

But then you have to wire it with a 5 volt power supply somehow. I got away with just mounting it inside a vent so that the actual pi is inside the garage and the camera its just clipped to the housing.

You can go overboard with a case that costs more than the pi itself:

I kinda like this guy's solution:

I like the last idea. You could probably make an argument that it would be more efficacious as a deterrent (assuming that is part of your end game) if it is clearly visible.

Maybe add a sign that says, "You are on camera. You can smile...or not, I do not give a %&*$."

If I am honest, I am so paranoid I would probably have fifteen of these stupid things inside and out if I was not aware of my neurosis.

You need to figure out how to use a battery strapped to this thing to get an idea of how long it would run before it had to be changed. Make it so.

I think a 10000mah battery will run it for a couple days.

Nicee--- have my vests and your bests!

There are numerous devices out there, just takes a touch of looking. Somebody may consider how this identifies with Raspberry Pi,That's the means by which this identifies with Pi. @inertia

You have simply changed my comment... Lol....
You creating me funny and lough....

tenor.gif

Actually, it's pretty obvious you're both spinners.

Hmm. It looks like another reason to buy a raspberry

Thanks for sharing sir

amazing news,,thanks for shareing

Amazing device Raspberry Pi Zero, I think.

Nicely done

@inertia love the high tech mounting system :)

Hi! Cool project, I love to use Pi Zero and I am a big fan of RPi. I also use it in my home automation system and alarm. One of my Pi Zero works quite nice and smoothly as element of home system with attached motion sensor, temp sensor and the camera. It easily handles one room :) Great job! I'll post some of my project soon. You should post more of your works, I bet you have something :)

thanx for that. Having a Raspi with the wide range cam but just played around with the v4l stuff and connected using VLC

btw. there are quite new infrared-camera (supplier: digikeys) for good price. Unfortunately they need to be connected using i2c (command interface) as well as SPI (video data).
I did not connected it to an Raspberry yet but an homemade Atmel AT32UC3A3256 Controller-Board with color LCD. Might be nice project to make it work for Raspies

WOw so you basically programmed your own trail cam. cool.

Thanks for open sourcing this.

Hello good day, I wanted to ask whether this same process can be applied in the raspberry with Kali Linux, grateful advance

Have you ever heard of Raspberry Pi? It's a low-cost micro-computer that is able to run Linux and has endless extension possibilities. It cost only about US$35 and opens up endless possibilities of what you can build with it.

There are many tools out there, just takes a bit of looking. Some of you might be wondering how this relates to Raspberry Pi, well I did this installation on my own Pi. That's how this relates to Pi and I though it was necessary to post how I did install Metasploit on my Pi. I would love comments down below and please give me some loving, but alas hack on my fellow hackers. :)