<jupyter_start><jupyter_text>OpenCV Object Tracking In last week’s blog post we got our feet wet by implementing a simple object tracking algorithm called “centroid tracking”. Today, we are going to take the next step and look at eight separate object tracking algorithms built right into OpenCV!You see, while our centroid tracker worked well, it required us to run an actual object detector on each frame of the input video. For the vast majority of circumstances, having to run the detection phase on each and every frame is undesirable and potentially computationally limiting.Instead, we would like to apply object detection only once and then have the object tracker be able to handle every subsequent frame, leading to a faster, more efficient object tracking pipeline. The question is — can OpenCV help us achieve such object tracking?The answer is undoubtedly a resounding “Yes”.To learn how to apply object tracking using OpenCV’s built-in object trackers, just keep reading. In the first part of today’s blog post, we are going to briefly review the eight object tracking algorithms built-in to OpenCV.From there I’ll demonstrate how we can use each of these object trackers in real-time.Finally, we’ll review the results of each of OpenCV’s object trackers, noting which ones worked under what situations and which ones didn’t.Let’s go ahead and get started tracking objects with OpenCV!  8 OpenCV Object Tracking ImplementationsYou might be surprised to know that OpenCV includes eight (yes, eight!) separate object tracking implementations that you can use in your own computer vision applications.I’ve included a brief highlight of each object tracker below:- **BOOSTING Tracker**: Based on the same algorithm used to power the machine learning behind Haar cascades (AdaBoost), but like Haar cascades, is over a decade old. This tracker is slow and doesn’t work very well. Interesting only for legacy reasons and comparing other algorithms. (minimum OpenCV 3.0.0)- **MIL Tracker**: Better accuracy than BOOSTING tracker but does a poor job of reporting failure. (minimum OpenCV 3.0.0)- **KCF Tracker**: Kernelized Correlation Filters. Faster than BOOSTING and MIL. Similar to MIL and KCF, does not handle full occlusion well. (minimum OpenCV 3.1.0)- **CSRT Tracker**: Discriminative Correlation Filter (with Channel and Spatial Reliability). Tends to be more accurate than KCF but slightly slower. (minimum OpenCV 3.4.2)- **MedianFlow Tracker**: Does a nice job reporting failures; however, if there is too large of a jump in motion, such as fast moving objects, or objects that change quickly in their appearance, the model will fail. (minimum OpenCV 3.0.0)- **TLD Tracker**: I’m not sure if there is a problem with the OpenCV implementation of the TLD tracker or the actual algorithm itself, but the TLD tracker was incredibly prone to false-positives. I do not recommend using this OpenCV object tracker. (minimum OpenCV 3.0.0)- **MOSSE Tracker**: Very, very fast. Not as accurate as CSRT or KCF but a good choice if you need pure speed. (minimum OpenCV 3.4.1)- **GOTURN Tracker**: The only deep learning-based object detector included in OpenCV. It requires additional model files to run (will not be covered in this post). My initial experiments showed it was a bit of a pain to use even though it reportedly handles viewing changes well (my initial experiments didn’t confirm this though). I’ll try to cover it in a future post, but in the meantime, take a look at Satya’s writeup. (minimum OpenCV 3.2.0)  My personal suggestion is to:- Use CSRT when you need higher object tracking accuracy and can tolerate slower FPS throughput- Use KCF when you need faster FPS throughput but can handle slightly lower object tracking accuracy- Use MOSSE when you need pure speed Satya Mallick also provides some additional information on these object trackers in his article as well.Object Trackers have been in active development in OpenCV 3. Here is a brief summary of which versions of OpenCV the trackers appear in: Now that you’ve had a brief overview of each of the object trackers, let’s get down to business!Object Tracking with OpenCVTo perform object tracking using OpenCV, open up a new file, name it opencv_object_tracker.py , and insert the following code:<jupyter_code># import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import argparse
import imutils
import time
import cv2
# extract the OpenCV version info
(major, minor) = cv2.__version__.split(".")[:2]
print('major=', major,'minor=',minor)
# initialize the bounding box coordinates of the object we are going
# to track
initBB = None
# initialize the FPS throughput estimator
fps = None

cap = cv2.VideoCapture(0)
# initialize the first frame in the video stream
firstFrame = None

# OPENCV_OBJECT_TRACKERS = {
#     "csrt": cv2.TrackerCSRT_create,
#     "kcf": cv2.TrackerKCF_create,
#     "boosting": cv2.TrackerBoosting_create,
#     "mil": cv2.TrackerMIL_create,
#     "tld": cv2.TrackerTLD_create,
#     "medianflow": cv2.TrackerMedianFlow_create,
#     "mosse": cv2.TrackerMOSSE_create
# }
 
# grab the appropriate object tracker using our dictionary of
# OpenCV object tracker objects
tracker = cv2.TrackerKCF_create()
# loop over frames from the video stream
while True:
    # grab the current frame and initialize the occupied/unoccupied
    ret, frame = cap.read()
    
    # check to see if we have reached the end of the stream
    if frame is None:
        break

    # resize the frame (so we can process it faster) and grab the
    # frame dimensions
    frame = imutils.resize(frame, width=500)
    (H, W) = frame.shape[:2]

    # check to see if we are currently tracking an object
    if initBB is not None:
        # grab the new bounding box coordinates of the object
        (success, box) = tracker.update(frame)

        # check to see if the tracking was a success
        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)

        # update the FPS counter
        fps.update()
        fps.stop()

        # initialize the set of information we'll be displaying on
        # the frame
        info = [("Tracker", args["tracker"]),
                ("Success", "Yes" if success else "No"),("FPS", "{:.2f}".format(fps.fps())),]

        # loop over the info tuples and draw them on our frame
        for (i, (k, v)) in enumerate(info):
            text = "{}: {}".format(k, v)
            cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
        
    # show the output frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    # if the 's' key is selected, we are going to "select" a bounding
    # box to track
    if key == ord("s"):
        # select the bounding box of the object we want to track (make
        # sure you press ENTER or SPACE after selecting the ROI)
        initBB = cv2.selectROI("Frame", frame, fromCenter=False,
                               showCrosshair=True)
        # start OpenCV object tracker using the supplied bounding box
        # coordinates, then start the FPS throughput estimator as well
        tracker.init(frame, initBB)
        fps = FPS().start()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0696090450892249, 'fraction_numerical': 0.009807928075194115, 'mean_word_length': 3.6586294416243654, 'pattern_counts': {'":': 7, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '110354', 'license': 'MIT', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1912, 'n_words': 1018, 'path': 'OpenCV/ObjectTrackingAlgo.ipynb', 'repo_name': 'erkundanec/BasicPython', 'size': '7341'}