Changing rectangle color for one class in object detection using Python and OpenCV

120 views Asked by At

I have a Python script that performs object detection, and it correctly identifies two labels. Currently, both of these labels are displayed in either red or yellow color. I want to keep one of the classes in red, but for the other class, I would like to change the rectangle color from yellow to green. How can I modify my code to achieve this?

Here is the relevant part of my Python script:

from ultralytics import YOLO
import cv2

cap = cv2.VideoCapture(0)

model_path = r"C:\Users\Asus Hn004W\Desktop\model_YOLOv8"
model = YOLO(model_path)

weight = 640
height = 640

while True:
    ret, frame = cap.read()

    if not ret:
        print("Camera does not work!")
        break

    results = model(frame)

    bbox = results[0].cpu().boxes.data.numpy().astype('int32')

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Currently, the code draws rectangles in red and yellow for the two classes. How can I modify it so that for one of the classes, the rectangle color is changed to green instead of yellow?

Thank you for your help!

0

There are 0 answers