Processing: Mega Clean up

In a previous post where I was cleaning up my code, I mentioned how I wanted to work out how to store the captured faces in an array of PImages rather than individual PImages like I am currently doing. This will significantly reduce the number of lines in my code and make it look a lot neater.

Well, after a long day of staring at my screens, and a few mental breakdowns I finally worked it out! Below is the code which does the magic:

//empty array to store captured faces for swapping
  PImage[] captured = new PImage[faces.length];

  //fill the array with the faces
  for (int i = 0; i < faces.length; i++) {
    captured[i] = get(faces[i].x*scale, faces[i].y*scale, faces[i].width*scale, faces[i].height*scale);

  //swap over the faces
  for (int s = 0; s < captured.length-1; s++) {
    captured[s].resize(faces[s+1].width*scale, faces[s+1].height*scale);

    mask.resize(faces[s+1].width*scale, faces[s+1].height*scale);

    image(captured[s], faces[s+1].x*scale, faces[s+1].y*scale);

  //swap the last detected face with the first detected face
  if (captured.length >= 2) {
    int c = captured.length-1;
    captured[c].resize(faces[0].width*scale, faces[0].height*scale);

    mask.resize(faces[0].width*scale, faces[0].height*scale);

    image(captured[c], faces[0].x*scale, faces[0].y*scale);

The new code shortens my old code significantly as there is no longer the need to grab the faces individually, then for each instance where there could be 2, 3 or 4 faces for example, it doesn’t need another if statement to separate them all. I’ll try to summarise the above code with words as unfortunately while it is shorter, its much harder to read through than before.

It starts by creating a PImage array which is the same length as the array of detected faces. The array is then filled with a for loop by iterating the value of i from 0 up to the length of the array. The number is then used in place of the array index so that it fills that spot in the array with the pixel information it is grabbing with the get function. To then swap the faces over, another for loop is used which uses the 4 functions needed from before; resize to match the host face, resize the mask to match, apply the mask, then place the face down in its new location. The for loop works by swapping the face with the next one along in the array. For example, face 1 is in place of face 2, face 2 in place of face 3, and so on until there aren’t any more faces. This loop leaves a problem with the last face in the array as it has nowhere to go. Logically it needs to loop back around to the beginning of the array and be in place of the first one. To do this I used an if statement which checks to see of there are 2 or more faces detected. If there are, it creates a variable called c which is the length of the array of captured faces minus one. This is so it can be used in the array index to always access the last face in the array (as arrays start at 0 not 1). It then uses c to resize and swap the face in place of the first one in the array to complete the cycle.

This new method has cut the length of my code in over a half making it much more concise. There is also another benefit which I didn’t even consider while making it. Before, my code was limited to swapping over 4 faces. This was then increased to 6 during testing as 4 wasn’t enough. With this new code there isn’t a set limit of faces it can swap. Using these loops I can swap every face that is detected which would work much better in a public space with a lot of people.

While I’ve already done the testing, I much prefer this final version of my code as it is much tidier and actually works better than it did before due to there not being a limit to the faces it can swap.

%d bloggers like this: