Object measurement with webcam

Shut up and show me the demo.

I have been experimenting with using a webcam to measure an objects size, width and height.

It works by comparing the size of a user defined box to the size of a magnetic strip (which is also user defined). I tested it against a ruler and it seems accurate but obviously the larger the object the more accurate it becomes. Right now it’s setup to measure the size of a ring finger.

As this is a weekend project I published the source up on github. I haven’t decided on a license yet so for now don’t adopt it in any commercial applications but do feel free to contact me to chat about funding further development.

I use getUserMedia / Canvas to do most of the interaction between the webcam and the browser and I’m using PaperJS for drawing the boxes onto the canvas. The actual math is really simple

// Takes a scale value and tries to figure out the mm diameter of each ring
function scaleToRingSize(widthPx){
  return (85.72500 / stripWidth * widthPx).toFixed(2); // 85.72500 being the width of a magnetic strip in mm
}

The fingers.js also contains code for converting ring sizes in mm to the nearest smaller ring size. IE if your ring size is 22mm it will give you a ring size in US/EU that is 22.2mm instead of 20mm.

I’m researching using Tracking.Js with some custom OpenCV maps to do better object detection, I know this is possible but right now the algos are a bit buggy and I’d need a lot of positive images to create a good training set. If I could do some object tracking in theory I could auto detect the strip / target object size and automate / realtime the whole measurement process and in theory I could do ask a server to do real time object detection, but that’s for another day..

The measurement app might not quite fit on your page, this is because it’s 800px x 600px with absolute positioning, I guess I could make it use relative positioning but right now I’m not interested.

The tool will work in all browsers that support getUserMedia which is IE11, FF 17, Chjrome 21, Safari 7 and Opera 12, iOS Safari, BBB 10, Opera Mobile 12. For more browser support queries see getUserMedia browser support. Obviously if you are still using IE then you have probably made many other poor life decisions and you are beyond my help. Obviously because I’m using JS / CSS the app will work on a mobile and desktop webcam so you can in theory measure objects whilst on the move.

I had to worry about the overheads of the large javascript fabric.js library so I decided I didn’t want the additional overheads of jQuery. It’s entirely written in JS (with obv some CSS/HTML) throw into the mix. I could probably refactor it down to just JS without too much headache and this would give site admins the ability to do something like:

<script src="measurer.js"></script>
var measure = new Measurer();
measure.create("targetDiv"); // can do it like this because getUserMedia is limited to one instance of browser/OS.
measure.on('success', function(measurement){
  alert("Got a measurement of", measurement);
  measure.destroy();
});

You will need a card with a magnetic strip on the back, ideally a credit card as these are guaranteed to be a universal size.

Final thoughts: None of the images are saved to a server but as you know these images are being passed through the Internet which means your ISP is sending a copy of them to the NSA. I strongly recommend against using this tool to measure any part of your anatomy.

My first Cordova App



Well it’s been an interesting 30 or so days. Among other things I have been attempting to build a mobile app with Apache Cordova, an open source tool for writing HTML/JS/CSS and deploying to most popular mobile operating systems.

At first development looked like they would take a few hours, mostly because I was using native plugins and building using Phonegap build, a free(for open source) build service provided by Adobe. Yep.. Adobe being the good guys, impressive 🙂 I couldn’t have been more wrong…

PhoneGap Vs Cordova

PhoneGap and Cordova are not the same thing, I’m going to talk about Cordova here. The reason being is that Phonegap Build could not be used as it didn’t have support for the phonegap-nfc plugin. If I was doing a project that didn’t depend on phonegap-nfc development would have been a lot easier and quicker.

So the first takeaway is if you are building a simple app that will use phonegap build supported plugins then use phonegap build, you will have way more fun.

Cordova is horribly fragmented

The plugin system is broken, badly. I had some majorly catastrophic few weeks just trying to bring the phonegap-nfc plugin into my project using cordova-cli. I “solved” this by getting someone else to build using cordova-cli 2.7.0. Various things were to blame here but eventually I settled on using Eclipse (an IDE I really dislike) and things got relatively stable to the point where I was able to use a modified BarcodeScanner branch to introduce new plugin functionality.

I want to be clear about how badly broken things are here though.. The plugin spec/documentation is near non existent, in fact only Adobe internal guys have it at the point of me writing this. Thankfully the Adobe guys took a little time out to help me get things working.. Good guy Adobe!

Debug tools

Debugging Cordova is horrible assuming you actually get past the 50 or so barriers to installation most of the errors messages are ambiguous, that’s if you get one.. Expect to see a lot of “Error is Null” or “File cannot be found” Exception errors.. To get around these you will basically need to do lots of going forward/backward and iterating in small steps, testing / compiling each time you make a change..

I ended up using Weinre for improving styling and issuing debug commands and I used logcat/eclipse console for compile / JS errors. Weinre has a weird issue where if an error happens it blocks future JS execution so never actually reports the error back to your remote console, pretty insane and creates a fair amount of pain until you are used to it. logcat’s filtering functionality is really broken and as my crappy LG phone throws an error every few MS that ends up spamming my logcat which makes spotting application errors pretty difficult.

An amazing community

The Cordova community and guys on IRC were amazing, really pulling me through some really challenging times.

Starting to hate Eclipse less

Using eclipse is horrible but it gets less bad once you get used to it. If you are familiar, using git behind your file structure but .gitignoring your bin folders will save you a fair amount of pointless data. In general git will save you a lot of pain though, see what I mentioned above about doing lots of small iterations and testing each one..

Mobile Dev is slow until it works

Until you have Cordova setup and your plugins working correctly things will be an uphill struggle but once you get everything setup you can quickly use your favorite JS development tools to quickly integrate new modules/functionality and that’s really when things start to make a lot of sense with cordova. I actually found myself enjoying the last 2 days of this project (yep ~28 days getting a working Android platform and 2 days actually building the app), I’m not looking forward to doing other platforms though..

Cordova in the future

Cordova 3 is going to be exciting, the move away from plugins and towards features should ensure plugin developers actively maintain their code meaning that more plugin should install with very little friction. I’m certainly going to be sticking with Cordova and will use it again in future projects. I think July/August time we can see plugin support stabilizing and that’s when things should get really interesting 🙂



Your system does not meet the requirements to create android projects: undefined

Cordova platform add android errors with

Your system does not meet the requirements to create android projects: undefined

Did you remember to

chown -R YOURUSERNAMEHERE /usr/local/lib/node_modules/cordova

You can check with..

ls -lsh /usr/local/lib/node_modules/cordova

You should see your username, not root..

If so..
Let’s do a quick update of Cordova:

cordova -v
# review the version you are at 
npm update cordova
cordova -v
# review the new version

Let’s make sure android works and the path is set to the android binary:

android

This brings up the Android SDK Manager.

Test an emulator by hitting up:
Tools > Manage AVDS, creating an image then hit Start. If your Android emulator works then continue, else something is up with your ADT/SDK deployment

Open up a CLI, type:

export | grep PATH

Make sure your Android SDK platform-tools and tools folders are available in the PATH variable.

Going okay? Let’s update the Android SDK

android update sdk

Now try visit your cordova app folder and try

cordova platform add android

Still no luck?
Try using the master2 branch of the cordova-cli repo *Warning, this is dangerous.

cd ~
git://github.com/apache/cordova-cli.git
cd ~/cordova-cli
git checkout master2
sudo npm install -g
exit
cordova -v

The above should output a new cordova version #

Post a comment! 🙂

Monitoring a Wind Turbine with a Raspberry Pi

saphonian_turbine_bladeless

As a weekend project I wanted to receive an email if my wind turbine wasn’t generating power when it should. I didn’t want to spend loads of money on fancy new equipment so I hacked it together mostly with equipment I had lying about.

Requirements

– Wind Turbine or Solar Panel (obviously)
– Bridge rectifier (converts the AC from the turbine to DC)
– Some sort of voltage regulator (10-24v –> 12v) (or to 5v and skip below)
– Car cigarette lighter mobile phone charger (£5)
– Raspberry Pi (£35)
– Delta Sigma ADC (£25)
– USB Cable (£1)
– 1 Hour free
– Met Office API Key (free — UK Only)

Steps

– Wire up the turbine and bridge rectifier
– Pop the bridge rectifier into the voltage regulator
– Put the regulated voltage through the car voltage adapter, this will give you 5v out
– Wire the 5v from the turbine into channel 0 on the Delta Sigma
– Pop the ADC into the Pi, follow this guide for setting up the software.

How it works

– The Pi asks the Met Office what the current wind speed is.
– If the wind speed is above a threshold then the Pi checks the turbine
– If the turbine isn’t generating 5v then email me.

Next Steps

– Use Machine Learning to find a better balance on announcements
– Rate limit announcements.
– Measure integer of turbine output instead of boolean state.
– Wire the Owl energy monitor up to the AC output of the grid tie inverter.
– Store value of state in something like Graphite or using Nagios for announcements

The conection to the server was unsuccessful

The application Error

The conection to the server was unsuccessful (file://android_asset/www/index.html)

Was caused by a script being inaccessible during the page load..

For me changing:

<script src="http://oldplace:8080/target/target-script-min.js"></script>

To

<script src="http://newplace:8080/target/target-script-min.js"></script>

Fixed it