We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I would like to use a value created within pushMatrix () and popMatrix (), outside this block. In my case, I would use the value generated by l = faces [0] .x; In rect outside pushMatrix () and popMatrix (). How can I do?
if(faces.length>0){ l = screenX( faces[0].x, faces[0].y ); }
https://processing.org/reference/screenX_.html
We can't debug pictures of code.
The value of l outside the push / pop is identical to that inside. What changes is the projection because of the scale and translate.
Your for loop is useless btw, why are you accessing the 1st item multiple times?
Because I want to recognize only the first face she sees.
How do I use translated and climbed value outside of push / pop?
This is my code:
import processing.video.*; import gab.opencv.*; import java.awt.Rectangle; Capture cam; OpenCV opencv; int l; void setup() { size(640, 480); cam = new Capture(this, width, height, 30); opencv = new OpenCV(this, width, height); opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE); cam.start(); } void draw() { if (cam.available() == true) { cam.read(); } pushMatrix(); scale(-1, 1); translate(-cam.width, 0); image(cam, 0, 0); opencv.loadImage(cam); Rectangle[] faces = opencv.detect(); noFill(); stroke(0, 255, 0); strokeWeight(3); for (int i = 0; i < faces.length; i++) { rect(faces[0].x, faces[0].y, faces[0].width, faces[0].height); l = screenX( faces[0].x ,); } println(l); popMatrix(); rect(l,0,50,50); }
TfGuy44 explained it
l = screenX( faces[0].x );
It does not work, it says I must add another value. What should I put?
then you don't need the loop.
then you don't need the loop. How do I do it?
rect(faces[0].x, faces[0].y, faces[0].width, faces[0].height);
is enough, no need for a loop.
you need to use the y position for the other variable, even if you only want the result for x.
l = screenX(faces[0].x, faces[0].y);
Ok for that I solved
For this, without the cycle, it does not work
please examine that picture. can you read it? no. neither can we.
what happens when the camera doesn't detect any faces?
well, then this will return an empty Rectangle array
Rectangle[] faces = opencv.detect();
but in this bit of code
it's accessing the first element regardless of whether it exists or not. that's the error.
the easy way around it to is check the length of the array before trying to access it.
if (faces.length > 0) { // it's safe to use the array rect(faces[0].x, faces[0].y, faces[0].width, faces[0].height); }
I solved, thank you so much for your help
Answers
if(faces.length>0){ l = screenX( faces[0].x, faces[0].y ); }
https://processing.org/reference/screenX_.html
We can't debug pictures of code.
The value of l outside the push / pop is identical to that inside. What changes is the projection because of the scale and translate.
Your for loop is useless btw, why are you accessing the 1st item multiple times?
Because I want to recognize only the first face she sees.
How do I use translated and climbed value outside of push / pop?
This is my code:
TfGuy44 explained it
It does not work, it says I must add another value. What should I put?
then you don't need the loop.
is enough, no need for a loop.
you need to use the y position for the other variable, even if you only want the result for x.
l = screenX(faces[0].x, faces[0].y);
Ok for that I solved
rect(faces[0].x, faces[0].y, faces[0].width, faces[0].height);
For this, without the cycle, it does not work
please examine that picture. can you read it? no. neither can we.
what happens when the camera doesn't detect any faces?
well, then this will return an empty Rectangle array
but in this bit of code
it's accessing the first element regardless of whether it exists or not. that's the error.
the easy way around it to is check the length of the array before trying to access it.
I solved, thank you so much for your help