Monday, July 30, 2012

Creating a Sonic Screwdriver with an Arduino Nano, Part 1

I was pretty excited to hear about the release of a Sonic Screwdriver remote. But after watching the YouTube demo, it seems kind of lame. The appeal of the Sonic Screwdriver, used by the good Doctor, comes from its universal applicability to help the Doctor get out of sticky situations or rid general annoyances. A programmable remote just doesn't cut it.

For those in the US who have never caught an old episode of Doctor Who at 5:30 AM in the morning on PBS or watch the newer episodes on Amazon Prime, a Sonic Screwdriver is akin to MacGyver's swiss army knife, but only more futuristic - the BBC prop makers added an LED to show that it is from the future.



My disappointment in the Sonic Screwdriver remote (available on ThinkGeek) has inspired me to design a Sonic Screwdriver that is more true to fiction. I just can't see the Doctor sitting somewhere waving his screwdriver infront of an old Panasonic remote. In this post I will go my plans to integrate a TV-B-Gone into a Sonic Screwdriver. In future posts, I may show how to integrate other functions such as 128 kHz RFID technology (to open my apartment or work doors) and possibly integrating 13.56 MHz MIFARE technology (in DC we use SmarTrip for public transportation).

Friday, July 27, 2012

Face and Eye Tracking Fun!

I recently discovered the openframeworks toolkit. It is an open source C++ toolkit that wraps together many commonly used libraries for simple and fast experimentation. I absolutely love it!

When I shared code in the past, it took quite a bit of time to document what libraries, which version, and sometimes (even with C++) which operating system the new user must install for the code to compile properly. With openframeworks, that annoyance is a thing of the past.


For this post I decided to make a quick and fun example adopted from one of openframeworks' provided examples on detecting faces in pictures. The example can be found under the folder: examples/addons/opencvHaarFinderExample.

I decided to extend their example to detect faces from a live webcam or a quicktime movie. I was amazed at how few lines of code it took. To get started, download the appropriate toolkit for your IDE, here. Then copy the folder opencvHaarFinderExample in examples/addons/ to apps/myApps/. Then replace the code in testApp.cpp with the following:
#include "testApp.h" //-------------------------------------------------------------- void testApp::setup(){ camWidth = 480; camHeight = 360; //uncomment and comment lines below to test different detectors finder.setup("haarcascade_frontalface_default.xml"); //finder.setup("haarcascade_frontalface_alt.xml"); //finder.setup("haarcascade_frontalface_alt2.xml"); //finder.setup("haarcascade_eyeglasses.xml"); //finder.setup("haarcascade_eye.xml"); #ifdef _USE_LIVE_VIDEO VideoGrabber.setVerbose(true); VideoGrabber.initGrabber(camWidth,camHeight,OF_IMAGE_COLOR); colorImg.allocate(camWidth,camHeight,OF_IMAGE_COLOR); #else VideoPlayer.loadMovie("ThemeFromShaft.mov"); VideoPlayer.play(); #endif } //-------------------------------------------------------------- void testApp::update(){ ofBackground(100,100,100); bool newFrame = false; #ifdef _USE_LIVE_VIDEO VideoGrabber.grabFrame(); newFrame = VideoGrabber.isFrameNew(); #else VideoPlayer.idleMovie(); newFrame = VideoPlayer.isFrameNew(); #endif if (newFrame){ #ifdef _USE_LIVE_VIDEO colorImg.setFromPixels(VideoGrabber.getPixels(), camWidth, camHeight, OF_IMAGE_COLOR); #else colorImg.setFromPixels(VideoPlayer.getPixels(), camWidth, camHeight, OF_IMAGE_COLOR); #endif finder.findHaarObjects(colorImg); } } //-------------------------------------------------------------- void testApp::draw(){ ofSetHexColor(0xffffff); colorImg.draw(0,0); ofNoFill(); for(int i = 0; i < finder.blobs.size(); i++) { ofRectangle dim = finder.blobs[i].boundingRect; ofRect(dim.x, dim.y, dim.width, dim.height); } ofSetHexColor(0xffffff); char reportStr[1024]; sprintf(reportStr, "Number Detected: %lu fps: %f", finder.blobs.size(), ofGetFrameRate()); ofDrawBitmapString(reportStr, 20, 20+camHeight); } //-------------------------------------------------------------- void testApp::keyPressed (int key){ switch (key){ case ' ': break; } } //-------------------------------------------------------------- void testApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::windowResized(int w, int h){ } Then replace the code in testApp.h with the following:
#ifndef _TEST_APP #define _TEST_APP #include "ofMain.h" #include "ofxCvHaarFinder.h" #define _USE_LIVE_VIDEO //comment to use video, uncomment to use webcam class testApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed (int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); int camWidth, camHeight; #ifdef _USE_LIVE_VIDEO ofVideoGrabber VideoGrabber; #else ofVideoPlayer VideoPlayer; #endif ofxCvHaarFinder finder; ofImage colorImg; }; #endif For those mac users out there, just download this, open /apps/myApps/HeadTracking/TrackingExample.xcodeproj in Xcode (3.2 and above) and hit run!

Wednesday, July 11, 2012

Historical Minute-by-Minute Stock Prices in MATLAB

This is an extension of my first post on this blog. My first post went over how to gather the minute to minute stock price data from Google Finance using Mathematica. Using the same general programming structure, I've created a function to do this in MATLAB. 

The IntraDayStockData function can be obtained hereThis function takes necessary inputs of a stock symbol and the name of the exchange the stock is traded in. This function also takes optional inputs of quote frequency (in seconds) and number of previous trading days. By default, the optional inputs are set for 1 minute quote intervals and 15 previous trading days. The output of this function are the time, the time in string format, trade volume, highest prices, lowest prices, and closing prices. Here is a quick example for getting the minute by minute quotes of the latest trading day (today) for JP Morgan Chase:

jpm = IntraDayStockData('JPM','NYSE','60','1d'); plot(jpm.date,jpm.close,'b-'); hold on; plot(jpm.date,jpm.high,'r-'); hold on; plot(jpm.date,jpm.low,'g-'); datetick('x',16);

Tuesday, July 3, 2012

Where are the Homicides in Chicago?

Recently, CNN did a report on the high number of homicides that is plaguing Chicago. One of the sure fire solutions suggested was to place more police in the areas where these killings occur. It got me wondering where these homicides actually occur.

Using the code from my previous post, I called the crime API that the city of Chicago provides and plotted out all the reported homicides between August 1st, 2011 and May 31st, 2012. There were 403 in total.


Now where did these incidents occur?



Initially it looks as if the homicides are wide spread throughout the city. However, if we do a breakdown by neighborhood it becomes obvious that some are much worse off than others. Below is a table of each Chicago neighborhood and its associated number of homicides between August 1st, 2011 and May 31st, 2012. I've also provided a more convenient graphical representation of this table. The Mathematica code and data files I used for this post can be found here.