当前位置:   article > 正文

OpenCV-目标跟踪_opencv trackercsrt

opencv trackercsrt

使用OpenCV进行目标跟踪,各种跟踪效果都能比较,还是很有意思的:

  1. # USAGE
  2. # python opencv_object_tracking.py
  3. # python opencv_object_tracking.py --video dashcam_boston.mp4 --tracker csrt
  4. # import the necessary packages
  5. from imutils.video import VideoStream
  6. from imutils.video import FPS
  7. import argparse
  8. import imutils
  9. import time
  10. import cv2
  11. # construct the argument parser and parse the arguments
  12. ap = argparse.ArgumentParser()
  13. ap.add_argument("-v", "--video", type=str,default="./race.mp4",help="path to input video file")
  14. ap.add_argument("-t", "--tracker", type=str, default="kcf",help="OpenCV object tracker type")
  15. args = vars(ap.parse_args())
  16. # extract the OpenCV version info
  17. (major, minor) = cv2.__version__.split(".")[:2]
  18. # if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
  19. # function to create our object tracker
  20. if int(minor) < 3:
  21. tracker = cv2.Tracker_create(args["tracker"].upper())
  22. # otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the
  23. # approrpiate object tracker constructor:
  24. else:
  25. # initialize a dictionary that maps strings to their corresponding
  26. # OpenCV object tracker implementations
  27. OPENCV_OBJECT_TRACKERS = {
  28. # "csrt": cv2.TrackerCSRT_create,
  29. "kcf": cv2.TrackerKCF_create,
  30. "boosting": cv2.TrackerBoosting_create,
  31. "mil": cv2.TrackerMIL_create,
  32. "tld": cv2.TrackerTLD_create,
  33. "medianflow": cv2.TrackerMedianFlow_create,
  34. # "mosse": cv2.TrackerMOSSE_create
  35. }
  36. # grab the appropriate object tracker using our dictionary of
  37. # OpenCV object tracker objects
  38. tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
  39. # initialize the bounding box coordinates of the object we are going
  40. # to track
  41. initBB = None
  42. # if a video path was not supplied, grab the reference to the web cam
  43. if not args.get("video", False):
  44. print("[INFO] starting video stream...")
  45. vs = VideoStream(src=0).start()
  46. time.sleep(1.0)
  47. # otherwise, grab a reference to the video file
  48. else:
  49. vs = cv2.VideoCapture(args["video"])
  50. # initialize the FPS throughput estimator
  51. fps = None
  52. # loop over frames from the video stream
  53. while True:
  54. # grab the current frame, then handle if we are using a
  55. # VideoStream or VideoCapture object
  56. frame = vs.read()
  57. frame = frame[1] if args.get("video", False) else frame
  58. # check to see if we have reached the end of the stream
  59. if frame is None:
  60. break
  61. # resize the frame (so we can process it faster) and grab the
  62. # frame dimensions
  63. frame = imutils.resize(frame, width=500)
  64. (H, W) = frame.shape[:2]
  65. # check to see if we are currently tracking an object
  66. if initBB is not None:
  67. # grab the new bounding box coordinates of the object
  68. (success, box) = tracker.update(frame)
  69. # check to see if the tracking was a success
  70. if success:
  71. (x, y, w, h) = [int(v) for v in box]
  72. cv2.rectangle(frame, (x, y), (x + w, y + h),(0, 255, 0), 2)
  73. # update the FPS counter
  74. fps.update()
  75. fps.stop()
  76. # initialize the set of information we'll be displaying on
  77. # the frame
  78. info = [
  79. ("Tracker", args["tracker"]),
  80. ("Success", "Yes" if success else "No"),
  81. ("FPS", "{:.2f}".format(fps.fps())),
  82. ]
  83. # loop over the info tuples and draw them on our frame
  84. for (i, (k, v)) in enumerate(info):
  85. text = "{}: {}".format(k, v)
  86. cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
  87. cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
  88. # show the output frame
  89. cv2.imshow("Frame", frame)
  90. key = cv2.waitKey(1) & 0xFF
  91. # if the 's' key is selected, we are going to "select" a bounding
  92. # box to track
  93. if key == ord("s"):
  94. # select the bounding box of the object we want to track (make
  95. # sure you press ENTER or SPACE after selecting the ROI)
  96. initBB = cv2.selectROI("Frame", frame, fromCenter=False,
  97. showCrosshair=True)
  98. # start OpenCV object tracker using the supplied bounding box
  99. # coordinates, then start the FPS throughput estimator as well
  100. tracker.init(frame, initBB)
  101. fps = FPS().start()
  102. # if the `q` key was pressed, break from the loop
  103. elif key == ord("q"):
  104. break
  105. # if we are using a webcam, release the pointer
  106. if not args.get("video", False):
  107. vs.stop()
  108. # otherwise, release the file pointer
  109. else:
  110. vs.release()
  111. # close all windows
  112. cv2.destroyAllWindows()

kcf的跟踪效果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/864795
推荐阅读
相关标签
  

闽ICP备14008679号