Skip to content

Commit

Permalink
tracker.py supports arguments for more cli flexibility (#18)
Browse files Browse the repository at this point in the history
* tracking.py now accepts arguments, and it is more configurable from the command line

* README updated with the tracker arguments
  • Loading branch information
gaborvecsei authored Oct 17, 2020
1 parent 3a4b0c1 commit 56490a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
59 changes: 42 additions & 17 deletions examples/tracking.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import argparse
from functools import partial

import cv2

import color_tracker
Expand All @@ -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)
Expand All @@ -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()

0 comments on commit 56490a3

Please sign in to comment.