diff --git a/README.md b/README.md index 9f24178..0809ac3 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ python object-tracker-single.py -v For example, you can use the demo video provided with this code as -- ```shell -python object-tracker-single.py -v demo-video-single.avi +python object-tracker-single.py -v docs/demo/demo-video-single.avi ``` ### Tracking using Live Video @@ -46,14 +46,14 @@ python object-tracker-single.py -d For example, on most systems device id 0 is the webcam attached. ```shell: -python object-tracker-single.py -v docs/demo-single-video.avi +python object-tracker-single.py -v 0 ``` Use the `-l` or `--dispLoc' command line option to display the locations of the tracked object as show below - ```shell: -python object-tracker-single.py -v docs/demo-single-video.avi -l +python object-tracker-single.py -v docs/demo/demo-video-single.avi -l ``` __NOTE__ -- If you want to do multi object tracking code, use the file `object-tracker-multi.py` instead of `object-tracker-single.py`. This is a hack to do multi-object tracking and __hence the code slows down__. diff --git a/get_points.py b/get_points.py index a825d44..b81d852 100644 --- a/get_points.py +++ b/get_points.py @@ -18,27 +18,27 @@ def run(im, multi=False): def callback(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: - if multi == False and len(pts_2) == 1: - print "WARN: Cannot select another object in SINGLE OBJECT TRACKING MODE." - print "Delete the previously selected object using key `d` to mark a new location." - return + if multi == False and len(pts_2) == 1: + print("WARN: Cannot select another object in SINGLE OBJECT TRACKING MODE.") + print("Delete the previously selected object using key `d` to mark a new location.") + return run.mouse_down = True pts_1.append((x, y)) elif event == cv2.EVENT_LBUTTONUP and run.mouse_down == True: run.mouse_down = False pts_2.append((x, y)) - print "Object selected at [{}, {}]".format(pts_1[-1], pts_2[-1]) + print(f"Object selected at [{pts_1[-1]}, {pts_2[-1]}]") elif event == cv2.EVENT_MOUSEMOVE and run.mouse_down == True: im_draw = im.copy() cv2.rectangle(im_draw, pts_1[-1], (x, y), (255,255,255), 3) cv2.imshow(window_name, im_draw) - print "Press and release mouse around the object to be tracked. \n You can also select multiple objects." + print("Press and release mouse around the object to be tracked. \n You can also select multiple objects.") cv2.setMouseCallback(window_name, callback) - print "Press key `p` to continue with the selected points." - print "Press key `d` to discard the last object selected." - print "Press key `q` to quit the program." + print("Press key `p` to continue with the selected points.") + print("Press key `d` to discard the last object selected.") + print("Press key `q` to quit the program.") while True: # Draw the rectangular boxes on the image @@ -58,17 +58,17 @@ def callback(event, x, y, flags, param): return corrected_point elif key == ord('q'): # Press key `q` to quit the program - print "Quitting without saving." + print("Quitting without saving.") exit() elif key == ord('d'): # Press ket `d` to delete the last rectangular region if run.mouse_down == False and pts_1: - print "Object deleted at [{}, {}]".format(pts_1[-1], pts_2[-1]) + print(f"Object deleted at [{pts_1[-1]}, {pts_2[-1]}]") pts_1.pop() pts_2.pop() im_disp = im.copy() else: - print "No object to delete." + print("No object to delete.") cv2.destroyAllWindows() point= [(tl + br) for tl, br in zip(pts_1, pts_2)] corrected_point=check_point(point) @@ -105,7 +105,7 @@ def check_point(points): try: im = cv2.imread(args["imagepath"]) except: - print "Cannot read image and exiting." + print("Cannot read image and exiting.") exit() points = run(im) - print "Rectangular Regions Selected are -> ", points + print("Rectangular Regions Selected are -> ", points) diff --git a/object-tracker-multiple.py b/object-tracker-multiple.py index f86bd03..10e872f 100644 --- a/object-tracker-multiple.py +++ b/object-tracker-multiple.py @@ -10,15 +10,15 @@ def run(source=0, dispLoc=False): # If Camera Device is not opened, exit the program if not cam.isOpened(): - print "Video device or file couldn't be opened" + print("Video device or file couldn't be opened") exit() - print "Press key `p` to pause the video to start tracking" + print("Press key `p` to pause the video to start tracking") while True: # Retrieve an image and Display it. retval, img = cam.read() if not retval: - print "Cannot capture frame device" + print("Cannot capture frame device") exit() if(cv2.waitKey(10)==ord('p')): break @@ -31,7 +31,7 @@ def run(source=0, dispLoc=False): points = get_points.run(img, multi=True) if not points: - print "ERROR: No object to be tracked." + print("ERROR: No object to be tracked.") exit() cv2.namedWindow("Image", cv2.WINDOW_NORMAL) @@ -39,7 +39,7 @@ def run(source=0, dispLoc=False): # Initial co-ordinates of the object to be tracked # Create the tracker object - tracker = [dlib.correlation_tracker() for _ in xrange(len(points))] + tracker = [dlib.correlation_tracker() for _ in range(len(points))] # Provide the tracker the initial position of the object [tracker[i].start_track(img, dlib.rectangle(*rect)) for i, rect in enumerate(points)] @@ -47,10 +47,10 @@ def run(source=0, dispLoc=False): # Read frame from device or file retval, img = cam.read() if not retval: - print "Cannot capture frame device | CODE TERMINATION :( " + print("Cannot capture frame device | CODE TERMINATION :( ") exit() # Update the tracker - for i in xrange(len(tracker)): + for i in range(len(tracker)): tracker[i].update(img) # Get the position of th object, draw a # bounding box around it and display it. @@ -58,11 +58,11 @@ def run(source=0, dispLoc=False): pt1 = (int(rect.left()), int(rect.top())) pt2 = (int(rect.right()), int(rect.bottom())) cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3) - print "Object {} tracked at [{}, {}] \r".format(i, pt1, pt2), - if dispLoc: - loc = (int(rect.left()), int(rect.top()-20)) - txt = "Object tracked at [{}, {}]".format(pt1, pt2) - cv2.putText(img, txt, loc , cv2.FONT_HERSHEY_SIMPLEX, .5, (255,255,255), 1) + print(f"Object {i} tracked at [{pt1}, {pt2}] \r") + # if dispLoc: + loc = (int(rect.left()), int(rect.top()-20)) + txt = f'Object tracked at [{pt1}, {pt2}]' + cv2.putText(img, txt, loc , cv2.FONT_HERSHEY_SIMPLEX, .5, (255,255,255), 1) cv2.namedWindow("Image", cv2.WINDOW_NORMAL) cv2.imshow("Image", img) # Continue until the user presses ESC key diff --git a/object-tracker-single.py b/object-tracker-single.py index c29d7aa..919be8b 100644 --- a/object-tracker-single.py +++ b/object-tracker-single.py @@ -10,15 +10,15 @@ def run(source=0, dispLoc=False): # If Camera Device is not opened, exit the program if not cam.isOpened(): - print "Video device or file couldn't be opened" + print("Video device or file couldn't be opened") exit() - print "Press key `p` to pause the video to start tracking" + print("Press key `p` to pause the video to start tracking") while True: # Retrieve an image and Display it. retval, img = cam.read() if not retval: - print "Cannot capture frame device" + print("Cannot capture frame device") exit() if(cv2.waitKey(10)==ord('p')): break @@ -31,7 +31,7 @@ def run(source=0, dispLoc=False): points = get_points.run(img) if not points: - print "ERROR: No object to be tracked." + print("ERROR: No object to be tracked.") exit() cv2.namedWindow("Image", cv2.WINDOW_NORMAL) @@ -47,7 +47,7 @@ def run(source=0, dispLoc=False): # Read frame from device or file retval, img = cam.read() if not retval: - print "Cannot capture frame device | CODE TERMINATING :(" + print("Cannot capture frame device | CODE TERMINATING :(") exit() # Update the tracker tracker.update(img) @@ -57,7 +57,7 @@ def run(source=0, dispLoc=False): pt1 = (int(rect.left()), int(rect.top())) pt2 = (int(rect.right()), int(rect.bottom())) cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3) - print "Object tracked at [{}, {}] \r".format(pt1, pt2), + print (f"Object tracked at [{pt1}, {pt2}] \r") if dispLoc: loc = (int(rect.left()), int(rect.top()-20)) txt = "Object tracked at [{}, {}]".format(pt1, pt2)