Getting the individual results from the LineCounting object

Hello, good day.

This is with respect to some doubts that I have with respect to the LineCounting mentioned in the following github,

I see that the it has different attributes like left, right, top and bottom. The code example indicates that this can be visualized over the video frame itself.
However, is it possible to know the data type in order to be able to extract this information separately in the code itself ?

Thank you.

Yes, it is posisble. The programmatic access to line count results can be done via line_counts field of result object.

But instead off calling degirum_tools.annotate_video, we need to do regular predict_batch call.

import degirum as dg, degirum_tools

hw_location = "@cloud"
model_zoo_url = "degirum/public"
model_name = "yolo_v5s_coco--512x512_quant_n2x_orca1_1"
video_source = "https://raw.githubusercontent.com/DeGirum/PySDKExamples/main/images/Traffic.mp4"
ann_path = "temp/multi_object_tracking_video_file.mp4"
counting_lines = [(120, 430, 870, 430), (860, 80, 860, 210)]


# load model
model = dg.load_model(
    model_name=model_name, 
    inference_host_address=hw_location,
    zoo_url=model_zoo_url,
    token=degirum_tools.get_token(),
    overlay_color=[(255,0,0)]
)

# create object tracker
tracker = degirum_tools.ObjectTracker(
    class_list=["car"],
    track_thresh=0.35,
    track_buffer=100,
    match_thresh=0.9999,
    trail_depth=20,
    anchor_point=degirum_tools.AnchorPoint.BOTTOM_CENTER,
)

# create line counter
line_counter = degirum_tools.LineCounter(counting_lines)

# attach object tracker and line counter to model
degirum_tools.attach_analyzers(model, [tracker, line_counter])


with degirum_tools.open_video_stream(video_source) as stream:
    for frame, result in enumerate(model.predict_batch(degirum_tools.video_source(stream))):
        for line, count in enumerate(result.line_counts):
            if count.left > 0 or count.right > 0:
                print(f"At frame {frame} line {line} crossing detected: to the left {count.left}, to the right {count.right}")

result.line_counts is a list of either VectorCounts objects or LineCounts objects (depending on how you instantiated your line counter: if absolute_directions is True, then it will be LineCounts, otherwise VectorCounts), one for each line.
VectorCounts object has the following attributes:

  • left: how many times line was crossed from right to left by all objects
  • right: how many times line was crossed from left to right by all objects
  • for_class: a dictionary keyed by class label, containing SingleVectorCounts objects with same left and right attributes.

LineCounts object in addition to left and right, has top and bottom attributes with similar semantic.

Hello @vladk , I appreciate your help, thank you very much.
The above snippet was extremely helpful.

I would like to clarify one more doubts related to the same.
According to some “documentation” obtained through pydoc, I could find the following,

Class to count object tracking trails crossing lines.

Analyzes the object detection result object passed to analyze method and, for each object trail
in the result.trails dictionary, checks if this trail crosses any lines specified by the lines
constructor parameter.

  1. This means that the value of trail_depth parameter provided to the ObjectTracker class when creating the Tracker object will impact the LineCounter, is my understanding correct ?
tracker = degirum_tools.ObjectTracker(
        class_list = ["Car"],
        track_thresh = 0.25,
        track_buffer = 100,
        match_thresh = 0.9999,
        trail_depth = 20,
        anchor_point = degirum_tools.AnchorPoint.BOTTOM_CENTER,
)
  1. Is there any way to select/configure a centroid instead of rectangle for tracking ?

Thank you once again for your time and support.

Yes, it is correct. It must be trail_depth >= 2 so the LineCounter will be able to count line crossings.

Yes. Just pass the following parameter to LineCounter constructor:
anchor_point = degirum_tools.AnchorPoint.CENTER.

Please be advised that similar anchor_point parameter in ObjectTracker only affects the object tracker annotations. ObjectTracker always reports full bbox into the trail. It is LineCounter logic, which decides, which point to use for crossing detection.

1 Like