from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
tracker_name= 'csrt'
OPENCV_OBJECT_TRACKERS = {
"csrt": cv2.TrackerCSRT_create,
"kcf": cv2.TrackerKCF_create,
"boosting": cv2.TrackerCSRT_create,
"mil": cv2.TrackerMIL_create,
"tld": cv2.TrackerCSRT_create,
"medianflow": cv2.TrackerCSRT_create,
"mosse": cv2.TrackerCSRT_create
}
tracker = OPENCV_OBJECT_TRACKERS['csrt']()
initBB = None
print("打开摄像头...")
vs = VideoStream(src=0).start()
time.sleep(1.0)
while True:
frame = vs.read()
if frame is None:
break
frame=cv2.flip(frame,1)
frame = imutils.resize(frame,width=500)
(H, W) = frame.shape[:2]
if initBB is not None:
(success, box) = tracker.update(frame)
if success:
(x, y, w, h) = [int(v) for v in box]
cv2,rectangle(frame, (x, y),(x + w, y + h),
(0, 255, 0),2)
cv2.imshow("Frame",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("s"):
initBB = cv2.selectROI("Frame", frame, fromCenter=False,showCrosshair=True)
tracker.init(frame, initBB)
if key == ord("q"):
break
vs.stop()
cv2.destroyAllwindows()
目标追踪之调用摄像头
最新推荐文章于 2024-06-13 16:33:45 发布