diff --git a/README.md b/README.md index 4efc92f..0cda346 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,26 @@ You will need the following packages: ## Object Tracker -- Check out the **[examples folder](examples)**, or go straight to the **[sample tracking app](examples/tracking.py)** which is an extended version of the script below +- Check out the **[examples folder](examples)**, or go straight to the **[sample tracking app](examples/tracking.py)** which is an extended version of the script below. +This script tracks the red-ish objects, if you'd like to track another color, then start with the `hsv_color_detector.py` script ``` python - python examples/tracking.py + $ python examples/tracking.py --help + + + usage: tracking.py [-h] [-low LOW LOW LOW] [-high HIGH HIGH HIGH] + [-c CONTOUR_AREA] [-v] + + optional arguments: + -h, --help show this help message and exit + -low LOW LOW LOW, --low LOW LOW LOW + Lower value for the HSV range. Default = 155, 103, 82 + -high HIGH HIGH HIGH, --high HIGH HIGH HIGH + Higher value for the HSV range. Default = 178, 255, + 255 + -c CONTOUR_AREA, --contour-area CONTOUR_AREA + Minimum object contour area. This controls how small + objects should be detected. Default = 2500 + -v, --verbose ``` - Simple script: diff --git a/examples/tracking.py b/examples/tracking.py index e9c92be..70c0bd4 100644 --- a/examples/tracking.py +++ b/examples/tracking.py @@ -1,3 +1,6 @@ +import argparse +from functools import partial + import cv2 import color_tracker @@ -7,7 +10,20 @@ HSV_UPPER_VALUE = [178, 255, 255] -def tracking_callback(tracker: color_tracker.ColorTracker): +def get_args(): + parser = argparse.ArgumentParser() + parser.add_argument("-low", "--low", nargs=3, type=int, default=HSV_LOWER_VALUE, + help="Lower value for the HSV range. Default = 155, 103, 82") + parser.add_argument("-high", "--high", nargs=3, type=int, default=HSV_UPPER_VALUE, + help="Higher value for the HSV range. Default = 178, 255, 255") + parser.add_argument("-c", "--contour-area", type=float, default=2500, + help="Minimum object contour area. This controls how small objects should be detected. Default = 2500") + parser.add_argument("-v", "--verbose", action="store_true") + args = parser.parse_args() + return args + + +def tracking_callback(tracker: color_tracker.ColorTracker, verbose: bool = True): # Visualizing the original frame and the debugger frame cv2.imshow("original frame", tracker.frame) cv2.imshow("debug frame", tracker.debug_frame) @@ -17,24 +33,33 @@ def tracking_callback(tracker: color_tracker.ColorTracker): if key == 27: tracker.stop_tracking() - for obj in tracker.tracked_objects: - print("Object {0} center {1}".format(obj.id, obj.last_point)) + if verbose: + for obj in tracker.tracked_objects: + print("Object {0} center {1}".format(obj.id, obj.last_point)) + + +def main(): + args = get_args() + + # Creating a kernel for the morphology operations + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) + # Init the ColorTracker object + tracker = color_tracker.ColorTracker(max_nb_of_objects=5, max_nb_of_points=20, debug=True) -# Creating a kernel for the morphology operations -kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) + # Setting a callback which is called at every iteration + callback = partial(tracking_callback, verbose=args.verbose) + tracker.set_tracking_callback(tracking_callback=callback) -# Init the ColorTracker object -tracker = color_tracker.ColorTracker(max_nb_of_objects=5, max_nb_of_points=20, debug=True) + # Start tracking with a camera + with color_tracker.WebCamera(video_src=0) as webcam: + # Start the actual tracking of the object + tracker.track(webcam, + hsv_lower_value=args.low, + hsv_upper_value=args.high, + min_contour_area=args.contour_area, + kernel=kernel) -# Setting a callback which is called at every iteration -tracker.set_tracking_callback(tracking_callback=tracking_callback) -# Start tracking with a camera -with color_tracker.WebCamera(video_src=0) as webcam: - # Start the actual tracking of the object - tracker.track(webcam, - hsv_lower_value=HSV_LOWER_VALUE, - hsv_upper_value=HSV_UPPER_VALUE, - min_contour_area=2500, - kernel=kernel) +if __name__ == "__main__": + main()