Files
Project_documents/03_segmentation_README.md
2026-11-23 11:42:41 +05:30

106 lines
3.7 KiB
Markdown

# Task 03: Instance Segmentation Using Python
## Task Title
Instance Segmentation Using Python
## Description
Implement and test the `03_segmentation.py` script on the Jetson Orin Nano. Runs YOLO26n-seg to find objects and draw their exact pixel outline (mask) in addition to the box, class name and confidence.
## When to Use
Use this when a rectangle is not precise enough, for example measuring object area, cutting an object out of the background, or separating touching objects.
## 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-seg.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 03_segmentation.py
```
Check the camera is visible before running:
```
ls /dev/video*
```
## How It Works
1. Load the segmentation weights (yolo26n-seg.pt).
2. Open the camera feed.
3. Run the model on each frame.
4. Read the masks from the result and overlay them as coloured regions on the frame.
5. Draw labels, FPS and object count. Press Q to quit.
## Settings You Can Change
Edit these values at the top of the script:
- CAMERA_INDEX, FRAME_W, FRAME_H : camera options
- CONF_THRESHOLD / IOU_THRESHOLD : detection thresholds
- MODEL_WEIGHTS = yolo26n-seg.pt
- Mask transparency / colour options, if defined in the script
## Controls
- Press `Q` to quit the live window
## Verify the Output
Run `python3 03_segmentation.py` and confirm:
- [ ] A live window opens with coloured mask overlays on objects.
- [ ] Mask edges follow the object shape, not just a rectangle.
- [ ] FPS is lower than detection (masks cost more); 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-seg.pt format=engine half=True
```
Then set `MODEL_WEIGHTS = 'yolo26n-seg.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
Gives pixel-exact outlines, unlike 01_detection which gives boxes only.
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
- [Instance segmentation task docs](https://docs.ultralytics.com/tasks/segment/)
- [Segmentation models list](https://docs.ultralytics.com/tasks/segment/#models)
- [Ultralytics YOLO26 model page](https://docs.ultralytics.com/models/yolo26/)
- [COCO dataset](https://docs.ultralytics.com/datasets/segment/coco/)
- [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)