├── README.md ├── index.py └── videos └── chaplin.mp4 /README.md: -------------------------------------------------------------------------------- 1 | # OpenCV-Object-Tracking 2 | 3 | You can read my article here 4 | 5 | [https://ehsangazar.com/object-tracking-with-opencv-fd18ccdd7369](https://ehsangazar.com/object-tracking-with-opencv-fd18ccdd7369) 6 | 7 | ## What is Object Tracking ? 8 | 9 | Simply put, locating an object in successive frames of a video is called tracking. 10 | Usually tracking algorithms are faster than detection algorithms. The reason is simple. When you are tracking an object that was detected in the previous frame, you know a lot about the appearance of the object. 11 | You also know the location in the previous frame and the direction and speed of its motion. 12 | If you are running a face detector on a video and the person's face get's occluded by an object, the face detector will most likely fail. A good tracking algorithm, on the other hand, will handle some level of occlusion. 13 | One of the famous libraries for tracking is OpenCV.  14 | OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision.[1] Originally developed by Intel, 15 | 16 | ## How to install OpenCV on Mac OS?  17 | Simple, you have probably python installed, so use brew to install opencv. 18 | brew install opencv 19 | pip3 install numpy 20 | Then I used this video which is a short cut of Chaplin for doing object tracking, I am trying to track his face while he is dancing and turning around. 21 | Normally the objects we are tracking would not be disappeared, but in this case for comparing different methods provided by OpenCV, I used this video. 22 | Firstly importing cv2 23 | ``` 24 | import cv2 25 | ``` 26 | Then,  27 | ``` 28 | tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'CSRT', 'MOSSE'] 29 | ``` 30 | 31 | ## Conclusion 32 | If you are looking for solving tracking object in videos, OpenCV is one of the best, there are different algorithms which based on you scenario might work better. -------------------------------------------------------------------------------- /index.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import sys 3 | 4 | (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') 5 | 6 | if __name__ == '__main__' : 7 | 8 | # Set up tracker. 9 | # Instead of MIL, you can also use 10 | 11 | tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'CSRT', 'MOSSE'] 12 | tracker_type = tracker_types[5] 13 | 14 | if int(minor_ver) < 3: 15 | tracker = cv2.Tracker_create(tracker_type) 16 | else: 17 | if tracker_type == 'BOOSTING': 18 | tracker = cv2.TrackerBoosting_create() 19 | if tracker_type == 'MIL': 20 | tracker = cv2.TrackerMIL_create() 21 | if tracker_type == 'KCF': 22 | tracker = cv2.TrackerKCF_create() 23 | if tracker_type == 'TLD': 24 | tracker = cv2.TrackerTLD_create() 25 | if tracker_type == 'MEDIANFLOW': 26 | tracker = cv2.TrackerMedianFlow_create() 27 | if tracker_type == 'CSRT': 28 | tracker = cv2.TrackerCSRT_create() 29 | if tracker_type == 'MOSSE': 30 | tracker = cv2.TrackerMOSSE_create() 31 | 32 | # Read video 33 | video = cv2.VideoCapture("./videos/chaplin.mp4") 34 | 35 | # Exit if video not opened. 36 | if not video.isOpened(): 37 | print("Could not open video") 38 | sys.exit() 39 | 40 | # Read first frame. 41 | ok, frame = video.read() 42 | if not ok: 43 | print('Cannot read video file') 44 | sys.exit() 45 | 46 | # Define an initial bounding box 47 | bbox = (287, 23, 86, 320) 48 | 49 | # Uncomment the line below to select a different bounding box 50 | bbox = cv2.selectROI(frame, False) 51 | 52 | # Initialize tracker with first frame and bounding box 53 | ok = tracker.init(frame, bbox) 54 | 55 | while True: 56 | # Read a new frame 57 | ok, frame = video.read() 58 | if not ok: 59 | break 60 | 61 | # Start timer 62 | timer = cv2.getTickCount() 63 | 64 | # Update tracker 65 | ok, bbox = tracker.update(frame) 66 | 67 | # Calculate Frames per second (FPS) 68 | fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer); 69 | 70 | # Draw bounding box 71 | if ok: 72 | # Tracking success 73 | p1 = (int(bbox[0]), int(bbox[1])) 74 | p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) 75 | cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1) 76 | else : 77 | # Tracking failure 78 | cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2) 79 | 80 | # Display tracker type on frame 81 | cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2); 82 | 83 | # Display FPS on frame 84 | cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2); 85 | 86 | # Display result 87 | cv2.imshow("Tracking", frame) 88 | 89 | # Exit if ESC pressed 90 | k = cv2.waitKey(1) & 0xff 91 | if k == 27 : break -------------------------------------------------------------------------------- /videos/chaplin.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ehsangazar/opencv-object-tracking/b3b26314cf1cdd0bfd3f7d730308daa503d7dc71/videos/chaplin.mp4 --------------------------------------------------------------------------------