# Problems with this code (Help please)

**URL:** <https://discourse.processing.org/t/problems-with-this-code-help-please/21517>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [June 3, 2020, 1:44am UTC](https://discourse.processing.org/t/problems-with-this-code-help-please/21517 "2020-06-03T01:44:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![panic](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@panic](https://discourse.processing.org/u/panic)\
**Post date:** [June 3, 2020, 1:44am UTC](https://discourse.processing.org/t/problems-with-this-code-help-please/21517/1 "2020-06-03T01:44:30Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

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:

```auto
 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:

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [June 3, 2020, 4:29am UTC](https://discourse.processing.org/t/problems-with-this-code-help-please/21517/2 "2020-06-03T04:29:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [June 3, 2020, 5:12am UTC](https://discourse.processing.org/t/problems-with-this-code-help-please/21517/3 "2020-06-03T05:12:58Z")

</div>

> [@panic](#):
>
> ```auto
> 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();
> 
> ```

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.
