# How to count detected classes in a video using detectron2 and supervision

**URL:** <https://discuss.roboflow.com/t/how-to-count-detected-classes-in-a-video-using-detectron2-and-supervision/5257>\
**Category:** 🤝  Community Help\
**Created:** [April 11, 2024, 11:17pm UTC](https://discuss.roboflow.com/t/how-to-count-detected-classes-in-a-video-using-detectron2-and-supervision/5257 "2024-04-11T23:17:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamal\_Moha](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/kamal_moha/32/3427_2.png) [@Kamal\_Moha](https://discuss.roboflow.com/u/Kamal_Moha)\
**Post date:** [April 11, 2024, 11:17pm UTC](https://discuss.roboflow.com/t/how-to-count-detected-classes-in-a-video-using-detectron2-and-supervision/5257/1 "2024-04-11T23:17:29Z")

</div>

I have created a video object detection model using detectron2. My detectron2 model is able to properly do inference in the video.  
But I would like to now count the number of detected classes as the video progresses.

I have tried exploring [supervision](https://github.com/roboflow/supervision/), but would like to get help on how to implement this.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![leo](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/leo/32/2228_2.png) [@leo](https://discuss.roboflow.com/u/leo)\
**Post date:** [April 12, 2024, 1:36pm UTC](https://discuss.roboflow.com/t/how-to-count-detected-classes-in-a-video-using-detectron2-and-supervision/5257/2 "2024-04-12T13:36:54Z")

</div>

Hey @Kamal_Moha

Could you clarify where you are at the moment? If you got your detections into Supervision, you could just find the length of the detections object:

```python
# detections = sv.Detections()...
print(len(detections)) # Will print the number of detected objects

```

---

<div class="post-metadata">

**Author:** ![Kamal\_Moha](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/kamal_moha/32/3427_2.png) [@Kamal\_Moha](https://discuss.roboflow.com/u/Kamal_Moha)\
**Post date:** [April 13, 2024, 7:11pm UTC](https://discuss.roboflow.com/t/how-to-count-detected-classes-in-a-video-using-detectron2-and-supervision/5257/3 "2024-04-13T19:11:51Z")

</div>

Hi @leo, I have managed to print the len of detections for each frame.

```auto
import numpy as np
import supervision as sv

video_info = sv.VideoInfo.from_video_path(SUBWAY_VIDEO_PATH)
# line_zone = sv.LineZone(sv.Point(600, 0), sv.Point(600, 300))
# initiate polygon zone
polygon = np.array([
    [44, 22],
    [52, 710],
    [1288, 714],
    [1292, 22],
    [48, 18]
])

zone = sv.PolygonZone(polygon=polygon, frame_resolution_wh=video_info.resolution_wh)

# initiate annotators
box_annotator = sv.BoundingBoxAnnotator(thickness=4)
zone_annotator = sv.PolygonZoneAnnotator(zone=zone, color=sv.Color.white(), 
                                         thickness=6, text_thickness=6, text_scale=4)

def process_frame(frame: np.ndarray, i: int) -> np.ndarray:
    print('frame', i)
    # extract video frame
    generator = sv.get_video_frames_generator(SUBWAY_VIDEO_PATH)
    iterator = iter(generator)
    frame = next(iterator)

    # detect
    outputs = predictor(frame)
    all_detections = sv.Detections.from_detectron2(outputs)
    #filtering for 'person' detection
    detections = all_detections[all_detections.class_id == 0]
    zone.trigger(detections=detections)

    # annotate
    box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
    frame = box_annotator.annotate(scene=frame, detections=detections, skip_label=True)
    frame = zone_annotator.annotate(scene=frame)
    return frame

sv.process_video(source_path=SUBWAY_VIDEO_PATH, target_path=f"new-result.mp4", 
                 callback=process_frame)

```

Running the above code in Kaggle notebooks produces the below video frame;

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/8/85f63c4be300b7e03a524d3211da898c6283bcb6.jpeg)

The video it produces has the below issues that I would like help on;

1. `sv.process_video()` has only made predictions on the first frame neglecting the other frames. And by the way the original video is 6sec long with 181 frames. When I open the newly created video, it definitely doesn’t show other frames and it’s not a continuous video
2. How can I widen the polygon size(coordinates) so that I can create a polygon for the whole frame. As you can see from the above pic, the polygon shape is in white color. I want to enlarge that so that I can detect more objects in the frame

Thanks and I would like to get your support on this.

---

<div class="post-metadata">

**Author:** ![leo](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/leo/32/2228_2.png) [@leo](https://discuss.roboflow.com/u/leo)\
**Post date:** [April 21, 2024, 9:07pm UTC](https://discuss.roboflow.com/t/how-to-count-detected-classes-in-a-video-using-detectron2-and-supervision/5257/4 "2024-04-21T21:07:36Z")

</div>

Hey @Kamal_Moha

> [@Kamal\_Moha](#):
>
> `sv.process_video()` has only made predictions on the first frame neglecting the other frames. And by the way the original video is 6sec long with 181 frames. When I open the newly created video, it definitely doesn’t show other frames and it’s not a continuous video

The Supervision `process_video` function provides the video frame and video frame number to the callback, in this case, your `process_frame` function. Within this function, you should not need to modify the `frame` variable, yet within your code, you create a Supervision frame generator on every iteration of your video, which returns the first frame.

What that means is this portion of your code is unnecessary and overwrites the frame variable with the first frame, regardless of when it actually is.

```auto
    # extract video frame
    generator = sv.get_video_frames_generator(SUBWAY_VIDEO_PATH)
    iterator = iter(generator)
    frame = next(iterator)

```

> [@Kamal\_Moha](#):
>
> How can I widen the polygon size(coordinates) so that I can create a polygon for the whole frame. As you can see from the above pic, the polygon shape is in white color. I want to enlarge that so that I can detect more objects in the frame

It’s not completely clear to me what you are trying to accomplish here, but you can change the polygon size where you declare `polygon`:

```auto
polygon = np.array([
    [44, 22], # Change these to different (x, y) coordinates of the frame
    [52, 710],
    [1288, 714],
    [1292, 22],
    [48, 18]
])

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/bb6edbfa56d0cf2ee603f061e9e09103502cc8cc.png) [@system](https://discuss.roboflow.com/u/system)\
**Post date:** [May 12, 2024, 9:07pm UTC](https://discuss.roboflow.com/t/how-to-count-detected-classes-in-a-video-using-detectron2-and-supervision/5257/5 "2024-05-12T21:07:38Z")

</div>

Closed on May 12, 2024, 9:07pm UTC

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
