# OpenCV detection problem

**URL:** https://discourse.processing.org/t/opencv-detection-problem/24831
**Category:** Libraries
**Created:** [October 22, 2020, 9:39pm UTC](https://discourse.processing.org/t/opencv-detection-problem/24831 "2020-10-22T21:39:08Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Vickydcl](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@Vickydcl](https://discourse.processing.org/u/Vickydcl)
#### Post date: [October 22, 2020, 9:39pm UTC](https://discourse.processing.org/t/opencv-detection-problem/24831/1 "2020-10-22T21:39:08Z")

</div>

So I’m doing a videogame where you use your face to move the gun and blink to shot.  
Mi first attempt was doing it like this:

```auto
setup()
{
  camara = new Capture( this, ancho, alto );
  camara.start();
  opencv = new OpenCV(this, ancho, alto );
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}
draw()
{
    camara.read();
    opencv.loadImage( camara );
//detect face
    rostros = opencv.detect();
    for (int i = 0; i < rostros.length; i++) 
        {
           PImage cara = camara.get(rostros[i].x, rostros[i].y, rostros[i].width, rostros[i].height);
           
           //eyes opencv

           opencv_ojos = new OpenCV(this, cara );  
           opencv_ojos.loadCascade(OpenCV.CASCADE_EYE); 
           Rectangle[] ojos = opencv_ojos.detect();
           //draw eyes

            stroke(0,0,255);
           
            if(ojos.length!=0 && ojos.length!=1) //only show 2 eyes
            {
              for(int n=0; n<2;n++)
               {
                  noFill();
                  rect(ojos[n].x + rostros[i].x, ojos[n].y + rostros[i].y,ojos[n].width,ojos[n].height);
              }
            } 
       }

```

And it worked, but the game renders slow because I’m creating the opencv\_ojos object several times, so I tried another way:

```auto
setup(){
  camara = new Capture( this, ancho, alto );
  camara.start();
  opencv = new OpenCV(this, ancho, alto );
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
  opencv_ojos = new OpenCV(this, ancho,alto );  
  opencv_ojos.loadCascade(OpenCV.CASCADE_EYE); 
}
draw()
{
    camara.read();
    opencv.loadImage( camara );
//detect face
    rostros = opencv.detect();
    for (int i = 0; i < rostros.length; i++) 
        {
           PImage cara = camara.get(rostros[i].x, rostros[i].y, rostros[i].width, rostros[i].height);
           
           // load pimage cara into opencv_ojos

           opencv_ojos.loadImage(cara);
           Rectangle[] ojos = opencv_ojos.detect();

           //draw eyes

            stroke(0,0,255);
           
            if(ojos.length!=0 && ojos.length!=1)
            {
              for(int n=0; n<2;n++)
               {
                  noFill();
                  rect(ojos[n].x + rostros[i].x, ojos[n].y + rostros[i].y,ojos[n].width,ojos[n].height);
              }
            }  
}

```

But in this way, the program does not detect the eyes, so i don’t know what to do, **any advice**?  
Thanks!

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [October 23, 2020, 12:27pm UTC](https://discourse.processing.org/t/opencv-detection-problem/24831/2 "2020-10-23T12:27:07Z")

</div>

> [@Vickydcl](#):
>
> ```auto
> opencv_ojos = new OpenCV(this, cara );  
> opencv_ojos.loadCascade(OpenCV.CASCADE_EYE); 
> 
> ```

I guess without using the specific eye detection function loadCascade() on every new OpenCv object, detection really will be impossible.
