Why doesn't this code work? (Noob question)

please format code with </> button * homework policy * asking questions

I am new to processing and I had doubts when creating a code for a project.
I want that detecting the face with the camera produces changes in the pixels of a video that I import. The camera detects my face and the brightness changes but the video is not seen. I don’t know if it can’t or I’m not applying the code well.

import gab.opencv.*;
import processing.video.*;
import java.awt.*;
Capture video;
OpenCV opencv;
Movie film;


void setup() {  
  size(320, 240);  

  video = new Capture(this, 320, 240);  
  
  opencv = new OpenCV(this, 640/2, 480/2);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  

  video.start();
  film= new Movie(this, "Secuencia 01.mov");
  film.loop();
}
  

void captureEvent(Capture video) {  

  video.read();
}
void draw() {
   scale(2);
  opencv.loadImage(video);
  loadPixels();
  video.loadPixels();  
  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
  Rectangle[] faces = opencv.detect();
  println(faces.length);

  for (int x = 0; x < video.width; x++) {    
    for (int y = 0; y < video.height; y++) {      
      // Calculate the 1D location from a 2D grid
      int loc = x + y * video.width;      
    
          
      float r = red  (video.pixels[loc]);      
      float g = green(video.pixels[loc]);      
      float b = blue (video.pixels[loc]);      
      
  
      
       for (int i = 0; i < faces.length; i++) {
    println(faces[i].x + "," + faces[i].y);
      float d = dist(faces[i].x, faces[i].y, faces[i].width, faces[i].height);      
      float adjustbrightness = map(d, 0, 100, 4, 0);      

      r *= adjustbrightness;      
      g *= adjustbrightness;      
      b *= adjustbrightness;      
      
        
      r = constrain(r, 0, 255);      
      g = constrain(g, 0, 255);      
      b = constrain(b, 0, 255);      
    
       
      color c = color(r, g, b);      
      pixels[loc] = c;    
    }  
  } }

  updatePixels();
}
1 Like

I think this is what you were trying to do. I did not translate the brightness (I tried) from your face (from your video cam) into the movie. I am just displaying the face position on top of the movie. This should get you to your next step.

Kf

import processing.video.*;
import java.awt.*;
Capture cam;
OpenCV opencv;
Movie film;


void settings() {
  size(640, 480);
}

void setup() {  
  colorMode(HSB, 256, 256, 256);

  film= new Movie(this, "demo.mp4");
  cam = new Capture(this, width, height);  

  opencv = new OpenCV(this, width, height);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  

  cam.start();
  film.loop();


  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
}

void draw() {
  background(250, 150, 0);

  //scale(2);
  opencv.loadImage(cam);

  //video.loadPixels();  
  image(film, 0, 0);
  loadPixels();

  Rectangle[] faces = opencv.detect();



  if (frameCount%15==0) println(faces.length);

  for (int x = 0; x < film.width; x++) {    
    for (int y = 0; y < film.height; y++) {      
      // Calculate the 1D location from a 2D grid
      int loc = x + y * film.width;      

      float h= hue(film.pixels[loc]);
      float s= saturation(film.pixels[loc]);
      float br= brightness(film.pixels[loc]);



      for (int i = 0; i < faces.length; i++) {
        //println(faces[i].x + "," + faces[i].y);
        float d = dist(faces[i].x, faces[i].y, faces[i].width, faces[i].height);      
        float adjustbrightness = map(d, 50, 200, 0, 255);


        color c = color(h, s, br);      
        pixels[loc] = c;
      }
    }
  }
  updatePixels();

  for (int i = 0; i < faces.length; i++) {
    //println(faces[i].x + "," + faces[i].y);
    float d = dist(faces[i].x, faces[i].y, faces[i].width, faces[i].height);

    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);

    if (frameCount%15==0) println(faces[i].x, faces[i].y, d);
  }
}


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

void movieEvent(Movie m) {
  m.read();
}

2 Likes

Thank you very much, that was the main problem I had, I can continue, thank you.