diff --git a/get_points.py b/get_points.py index a825d44..574c41c 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("Object selected at [{}, {}]".format(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 @@ -49,7 +49,7 @@ def callback(event, x, y, flags, param): # Display the cropped images cv2.namedWindow(window_name_2, cv2.WINDOW_NORMAL) cv2.imshow(window_name_2, im_disp) - key = cv2.waitKey(30) + key = (0xFF & cv2.waitKey(30)) if key == ord('p'): # Press key `s` to return the selected points cv2.destroyAllWindows() @@ -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("Object deleted at [{}, {}]".format(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) \ No newline at end of file diff --git a/object-tracker-multiple.py b/object-tracker-multiple.py index f86bd03..9cd0e9c 100644 --- a/object-tracker-multiple.py +++ b/object-tracker-multiple.py @@ -10,17 +10,17 @@ 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')): + if(0xFF & cv2.waitKey(10))==ord('p'): break cv2.namedWindow("Image", cv2.WINDOW_NORMAL) cv2.imshow("Image", img) @@ -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,15 +58,15 @@ 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), + print("Object {} tracked at [{}, {}] \r".format(i, pt1, pt2), end=' ') 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) + txt = "Object tracked at [{}, {}]".format(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 - if cv2.waitKey(1) == 27: + if (0xFF & cv2.waitKey(1)) == 27: break # Relase the VideoCapture object @@ -86,4 +86,4 @@ def run(source=0, dispLoc=False): source = args["videoFile"] else: source = int(args["deviceID"]) - run(source, args["dispLoc"]) + run(source, args["dispLoc"]) \ No newline at end of file diff --git a/object-tracker-single.py b/object-tracker-single.py index c29d7aa..728b933 100644 --- a/object-tracker-single.py +++ b/object-tracker-single.py @@ -10,17 +10,17 @@ 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')): + if(0xFF & cv2.waitKey(10))==ord('p'): break cv2.namedWindow("Image", cv2.WINDOW_NORMAL) cv2.imshow("Image", img) @@ -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("Object tracked at [{}, {}] \r".format(pt1, pt2), end=' ') if dispLoc: loc = (int(rect.left()), int(rect.top()-20)) txt = "Object tracked at [{}, {}]".format(pt1, pt2) @@ -65,7 +65,7 @@ def run(source=0, dispLoc=False): cv2.namedWindow("Image", cv2.WINDOW_NORMAL) cv2.imshow("Image", img) # Continue until the user presses ESC key - if cv2.waitKey(1) == 27: + if (0xFF & cv2.waitKey(1)) == 27: break # Relase the VideoCapture object @@ -85,4 +85,4 @@ def run(source=0, dispLoc=False): source = args["videoFile"] else: source = int(args["deviceID"]) - run(source, args["dispLoc"]) + run(source, args["dispLoc"]) \ No newline at end of file