# Task 06: ROI (Region of Interest) Detection Using Python ## Task Title ROI (Region of Interest) Detection Using Python ## Description Implement and test the `06_roi_detection.py` script on the Jetson Orin Nano. Runs YOLO26n detection but only reports and counts objects that are inside a user-defined region of interest. The region is drawn on the frame and objects outside it are ignored. ## When to Use Use this for zone monitoring, such as counting people at a door, checking a restricted area, or watching one machine or shelf. ## Hardware and Software - Device: NVIDIA Jetson Orin Nano (JetPack with CUDA and TensorRT) - Camera: USB webcam (for example Logitech BRIO 100) at CAMERA_INDEX 0 - Language: Python 3 - Model: YOLO26 (Ultralytics), weights `yolo26n.pt` - Libraries: ultralytics, opencv-python, numpy ## Installation ``` pip install ultralytics opencv-python ``` On Jetson, install the JetPack-matched PyTorch and torchvision builds first (see the Ultralytics Jetson guide in the references). Model weights download automatically on first run if the machine is online. ## Command to Run ``` python3 06_roi_detection.py ``` Check the camera is visible before running: ``` ls /dev/video* ``` ## How It Works 1. Load the YOLO26n model. 2. Define the ROI (rectangle or polygon) in the script settings. 3. Run detection on each frame. 4. Check whether each box centre falls inside the ROI. 5. Draw only the objects inside the ROI, show the in-zone count and FPS. Press Q to quit. ## Settings You Can Change Edit these values at the top of the script: - ROI coordinates (rectangle or polygon points) : the zone to watch - CAMERA_INDEX, FRAME_W, FRAME_H : camera options - CONF_THRESHOLD / IOU_THRESHOLD : detection thresholds - MODEL_WEIGHTS = yolo26n.pt ## Controls - Press `Q` to quit the live window ## Verify the Output Run `python3 06_roi_detection.py` and confirm: - [ ] The ROI outline is visible on the live window. - [ ] Objects inside the ROI get boxes and are counted. - [ ] Objects outside the ROI are not drawn or counted. - [ ] The count updates as objects enter or leave; Q closes the window. ## Optional: Faster Inference with TensorRT For higher FPS on the Orin Nano, export the model to a TensorRT engine once and point `MODEL_WEIGHTS` to the engine file: ``` yolo export model=yolo26n.pt format=engine half=True ``` Then set `MODEL_WEIGHTS = 'yolo26n.engine'` in the script. ## Troubleshooting - Camera not opening: check `ls /dev/video*`, try CAMERA_INDEX 1, and make sure no other app is using the camera. - Window does not show over SSH: run on the Jetson desktop, or use X11 forwarding. - Low FPS: use the nano model, lower FRAME_W / FRAME_H, run `sudo nvpmodel -m 0` and `sudo jetson_clocks`, or export to TensorRT. - Weights not found: connect to the internet for the first run or copy the .pt file next to the script. - Out of memory: close other apps and reduce the input size. ## How This Differs From the Other Tasks Same detector as 01 but limited to a chosen zone. Files in this folder: 01_detection, 02_tracking, 03_segmentation, 04_pose_estimation, 05_classification, 06_roi_detection, 07_obb_detection, 08_depth_estimation. ## References - [Object counting in regions](https://docs.ultralytics.com/guides/region-counting/) - [Ultralytics Solutions](https://docs.ultralytics.com/solutions/) - [Object detection task docs](https://docs.ultralytics.com/tasks/detect/) - [OpenCV drawing functions](https://docs.opencv.org/4.x/dc/da5/tutorial_py_drawing_functions.html) - [Ultralytics on NVIDIA Jetson guide](https://docs.ultralytics.com/guides/nvidia-jetson/) - [TensorRT export](https://docs.ultralytics.com/integrations/tensorrt/) - [Ultralytics GitHub](https://github.com/ultralytics/ultralytics) - [JetPack SDK](https://developer.nvidia.com/embedded/jetpack)