Tracking and putting ID to the tracked car problem

93 Views Asked by At

This is my code and my main concern is putting ID Number to the tracked vehicle inside the frame in order to seperate each car inside the frame, I want to use supervision but I'm having a hard time implementing it. I've researched about other tracking libraries such as sort, deepsort, and bytetrack, however supervision is the one I chose as it looks more implementable for me and my knowledge. I'm sorry for confusing and noob question. That's why I want to look for ways to implement supervision to my program.

from ultralytics import YOLO
import cv2
import cvzone
import math
import os
import supervision as sv 

#cap = cv2.VideoCapture(0) #For Camera
cap = cv2.VideoCapture('cars.mp4')
cap.set(3, 1280)
cap.set(4, 720)
screenshot_count = 0
folder_name = "Screenshots"
os.makedirs(folder_name, exist_ok=True)

model = YOLO("../weights/yolov8n.pt")

classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat", 
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", 
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", 
              "handbag","tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", 
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", 
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", 
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed", 
              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone", 
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", 
              "teddy bear", "hair drier", "toothbrush"]

mask = cv2.imread("YoloThings/mask1.png")

while True:
    success, img = cap.read()
    
    rof = cv2.bitwise_and(img,mask)

    results = model(rof, stream=True)

    for r in results:
        boxes = r.boxes
        
        for box in boxes:
        
            #Bounding Box---------------
            x1,y1,x2,y2 = box.xyxy[0]
            x1,y1,x2,y2 = int(x1),int(y1),int(x2),int(y2)
            #cv2.rectangle(img, (x1,y1),(x2,y2),(0,255,0),3) //For plain box
            detections =sv.Detections.from_ultralytics()

            w,h = x2-x1, y2-y1
            cropped_rof = img[y1:y1+h, x1:x1+w]
            #Confidence
            conf = math.ceil((box.conf[0]*100))/100
            print(x1, y1)
            #Class 
            cls = int(box.cls[0])
            currentClass = classNames[cls]

            if currentClass =="car" or currentClass =="truck" or currentClass =="motorbike" or currentClass =="bus" and conf >0.3:
                cvzone.putTextRect(img, f'{classNames[cls]} {x1} {y1}',(x1,y1-20),scale=1.5,thickness=2, offset=5)
                cvzone.cornerRect(img,(x1,y1,w,h))
                
            
    cv2.imshow("Image", img)
    cv2.imshow("Region of Focus", rof)
    if cv2.waitKey(1) == ord("s"):
        screenshot_count += 1
        filename = os.path.join(folder_name, f"screenshot_{screenshot_count}.png")
        cv2.imwrite(filename, cropped_rof)

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

I've tried to use sort but it uses lap that requires access to my gpu, my gpu is kinda low that's why I dont want to use it as it may affect the performance of my laptop.

0

There are 0 best solutions below