Problems with this code (Help please)

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

I am new to processing and am having trouble creating code. I’m using a face detector, but instead of a square I want the face to pixel that area of ​​a video. I have made a code but the pixels do not appear, how can I solve it? Thanks a lot!
Modified the code but I get a black square, I think it’s a color problem, can someone fix it? Specifically this part:

 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);
 numPixelsWide = faces [i].width;
  numPixelsHigh = faces[i].height;
  println(numPixelsWide);
  movColors = new color[numPixelsWide * numPixelsHigh];
  film.read();
film.loadPixels();
  for (int j = 0; j < numPixelsHigh; j++) {
    for (int r = 0; r < numPixelsWide; r++) {
      fill(movColors[j*numPixelsWide + r]);

I put the full code too:

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

int numPixelsWide, numPixelsHigh;
int blockSize = 10;

color movColors[];

Capture cam;

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

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

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

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

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


  noFill();




}

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);      

      }
    }
  }
      
  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);
 numPixelsWide = faces [i].width;
  numPixelsHigh = faces[i].height;
  println(numPixelsWide);
  movColors = new color[numPixelsWide * numPixelsHigh];
  film.read();
film.loadPixels();
  for (int j = 0; j < numPixelsHigh; j++) {
    for (int r = 0; r < numPixelsWide; r++) {
      fill(movColors[j*numPixelsWide + r]);
      
      ///////////
     
    }
  }
  
    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();
}
1 Like

The variables numPixelsWide and numPixelsHigh are never set.
Presumably these would be set to match the size of a face rectangle divided by blockSize.

1 Like

Reading and changing pixels are made between loadPixels() and updatePixels() They give you pixels[] array. Called as is it gives pixel array of the drawing canvas. If you want to read and write to film, so you need to call film.loadPixels() to get pixel array of the film.

Then again that part of the code doesn’t seem to do anything useful, so I guess it’s work in progress.

1 Like