Python led ışık takibi

Merhaba herkese iyi çalışmalar,
Video kayıdına altına aldığım led ışıkların merkezini saptayıp, bu noktaları kayıt süresince takip edip. Bu takip bilgisini zamana bağlı değişimini kaydetmek istiyorum. Bunun için aşağıdaki kodu geliştirdim. Pythonda kod yazma konusunda çok acemiyim, bu konuda bana yardımcı olmak isterseniz çok sevinirim.
Şimdiden yaptığınız yorumlar için teşekkür ederim.
Saygılarım ile,

import cv2

import numpy as np

import matplotlib.pyplot as plt

cap = cv2.VideoCapture(“C0.MP4”)

while True:

_, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

blur = cv2.GaussianBlur(gray, (9, 9), 0)

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #

lower_white = np.array([0, 0, 200], dtype=np.uint8) #

upper_white = np.array([145, 40, 255], dtype=np.uint8) #

mask = cv2.inRange(hsv, lower_white, upper_white) #

res = cv2.bitwise_and(frame,frame, mask= mask) #

(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(blur)

hi, threshold = cv2.threshold(blur, maxVal - 20, 230, cv2.THRESH_BINARY)

thr = threshold.copy()

cv2.resize(res, (300, 300)) #

cv2.resize(thr, (300, 300))

edged = cv2.Canny(threshold, 10, 150)

lightcontours, hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

_,img = cv2.threshold(img,0,255,cv2.THRESH_OTSU)

h, w = img.shape[:2]

cv2.resize(res, (300, 300))

contours0, hierarchy = cv2.findContours( img.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

moments  = [cv2.moments(cnt) for cnt in contours0] 

centroids = [( int(round(m['m10']/m['m00'])),int(round(m['m01']/m['m00'])) ) for m in moments]


print ('centroids:', centroids)

for c in centroids:

 cv2.circle(img,c,1,(0,0,0),2)

cv2.imshow('image', img)

#cv2.imshow('Res', res)

#cv2.imshow('light', thr)

#cv2.imshow('frame', frame)



if cv2.waitKey(1) & 0xff == ord('q'):

 break

cap.release()

cv2.destroyAllWindows()