Create different color bounding boxes for multiobject detection model

Hi,

I am trying to modify the jetson-inference code to display different colour bounding boxes with a label depending on the type of object detected. I have managed to overlay the label but am struggling to change the bounding box colours.

Has someone managed to achieve this?

Thanks

Hi,

Try to update the defaultColors() function in the detectNet.cpp:
https://github.com/dusty-nv/jetson-inference/blob/master/detectNet.cpp#L142

You can set the desired color for each class like this:

// for class k
if( n == k )
{
    mClassColors[0][n*4+0] = 0.0f;	// r
    mClassColors[0][n*4+1] = 200.0f;	// g
    mClassColors[0][n*4+2] = 255.0f;	// b
    mClassColors[0][n*4+3] = 100.0f;	// a
}

Thanks.

Hi,

I modified the code as below

bool detectNet::defaultColors()
{
	const uint32_t numClasses = GetNumClasses();
	
	if( !cudaAllocMapped((void**)&mClassColors[0], (void**)&mClassColors[1], numClasses * sizeof(float4)) )
		return false;

	for( uint32_t n=0; n < numClasses; n++ )
	{
	if  ( n == 0 )
		{
			mClassColors[0][n*4+0] = 255.0f;	// r
			mClassColors[0][n*4+1] = 0.0f;	// g
			mClassColors[0][n*4+2] = 0.0f;	// b
			mClassColors[0][n*4+3] = 100.0f;	// a
		}

    if ( n == 1)
		{
			mClassColors[0][n*4+0] = 0.0f;	// r
			mClassColors[0][n*4+1] = 255.0f;	// g
			mClassColors[0][n*4+2] = 00.0f;	// b
			mClassColors[0][n*4+3] = 100.0f;	// a
		}

	if ( n == 2)
		{
			mClassColors[0][n*4+0] = 0.0f;	// r
			mClassColors[0][n*4+1] = 0.0f;	// g
			mClassColors[0][n*4+2] = 255.0f;	// b
			mClassColors[0][n*4+3] = 100.0f;	// a
		}

	}
	
	return true;
}

However there seems to be no set result when running the detection. The colors seem to interchange randomly particularly when 3 different objects are detected in a single shot.

I see there is a SeClassColor function which doesn’t appear to be used, could this fucntion be required?

Hi,

Any idea what the problem could be?

Hi,

Suppose it should work.

Could you check if the detected label (classIndex) is correct here?
https://github.com/dusty-nv/jetson-inference/blob/master/detectNet.cpp#L455

Thanks

I have checked and it is correct, the correct class outputs to the terminal.

The fact that it is displaying colours which haven’t even been defined is what is confusing me and making me think something else in the logic is wrong. If I have only defined 3 default colours of red green and blue, how is it that other colours such as yellow purple etc are displayed when more than 3 objects are detected?

Its almost as though it is combining colours.

Hi,

I can change the color by modifying the defaultColors(). Could you give it one more try?

bool detectNet::defaultColors()
{
        const uint32_t numClasses = GetNumClasses();

        if( !cudaAllocMapped((void**)&mClassColors[0], (void**)&mClassColors[1], numClasses * sizeof(float4)) )
                return false;

        for( uint32_t n=0; n < numClasses; n++ )
        {
                if( n == 0 )  // <- This works!
                {
                        mClassColors[0][n*4+0] = 0.0f;  // r
                        mClassColors[0][n*4+1] = 0.0f;        // g
                        mClassColors[0][n*4+2] = 0.0f;        // b
                        mClassColors[0][n*4+3] = 100.0f;        // a
                }else if( n != 1 )
                {
                        mClassColors[0][n*4+0] = 0.0f;  // r
                        mClassColors[0][n*4+1] = 200.0f;        // g
                        mClassColors[0][n*4+2] = 255.0f;        // b
                        mClassColors[0][n*4+3] = 100.0f;        // a
                }
                else
                {
                        mClassColors[0][n*4+0] = 0.0f;  // r
                        mClassColors[0][n*4+1] = 255.0f;        // g
                        mClassColors[0][n*4+2] = 175.0f;        // b
                        mClassColors[0][n*4+3] = 100.0f;        // a
                }
        }

        return true;
}

Thanks.