This is my approach to counting objects with OpenCV + a dash of ROS2.
I wanted to use a video feed and figure out how many humans were occupying a space. Rather than create a standalone project I chose to pivot off an already existing one, rosVid.
There are two ROS2 nodes in this project, objCounter and webCounter. objCounter performs the heavy lifting and webCounter serves as the front-end.
The current assumption is that you are using rosVid and publishing to the /captures/phys/raw topic with a video feed of your choice.
Run objCounter like so:
source ros2_ws/install/setup.bash
ros2 run obj_counter objCounter --ros-args \
-p model:=./detections/ssd_mobilenet_v2_coco_quant_postprocess.tflite \
-p labelsFile:=./detections/labels.txt \
-p capture:=phys \
-p maxFps:=10 \
-p dbPath:=./door_counter.sqlite3 \
-p triggerMode:=center \
-p showCountsOverlay:=False
In another shell run the web server:
source ros2_ws/install/setup.bash
ros2 run obj_counter webCounter
Open a browser and visit the web app.
webCounter has two browser video delivery modes:
- Default: the page refreshes repeated single JPEG snapshots from
/frame.jpg. This is the most compatible mode and has proven more reliable in Windows Chrome and Edge. - MJPEG: the page uses
/video.mjpg, a single long-lived multipart response. This usually has less HTTP request overhead, but some browsers may show a black video area even though stats continue to update.
If your browser handles multipart MJPEG well, you can switch to the MJPEG mode:
ros2 run obj_counter webCounter --ros-args -p useMjpeg:=True