thumbnail">
With a model hosted on Roboflow like the ones above and the open source supervision Python package, you can count tops in your images and videos.
The following code snippet counts the number of tops present in an image.
To use the snippet below, you will need to run pip install roboflow supervision
. Replace the project name and model name with any model trained on Universe, such as those listed above.
import supervision as sv
import roboflow
roboflow.login()
rf = roboflow.Roboflow()
# replace with the top project you choose above
project = rf.workspace("smartcart-orubg").project("mini-market-items")
top_model = project.version(5).model
results = top_model.predict("top.jpg").json()
tops = sv.Detections.from_roboflow(results)
# print number of tops
print(len(tops))
With a bit more code, you can count the number of top present in a specific zone of your image or video.
The following code snippet counts the number of top present in each frame in a video.
To use the snippet below, you will need to run pip install roboflow supervision
. Replace the project name and model name with any model trained on Universe, such as those listed above.
Read our blog post on counting objects in a zone
import numpy as np
import supervision as sv
import roboflow
SOURCE_VIDEO_PATH = "top.mp4"
TARGET_VIDEO_PATH = "top_out.mp4"
# use https://roboflow.github.io/polygonzone/ to get the points for your shape
polygon = np.array([
# draw 50x50 box in top left corner
[0, 0],
[50, 0],
[50, 50],
[0, 50]
])
roboflow.login()
rf = roboflow.Roboflow()
# replace with the top project you choose above
project = rf.workspace("smartcart-orubg").project("mini-market-items")
top_model = project.version(5).model
# create BYTETracker instance
top_tracker = sv.ByteTrack(track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=30)
# create VideoInfo instance
video_info = sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)
# create frame generator
generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
# create PolygonZone instance
zone = sv.PolygonZone(polygon=polygon, frame_resolution_wh=(video_info.width, video_info.height))
# create box annotator
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
colors = sv.ColorPalette.default()
# create instance of BoxAnnotator
zone_annotator = sv.PolygonZoneAnnotator(thickness=4, text_thickness=4, text_scale=2, zone=zone, color=colors.colors[0])
# define call back function to be used in video processing
def callback(frame: np.ndarray, index:int) -> np.ndarray:
# model prediction on single frame and conversion to supervision Detections
results = top_model.predict(frame).json()
tops = sv.Detections.from_roboflow(results)
# show top detections in real time
print(tops)
# tracking top detections
tops = top_tracker.update_with_detections(tops)
annotated_frame = box_annotator.annotate(scene=frame, detections=tops)
annotated_frame = zone_annotator.annotate(scene=annotated_frame)
# return frame with box and line annotated result
return annotated_frame
# process the whole video
sv.process_video(
source_path = SOURCE_VIDEO_PATH,
target_path = TARGET_VIDEO_PATH,
callback=callback
)
You can count how many tops have crossed a line using the supervision LineCounter
method.
The following code snippet counts the number of tops that cross a line in a video.
To use the snippet below, you will need to run pip install roboflow supervision
. Replace the project name and model name with any model trained on Universe, such as those listed above.
import numpy as np
import supervision as sv
import roboflow
SOURCE_VIDEO_PATH = "top.mp4"
TARGET_VIDEO_PATH = "top_out.mp4"
# use https://roboflow.github.io/polygonzone/ to get the points for your line
LINE_START = sv.Point(0, 300)
LINE_END = sv.Point(800, 300)
roboflow.login()
rf = roboflow.Roboflow()
# replace with the top project you choose above
project = rf.workspace("smartcart-orubg").project("mini-market-items")
top_model = project.version(5).model
# create BYTETracker instance
top_tracker = sv.ByteTrack(track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=30)
# create VideoInfo instance
video_info = sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)
# create frame generator
generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
# create LineZone instance, it is previously called LineCounter class
line_zone = sv.LineZone(start=LINE_START, end=LINE_END)
# create instance of BoxAnnotator
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
# create instance of TraceAnnotator
trace_annotator = sv.TraceAnnotator(thickness=4, trace_length=50)
line_zone_annotator = sv.LineZoneAnnotator(thickness=4, text_thickness=4, text_scale=2)
# define call back function to be used in video processing
def callback(frame: np.ndarray, index:int) -> np.ndarray:
# model prediction on single frame and conversion to supervision Detections
results = top_model.predict(frame).json()
tops = sv.Detections.from_roboflow(results)
# show top detections in real time
print(tops)
# tracking top detections
tops = top_tracker.update_with_detections(tops)
annotated_frame = trace_annotator.annotate(
scene=frame.copy(),
detections=tops
)
annotated_frame=box_annotator.annotate(
scene=annotated_frame,
detections=tops
)
# update line counter
line_zone.trigger(tops)
# return frame with box and line annotated result
return line_zone_annotator.annotate(annotated_frame, line_counter=line_zone)
# process the whole video
sv.process_video(
source_path = SOURCE_VIDEO_PATH,
target_path = TARGET_VIDEO_PATH,
callback=callback
)