Designing a Lab for ELEC 474

2 Weeks ago my supervisor asked if I would be interested in designing the 5th lab exercise for his 4th year course ELEC 474 Machine Vision to which I accepted. My task was very open ended all he wanted was a lab centered around Eigenface that would be appropriate for the students.

Designing the lab was an interesting experience on one monitor I was working on the program the students would eventually have to write and on the other monitor I had a LaTeX file of the lab handout.  It was tough to figure out how long to make the lab and what the right difficulty would be. I decided to split it into 2 main components - in the pre lab the students would prepare the face data and learn how to use cv::FileStorage while the actual Eigenface implementation would be done in the lab period.  I ended up coding both the pre-lab and the lab in their entirety and then removing the code from certain functions for the students to fill in.

The lab itself went quite well! As part of the pre-lab they had to code a function that would combine their face database with another classmates database and of course this process could be continued so they could all share data. This ended up working way better than I expected and some students who coded the function generically were able to use it for all sorts of handy data manipulation to quickly generate the test cases we wanted to see during marking. I think the students in particular really enjoyed visualizing the Eigenfaces and some of them really got into trying the different ColorMaps available in OpenCV - I think Hot and Bone produced the coolest results!

Eigenfaces using JET ColorMap

The one thing I might do differently would be the effort marks I put in the grading scheme. I put the effort marks in because I wanted the students to put some time into the “softer” part of the lab like choosing good and interesting faces to test on and labelling their data nicely etc. But in the end it just became tough to rationalize what grade a student was getting. In my teaching assistant positions I have noticed that when a grade is given for a demonstration with student present its a lot harder to give less than perfect grade as they will debate it with you but if its an assignment that is handed in students seem to accept non-perfect grades without saying anything, not sure why this is.

All in all it was an interesting experience that I would definitely do again. It was particularly interesting to see where the common pitfalls and misunderstandings were, gave me some more insight into teaching. I enjoyed designing the lab - I like teaching others especially when it is on a topic I like so much.

For anyone who is interested here are the lab materials!

lab5_prelab

lab5

Pre-Lab skeleton code:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>

#include
#include
#include

using namespace cv;
using namespace std;

Mat detectFace(const Mat &image, CascadeClassifier &faceDetector);
void resizeFace(Mat &face);
void addToDataSet(Mat &data, vector &labels, Mat &newData, vector &newLabels);

int main()
{
  // Set up face detector
  CascadeClassifier faceDetector;
  if(!faceDetector.load("lbpcascade_frontalface.xml"))
  {
    cerr << "ERROR: Could not load classifier cascade" << endl;
    return -1;
  }

  // fill these variable with your data set
  Mat samples;
  vector labels;

  return 0;
}

Mat detectFace(const Mat &image, CascadeClassifier &faceDetector)
{
  vector faces;
  faceDetector.detectMultiScale(image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30));

  if(faces.size() == 0)
  {
    cerr << "ERROR: No Faces found" << endl;     return Mat();   }   if(faces.size() > 1)
  {
    cerr << "ERROR: Multiple Faces Found" << endl;
    return Mat();
  }

  //Mat detected = image.clone();
  //for(unsigned int i = 0; i < faces.size(); i++)
  //{
  //    rectangle(detected, faces[i].tl(), faces[i].br(), Scalar(255,0,0));
  //}

  //imshow("faces detected", detected);
  //waitKey();

  return image(faces[0]).clone();
}

void resizeFace(Mat &face)
{
  // code here
}

void addToDataSet(Mat &samples, vector &labels, Mat &newSamples, vector &newLabels)
{
  // code here
}

Lab skeleton code

#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/highgui/highgui.hpp>

#include
#include
#include

using namespace cv;
using namespace std;

void addToDataSet(Mat &data, vector &labels, Mat &newData, vector &newLabels);
Mat norm_0_255(Mat src);
string recognizeFace(Mat query, Mat samples, vector labels);

int main()
{
  // Load your data and combine it with the data set of several of your peers using:
  // addToDataSet

  // Perform PCA
  // cv::PCA pca(....);

  // Visualize Mean
  Mat meanFace = pca.mean;
  // normalize and reshape mean
  imshow("meanFace", meanFace);
  waitKey();

  // Visualize Eigenfaces
  for(unsigned int i = 0; i < pca.eigenvectors.rows; i++)
  {
    Mat eigenface;
    eigenface = pca.eigenvectors.row(i).clone();
    // normalize and reshape eigenface
    //applyColorMap(eigenface, eigenface, COLORMAP_JET);

    imshow(format("eigenface_%d", i), eigenface);
    waitKey();
  }

  // Project all samples into the Eigenspace
  // code..

  // ID Faces
  // code..

  return 0;
}

void addToDataSet(Mat &samples, vector &labels, Mat &newSamples, vector &newLabels)
{
  // your code from the pre lab
}

Mat norm_0_255(Mat src)
{
  // Create and return normalized image
  // should work for 1 and 3 channel images
}

string recognizeFace(Mat query, Mat samples, vector labels)
{
  // given a query sample find the training sample it is closest to and return the proper label
  // implement a nearest neighbor algorithm to achieve this
}

Feel free to email me if you want the solutions!

read more

ARPool in London UK!

Jeremy Clarkson, Me, Stephen Fry, Salar on the set of Gadget Man

Just got back from taking ARPool to London England (wow what a trip!) I’ll write a blog about my trip later but right now I just want to focus on ARPool.

On Thursday Chris and Alex from North One Television were about an hour late picking us up at our hotel so we were starting to set up behind schedule. I’m pretty thankful that the schedule was changed so that filming was late afternoon on Friday rather than morning or we would have been very pressed for time. I had to help build the Truss – so much for this being a show up and plug in a computer gig, anyways it wasn’t too big a deal and at least I didn’t have to carry and assemble a pool table like past events. We got the structure up and mounted the camera and projector (I am a wizard with zip-ties!) and fired up the calibration.

Things were not as smooth as I had expected – the table calibration step, the final step that creates the transformation matrices was giving us grief - the final image after the transformation was either rotated 90 degrees or had some really strange jagged distortion. I thought I had fixed this step but it needs more work – I expect the problem is in the line intersection code possible a bad solution or an overflow problem. It is really weird because you can perform this step multiple times without altering the code and it can give wildly different results. I think that drawing more intermediate images will fix this problem especially during the line intersection process.

* edit * The week after we got back I added more intermediate debugging visuals which are nice to confirm the calibration is doing what we expect but didn’t solve the issue. Still can’t believe this but our source of grief was from Qt being unable to display images with an odd number of rows or columns. That alone makes sense but what still confounds me is how this hadn’t come before…

Anyways thanks to the randomness of the table calibration procedure it eventually worked for us and we moved on to finish the calibration. We skipped ball training and simply ran the system, surprisingly the classifier was still able to get the cue ball!

Stephen Fry playing ARPool - also I swear I am not photo-shopped into this photo its just a weird affect from the light behind me!

The filming process was very interesting, there were tons of crew members and I could never really figure out who was in charge with the exception of the director. I got to briefly explain to Stephen Fry and Jeremy Clarkson how the system worked and how to use it before they played a game on film. Stephen Fry got to use ARPool while Jeremy was on his own. The shoot went pretty well but after the first shot Stephen Fry sort of forgot how to use it and didn’t have the cue close enough to the cue ball for it to detect the shot. They also both kept bumping into the truss which of course can throw off the whole calibration -wince! After their game we filmed a few sequences of Stephen using ARPool which were set up and I was there to guide him through making the shot. ARPool isn’t quite a hands off demo at this point! We got a few more shots after the celebrities left and everyone was pretty happy with how it went and I am sure that it will look great!

read more