143 lines
6.9 KiB
Markdown
143 lines
6.9 KiB
Markdown
# YOLO26: 8 Computer Vision Tasks on Video (Jetson Orin Nano)
|
|
|
|
## Task Title
|
|
|
|
Run 8 YOLO26 computer vision tasks on a single input video using Python.
|
|
|
|
## Description
|
|
|
|
One script runs the YOLO26 model family on one video file (`ai_video.mp4`) and saves every result into a `results/` folder. Each task loads its own YOLO26 nano model, processes the video frame by frame (streaming, so memory stays low on the Jetson) and writes an annotated output video or files.
|
|
|
|
Tasks in the script:
|
|
|
|
1. Detection: boxes, class names and confidence on every frame, saved as a video.
|
|
2. Tracking: real tracking, object IDs persist across frames, saved as a video.
|
|
3. Segmentation: pixel-level masks on every frame, saved as a video.
|
|
4. Pose estimation: skipped by default (code kept but commented out).
|
|
5. Classification: top-1 label per frame overlaid on the video, plus a text log.
|
|
6. ROI extraction: crops each detected box out of sampled frames and saves each crop as an image.
|
|
7. Depth estimation: native YOLO26 depth model, saved as a colorized depth video plus raw depth arrays.
|
|
8. OBB (oriented bounding boxes): skipped by default (code kept but commented out).
|
|
|
|
## Hardware and Software
|
|
|
|
- Device: NVIDIA Jetson Orin Nano (JetPack with CUDA and TensorRT)
|
|
- Language: Python 3
|
|
- Libraries: ultralytics, opencv-python, numpy
|
|
- Input: `ai_video.mp4` in the same folder as the script
|
|
|
|
## Installation
|
|
|
|
```
|
|
pip install -U ultralytics
|
|
```
|
|
|
|
On Jetson, install the JetPack-matched PyTorch and torchvision builds first (see the Jetson guide in the references). Model weights download automatically on first run, so the first run needs internet.
|
|
|
|
## Folder Layout
|
|
|
|
```
|
|
your_folder/
|
|
yolo26_8_tasks_video.py (rename to match your script file name)
|
|
ai_video.mp4
|
|
results/ (created automatically)
|
|
```
|
|
|
|
## Command to Run
|
|
|
|
```
|
|
python3 yolo26_8_tasks_video.py
|
|
```
|
|
|
|
To use a different video, change `VIDEO_PATH` at the top of the script.
|
|
|
|
## Models Used
|
|
|
|
- Task 1 and 2 and 6: `yolo26n.pt` (detection, COCO 80 classes)
|
|
- Task 3: `yolo26n-seg.pt` (instance segmentation)
|
|
- Task 4 (optional): `yolo26n-pose.pt` (17 human keypoints)
|
|
- Task 5: `yolo26n-cls.pt` (ImageNet 1000 classes)
|
|
- Task 7: `yolo26n-depth.pt` (per-pixel metric depth in meters, run at imgsz=768)
|
|
- Task 8 (optional): `yolo26n-obb.pt` (DOTA aerial classes)
|
|
|
|
## Settings You Can Change
|
|
|
|
- `VIDEO_PATH = "ai_video.mp4"` : input video
|
|
- `OUT_DIR = "results"` : folder for all outputs
|
|
- `ROI_FRAME_STRIDE = 15` : save ROI crops every 15th frame
|
|
- `DEPTH_NPY_STRIDE = 30` : save a raw depth `.npy` file every 30th frame
|
|
- `imgsz=768` in the depth call : depth model input size; lower it if the Jetson runs out of memory
|
|
|
|
## How It Works
|
|
|
|
1. The script checks that the video exists and reads its FPS, width, height and frame count.
|
|
2. Each task runs inside its own `try/except`, so if one task fails the others still run and the failure is printed as `[n - Task] FAILED`.
|
|
3. Detection, tracking, segmentation and classification call the model with `stream=True`, read each result and write `result.plot()` to an output video.
|
|
4. Tracking uses `track(..., persist=True)` so IDs stay the same across frames.
|
|
5. ROI extraction reuses the detection results from Task 1 and crops boxes only on sampled frames, because a full video would create thousands of files.
|
|
6. Depth estimation reads `result.depth.data` (a float32 map in meters), normalizes it, applies the MAGMA color map and writes a colorized video. It prints the minimum and maximum depth across the clip.
|
|
|
|
## Output Files (inside `results/`)
|
|
|
|
- `1_detection.mp4` : detection video
|
|
- `2_tracking.mp4` : tracking video with persistent IDs
|
|
- `3_segmentation.mp4` : segmentation video
|
|
- `5_classification.mp4` : per-frame classification video
|
|
- `5_classification_log.txt` : one line per frame with the top-1 label and confidence
|
|
- `6_roi_crops/` : cropped objects, named like `frame15_roi0_person.png`
|
|
- `7_depth.mp4` : colorized depth video
|
|
- `7_depth_raw/` : raw depth arrays like `frame30.npy` (meters)
|
|
|
|
|
|
## Verify the Output
|
|
|
|
- [ ] The console prints the video size, FPS and frame count at the start.
|
|
- [ ] Each finished task prints `Saved:` with its path.
|
|
- [ ] `results/` contains the videos, the log, the crops folder and the depth folder.
|
|
- [ ] In `2_tracking.mp4` the same object keeps the same ID while it moves.
|
|
- [ ] In `3_segmentation.mp4` the colored masks follow object shapes.
|
|
- [ ] In `7_depth.mp4` nearer objects and farther objects have clearly different colors.
|
|
- [ ] The final line prints `Done. Check the 'results' folder for all saved outputs.`
|
|
|
|
## Notes and Limitations
|
|
|
|
- Pose (task 4): the pose model finds human keypoints. If the video has no person, every frame returns nothing. Uncomment the block and use footage with people.
|
|
- OBB (task 8): the OBB model is trained on aerial and satellite classes (planes, ships, vehicles, storage tanks). On everyday scenes it returns nothing. Uncomment the block and use aerial footage.
|
|
- Classification (task 5): classification expects one main subject per frame. With many objects in view the top-1 label is only a rough per-frame guess, not a scene description.
|
|
- ROI (task 6): this is not a separate YOLO model. It crops the detected boxes from the original frame.
|
|
|
|
## Optional: Faster Inference with TensorRT
|
|
|
|
Export a model once and use the engine file in the script:
|
|
|
|
```
|
|
yolo export model=yolo26n.pt format=engine half=True
|
|
```
|
|
|
|
Then replace `YOLO("yolo26n.pt")` with `YOLO("yolo26n.engine")`.
|
|
|
|
## Troubleshooting
|
|
|
|
- `Could not open 'ai_video.mp4'`: put the video in the same folder as the script or fix `VIDEO_PATH`.
|
|
- Output video is empty or cannot be played: install a player that supports `mp4v`, or change the codec in `open_writer`.
|
|
- Out of memory on Jetson: close other apps, lower `imgsz` for depth, or run one task at a time.
|
|
- Low speed: run `sudo nvpmodel -m 0` and `sudo jetson_clocks`, or export to TensorRT.
|
|
- Weights fail to download: connect to the internet for the first run, or copy the `.pt` files next to the script.
|
|
|
|
## References
|
|
|
|
- [Ultralytics YOLO26 models](https://docs.ultralytics.com/models/yolo26/)
|
|
- [Object detection task](https://docs.ultralytics.com/tasks/detect/)
|
|
- [Object tracking mode](https://docs.ultralytics.com/modes/track/)
|
|
- [Instance segmentation task](https://docs.ultralytics.com/tasks/segment/)
|
|
- [Pose estimation task](https://docs.ultralytics.com/tasks/pose/)
|
|
- [Image classification task](https://docs.ultralytics.com/tasks/classify/)
|
|
- [Oriented bounding boxes (OBB) task](https://docs.ultralytics.com/tasks/obb/)
|
|
- [Depth estimation task](https://docs.ultralytics.com/tasks/depth/)
|
|
- [Predict mode (streaming inference)](https://docs.ultralytics.com/modes/predict/)
|
|
- [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)
|
|
- [Ultralytics on PyPI](https://pypi.org/project/ultralytics/)
|
|
- [OpenCV VideoWriter docs](https://docs.opencv.org/4.x/dd/d9e/classcv_1_1VideoWriter.html)
|