Let’s Face It!

For this week’s image and video processing project, I decide to explore face recognition tools in a live video capture. Conceptually, I wanted to write a code that could detect a moving face and substitute the face with a random image. I came across a library called OpenCV that allows easy face detection processes. To mark the area where the person’s face is, I drew a rectangle approximately where the face is and substituted it with an image file drawn from an array of many.

Trial

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

Capture video;
OpenCV opencv;

void setup() {
 size(640, 480);
 video = new Capture(this, 640/2, 480/2);
 opencv = new OpenCV(this, 640/2, 480/2);
 opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE); 
 video.start();
}
void draw() {
 
 scale(2);
 opencv.loadImage(video);

 image(video, 0, 0 );

 noFill();
 stroke(255, 0, 0);
 strokeWeight(1);
 Rectangle[] faces = opencv.detect();
 println(faces.length);
 
 for (int i = 0; i < faces.length; i++) {
 println(faces[i].x + "," + faces[i].y);
 rect(faces[i].x, faces[i].y, faces[i].width+20, faces[i].height+20);
 }
}

void captureEvent(Capture c) {
 c.read();
}

Below is the final result.

Improvements

One of my biggest challenges was to scale the images to the size of the face captured in the live video to generate a smoother more accurate final output. I scaled the images manually but I am wondering if there are ways to adapt the code based on the size of the face captured.

Below is the code for the final output

import gab.opencv.*;
import processing.video.*;
import java.awt.*;
int index;
boolean pressed;

Capture video;
OpenCV opencv;
PImage []img=new PImage[7];
PImage face, obama;
int image_index;

void setup() {
 size(640, 480);
 video = new Capture(this, 640/2, 480/2);
 opencv = new OpenCV(this, 640/2, 480/2);
 opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE); 
 video.start();
 img[0]= loadImage("obama.png");
 img[1]= loadImage("trump.png");
 img[2]= loadImage("aaron.png");
 img[3]= loadImage("zbynek.png");
 img[4]= loadImage("lama1.png");
 img[5]= loadImage("Daniil.png");
 
}

void draw() {
 
 scale(2);
 opencv.loadImage(video);

 image(video, 0, 0 );

 fill(0);
 stroke(255, 0, 0);
 strokeWeight(1);
 Rectangle[] faces = opencv.detect();
 println(faces.length);
 
 for (int i = 0; i < faces.length; i++) {
 println(faces[i].x + "," + faces[i].y);
 image(img[index],faces[i].x, faces[i].y, faces[i].width+20, faces[i].height+20);

 if(mousePressed==true){
 image(img[index++],faces[i].x, faces[i].y, faces[i].width+20, faces[i].height+20);
 }
}
}


void captureEvent(Capture c) {
 c.read();
}