Too slow camera on android + ketai

edited August 2017 in Android Mode

Hi guys, i need a little help in a sketch i'm creating (sorry for my english, i'm brazilian). Ok, my sketch has to activate the camera and show the coordinates, however is very, very slow. I'm using my zenfone 3 with 4gb ram for tests.

Sorry for any error, first time using this forum This is the code:

 import ketai.camera.*;

  KetaiCamera cam;

   void iniciarCamera(int res_Width, int res_Height, int fps) {

   try{
     if(cam == null){
     cam = new KetaiCamera(this, res_Width, res_Height, fps);
    img = cam;
  }
} catch (Exception e){
    erroGeral+="Erro durante a inciação da camera. \n";
}   
}

  void onCameraPreviewEvent()
 {
     cam.read();
  }

`

 String erroGeral = "";
  void setup() {
  size (750,480);      // Resolução do programa.
  orientation(LANDSCAPE);
  imageMode(CENTER);
  iniciarCamera(640,480,24);  //Resolução da qualidade da camera.
  initScanner();
}


 void draw() {
   if(erroGeral==null || erroGeral == ""){

   if (cam!= null && cam.isStarted()){
  //image(cam, width/2, height/2, width, height);  
   //if (botaoPressionado){
     img = cam;
    //img.filter(THRESHOLD);
   // bs.imageFindBlobs(img);
    image(img, width/2, height/2, width, height); 

   leitura();
 //}

} else {
  background(128);
  textSize(20);
  fill(255);
  text("Aguardando camera....toque para ativar.", 100, height/2);
}



  // Tratativa para Erros em geral (ajuda a localizar no código onde o erro se encontra)
} else {
  background(color(255,0,0));
  textSize(20);
  fill(255);
  text(erroGeral, 100, height/2);
   } // Fim da trativa de erro.
}

  private void leitura() {

    img.loadPixels();

    bs.findBlobs(img.pixels, img.width, img.height);
    bs.loadBlobsFeatures();
    bs.drawBox(boundingBoxCol, boundingBoxThickness);
    bs.weightBlobs(false);
    bs.findCentroids();

    for (int i = 0; i < bs.getBlobsNumber(); i++) {
      stroke(0, 255, 0);
      strokeWeight(2);
      fill(255);

      point(bs.getCentroidX(i), bs.getCentroidY(i));

      textFont((createFont("Calibri", 14, true)));
      text("x-> " + bs.getCentroidX(i) + "\ny-> " + bs.getCentroidY(i)+ "\nId-> "+ i, bs.getCentroidX(i)+30, bs.getCentroidY(i));

      if ((bs.getCentroidX(i) < blobrefX) && (bs.getCentroidY(i) < blobrefY)) {
        blobrefX = bs.getCentroidX(i);
        blobrefY = bs.getCentroidY(i);
        blobrefId = i;
      }

      if (!passos[1]){
        matrizBlob[0] = true;
      }

      if (i != blobrefId) {

        if (bs.getCentroidX(i) < blobrefX && bs.getCentroidY(i) > blobrefY-blobH && bs.getCentroidY(i) < blobrefY+blobH )
        {
          blobrefId = i;
          blobrefX = bs.getCentroidX(i);
          blobrefY = bs.getCentroidY(i);
          blobrefEsq = true;
          matrizBlob[0] = true;
          matrizBlob[1] = true;
          passos[0] = true;
        } else if (bs.getCentroidX(i) > blobrefX && bs.getCentroidY(i) > blobrefY-blobH && bs.getCentroidY(i) < blobrefY+blobH ) {
          blobrefEsq = true;
          matrizBlob[0] = true;
          matrizBlob[1] = true;
          passos[0] = true;
        } else if (bs.getCentroidX(i) < blobrefX-blobW*1.5 && bs.getCentroidY(i) > blobrefY+ blobH && !passos[0] ) {
          blobrefEsq = false;
          passos[1] = true;
          matrizBlob[0] = false;
          matrizBlob[1] = true;
          passos[2] = true;
        } else if (bs.getCentroidX(i) >= blobrefX-blobW && bs.getCentroidY(i) > blobrefY+ blobH && !passos[0] && !passos[2]) {
          matrizBlob[0] = true;
          matrizBlob[1] = false;
          //matrizBlob[3] = true;
          //blobrefEsq = true;
          //println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        }


        if (blobrefEsq) {
          if (bs.getCentroidX(i) < blobrefX+blobW && bs.getCentroidY(i) > blobrefY+blobH && bs.getCentroidY(i)< blobrefY+3*blobH)   {matrizBlob[2] = true;}
          if (bs.getCentroidX(i) > blobrefX+blobW && bs.getCentroidY(i) > blobrefY+blobH && bs.getCentroidY(i)< blobrefY+3*blobH)   {matrizBlob[3] = true;}
          if (bs.getCentroidX(i) < blobrefX+blobW && bs.getCentroidY(i) > blobrefY+3*blobH && bs.getCentroidY(i)< blobrefY+7*blobH) {matrizBlob[4] = true;}
          if (bs.getCentroidX(i) > blobrefX+blobW && bs.getCentroidY(i) > blobrefY+3*blobH && bs.getCentroidY(i)< blobrefY+7*blobH) {matrizBlob[5] = true;}
        } else {
          if (bs.getCentroidX(i) < blobrefX-blobW && bs.getCentroidY(i) > blobrefY+blobH && bs.getCentroidY(i)< blobrefY+3*blobH)   {matrizBlob[2] = true;}
          if (bs.getCentroidX(i) > blobrefX-blobW && bs.getCentroidY(i) > blobrefY+blobH && bs.getCentroidY(i)< blobrefY+3*blobH)   {matrizBlob[3] = true;}
          if (bs.getCentroidX(i) < blobrefX-blobW && bs.getCentroidY(i) > blobrefY+3*blobH && bs.getCentroidY(i)< blobrefY+7*blobH) {matrizBlob[4] = true;}
          if (bs.getCentroidX(i) > blobrefX-blobW && bs.getCentroidY(i) > blobrefY+3*blobH && bs.getCentroidY(i)< blobrefY+7*blobH) {matrizBlob[5] = true;}
        }

      }
    println("ID REF: "+blobrefId+ "\n X REF:  "+blobrefX); // Debug
    }
   for (int p = 0; p <=  6; p++) {
     try {
       if (matrizBlob[p]) {

         testeAtivo+= (p)+"  ";        // Debug

         if (p == 0) {somaBlob += 1;}
         if (p == 1) {somaBlob += 2;}
         if (p == 2) {somaBlob += 4;}
         if (p == 3) {somaBlob += 8;}
         if (p == 4) {somaBlob += 16;}
         if (p == 5) {somaBlob += 32;}
        }
     } catch(IndexOutOfBoundsException e) {

       testeAtivo = "MUITOS PONTOS";
     }
}

    switch(somaBlob) {
      case 1:  letraBlob = "A";   break; 
      case 5:  letraBlob = "B";   break; 
      case 3:  letraBlob = "C";   break; 
      case 11: letraBlob = "D";   break; 
      case 9:  letraBlob = "E";   break; 
      case 7:  letraBlob = "F";   break;
      case 15: letraBlob = "G";   break;
      case 13: letraBlob = "H";   break;
      case 6:  letraBlob = "I";   break;
      case 14: letraBlob = "J";   break;
      default: letraBlob = "NoN"; break;
}

Answers

  • Edit post, highlight code, press Ctrl-o to format.

  • i think it's right now, thanks for help

  • One way is to call leitura every other frame.

    Even better is to drop the resolution of the blobs you are processing (not the display image) to half -- 375x240. That pushes 25% of the pixels through your most expensive operations.

Sign In or Register to comment.