文章来源地址https://uudwc.com/A/V6DE2文章来源:https://uudwc.com/A/V6DE2
目录
- source code:
source code:
import cv2
tracker = cv2.TrackerKCF_create()
video = cv2.VideoCapture(0) #cv2.CAP_DSHOW
while True:
ret,frame = video.read()
#if ret:
cv2.imshow("ROI Selector",frame)
k = cv2.waitKey(30)
if k == ord('q'):
break
bbox = cv2.selectROI(frame, False)
ok = tracker.init(frame,bbox)
cv2.destroyWindow("ROI Selector")
while True:
ret, frame = video.read()
ret, bbox = tracker.update(frame)
if ret:
p1 = (int(bbox[0]),int(bbox[1]))
p2 = (int(bbox[0]+bbox[2]),int(bbox[1]+bbox[3]))
cv2.rectangle(frame, p1,p2,(0,0,255),2,2)
cv2.imshow("Tracking", frame)
k = cv2.waitKey(1)
if k == ord('q'):
break