GOTURN OpenCV Python Deep learning based Object Tracking Algorithm





import cv2
import numpy as np

# Load GOTURN model
tracker = cv2.TrackerGOTURN_create()



#live video camera
cap = cv2.VideoCapture(1)
if not cap.isOpened():
    print("Error: Could not open video.")
    exit()

# Read the first frame
ret, frame = cap.read()
if not ret:
    print("Error: Could not read video frame.")
    exit()

bbox = cv2.selectROI(frame, False)

#bbox = (282, 34, 124, 141)  # Adjust based on your object location and size
ok = tracker.init(frame, bbox)

while True:
    ret, frame = cap.read()
    if not ret:
        break
    # Start timer
    timer = cv2.getTickCount()
    # Update the tracker
    ok, bbox = tracker.update(frame)
    if ok:    
        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
        p1 = (int(bbox[0]), int(bbox[1]))
        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
        cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)

    #display FPS
    cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2)






    cv2.imshow("Tracking", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    


print(bbox)
cap.release()
cv2.destroyAllWindows()

Comments