Pi Car X vilib color detection, how to make it detect several colours at once instead of just one at a time

Hello everynyan!

Im new to programming, I am working on my finals schoolproject and I met a specific issue that im trying to solve.
My question is in the title. I read the vilib documentation and vilib apparently only is capable of detecting one color at a time. But it was trained on 6 colors? I wonder if its possible to somehow make vilib recognise both for example Blue and Red in one picture.

For my specific usecase it would already suffice if it was possible to detect a colour without defining which colour to detect first.

I thought of a potential solution to this but idk if this would work:
Goal: while driving, the Pi Car X is supposed to recognise color A or B or C.
Restriction: Vilib only detects one color at a time (only A, B or C)

Potential solution:
Make vilib rotate through a Cycle of A,B or C detection really fast:
(heres the idea in noobish fictious programming language)

Pi Car X drives
while true:
vilib.color_detect(color=”A”)
if color A is detected, do X
else vilib.color_detect(color=”B”)
if color B is detected, do Y
else vilib.color_detect(color=”C”)
if color C is detected, do Z
loop

If this cycle would be able to happen really fast while the picar is driving rather slow, would this work? Im consciously ignoring aspects like the pi car recognising the wrong colours or environmental issues to this, i want to know if this would theoretically work.

Have a nice day :3

Yes, by default, the vilib library only supports detecting one color at a time.

Your idea of detecting multiple colors simultaneously is theoretically feasible. However, there are several important considerations for implementation:

Reduce single-frame processing time: Optimize the vilib detection by adjusting parameters like “minimum target size” and “confidence threshold” to minimize unnecessary computations.

Optimize the loop structure:

Use time.time() to control the loop frequency and avoid excessive CPU usage.

Implement a “detection result cache” to prevent the same object from triggering actions repeatedly.

Account for color-switching delay: Include a short delay, such as time.sleep(0.005), to ensure the vilib library completes its detection mode switch between colors.

Establish a priority mechanism: In the loop, you might implement a rule like “the first detected color triggers the action first”. If you need equal responsiveness for multiple colors, you can remove the break statement, but be aware that this might trigger multiple actions from a single frame.

Adjust for real-world conditions:

During testing, adjust the Vilib color thresholds (set_color_detect_threshold) based on ambient lighting to avoid detection failures.

If the vehicle is moving fast, you can either reduce the loop delay (decrease the time.sleep duration) or lower the vehicle’s speed for more reliable detection.

This approach requires careful tuning and testing to balance responsiveness and stability.