1

How can we set different colors for different objects detected using YOLO. Now everything detected are displaying in green rectangle.

function draw() {
    image(video, 0, 0, width, height); // Displaying image on a canvas
    for (let i = 0; i < objects.length; i++)  //Iterating through all objects
    {
        noStroke();
        fill(0, 255, 0); //Color of text
        text(objects[i].label, objects[i].x * width, objects[i].y * height - 5); 
        //Displaying the label
        noFill();
        strokeWeight(4); 
        stroke(0, 255, 0); //Defining stroke for rectangular outline
        rect(objects[i].x * width, objects[i].y * height, objects[i].w * width, 
        objects[i].h * height);
    }
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
soccerway
  • 10,371
  • 19
  • 67
  • 132

1 Answers1

2

stroke() sets a global state, it sets the color used to draw lines and borders around shapes. This state is kept even beyond frames.
This means all objects which are drawn after the call stroke() are drawn with the set color.
If you want to change the color, you've to call stroke() again.

If you want to use different colors the you can define a list of colors and use the modulo operator (%) to get an index of the color list. e.g. use a red, green and blue color:

colorList = [[255, 0, 0], [0, 255, 0], [0, 0, 255]];

function draw() {

    // [...]


    for (let i = 0; i < objects.length; i++) {

        // [...]

        let colorI = i % colorList.length;
        stroke(...colorList[colorI]);

        // [...]
    }
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174