Skip to content

Instantly share code, notes, and snippets.

@tzhbami7
Created April 22, 2026 08:46
Show Gist options
  • Select an option

  • Save tzhbami7/dd404d9c1d2cb38003691af5d7467779 to your computer and use it in GitHub Desktop.

Select an option

Save tzhbami7/dd404d9c1d2cb38003691af5d7467779 to your computer and use it in GitHub Desktop.
yolo.md
## What this gives you
* open an image,
* run a YOLO model on it,
* use **SAHI** to split the image into tiles,
* get better results on **small objects** than plain whole-image inference often gives. SAHI is specifically designed to slice large or high-resolution images into smaller parts, run detection on each slice, and stitch the results back together. ([GitHub][2])
## Step 1: Install Python on Windows 11
Install Python from the **Microsoft Store** or from **python.org**. The official Python Windows docs say both installation methods are supported, and after installation the `python` and `py` commands should be available in the terminal. ([Python documentation][3])
After installing, open **Command Prompt** and check:
```bat
python --version
py --version
```
If one of those works, you’re good.
## Step 2: Create a project folder
In File Explorer, create a folder like:
```text
C:\litter-monitor-demo
```
Then open **Command Prompt** in that folder and run:
```bat
cd C:\litter-monitor-demo
py -m venv .venv
```
The Python packaging guide recommends using `venv`, and on Windows the standard command is `py -m venv .venv`. ([Python Packaging][4])
## Step 3: Activate the virtual environment
Still in Command Prompt:
```bat
.venv\Scripts\activate
```
The official packaging guide shows this as the Windows activation command. Once activated, package installs go into this project only. ([Python Packaging][4])
You can confirm it is active with:
```bat
where python
```
You should see a path that includes `.venv\Scripts\python`. ([Python Packaging][4])
## Step 4: Install the packages
For the easiest first run, install:
```bat
python -m pip install --upgrade pip
pip install ultralytics sahi
```
Ultralytics documents `pip install -U ultralytics` as the recommended pip install path, and SAHI documents `pip install sahi` for basic installation. ([Ultralytics Docs][1])
## Step 5: Put a test image in the folder
Create a subfolder:
```bat
mkdir images
```
Then place one test image inside, for example:
```text
C:\litter-monitor-demo\images\test.jpg
```
For your use case, use a real street or hotspot image from the camera if possible.
## Step 6: Create the test script
Create a file named:
```text
run_sahi_test.py
```
Paste this in:
```python
from pathlib import Path
from sahi import AutoDetectionModel
from sahi.predict import get_sliced_prediction
# Change this to your image path
IMAGE_PATH = "images/test.jpg"
OUTPUT_DIR = "outputs"
# Use a small YOLO model for an easy first run
MODEL_PATH = "yolo11n.pt" # Ultralytics will download it automatically if needed
def main():
Path(OUTPUT_DIR).mkdir(parents=True, exist_ok=True)
detection_model = AutoDetectionModel.from_pretrained(
model_type="ultralytics",
model_path=MODEL_PATH,
confidence_threshold=0.25,
device="cpu", # change to "cuda:0" later if GPU works
)
result = get_sliced_prediction(
IMAGE_PATH,
detection_model,
slice_height=640,
slice_width=640,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
)
result.export_visuals(
export_dir=OUTPUT_DIR,
file_name="prediction",
hide_labels=False,
hide_conf=False,
)
print("Done.")
print(f"Visual output saved in: {OUTPUT_DIR}")
# Optional: print detections in terminal
for i, obj in enumerate(result.object_prediction_list, start=1):
bbox = obj.bbox
score = obj.score.value
category = obj.category.name
print(
f"{i}. class={category}, score={score:.3f}, "
f"bbox=({bbox.minx:.1f}, {bbox.miny:.1f}, {bbox.maxx:.1f}, {bbox.maxy:.1f})"
)
if __name__ == "__main__":
main()
```
This follows the documented idea behind SAHI: use a detection model, run sliced prediction, and merge the detections afterward. SAHI’s docs describe this sliced workflow, and Ultralytics’ SAHI guide explains that this is especially useful for large images and small objects. ([GitHub][2])
## Step 7: Run it
In the same Command Prompt window:
```bat
python run_sahi_test.py
```
If all goes well, you should get:
* a new `outputs` folder
* a prediction image with boxes drawn
* detection details printed in the terminal
## Step 8: Understand the result
This first test is mainly to prove that the toolchain works. The default YOLO model is a general object detector, so it may detect things like bottles, cups, bags, or people, but it will not yet be a litter-specialized model. Ultralytics supports using pretrained YOLO models easily, and SAHI works with YOLO-family models for sliced inference. ([Ultralytics Docs][1])
So for your product, the likely path is:
1. get this running,
2. test on your real hotspot images,
3. see what it detects well and what it misses,
4. later train or fine-tune a litter-specific model.
## Optional: GPU setup later
So after the CPU version works, the GPU path is:
* install or update the NVIDIA driver,
* install the correct PyTorch build from the official PyTorch install selector,
* then change this line in the script:
```python
device="cpu"
```
to:
```python
device="cuda:0"
```
And test with:
```bat
python
```
then:
```python
import torch
print(torch.cuda.is_available())
```
PyTorch documents this as the verification step. ([PyTorch][5])
## Easiest folder layout
Use this structure:
```text
C:\litter-monitor-demo
│ run_sahi_test.py
├── .venv
├── images
│ └── test.jpg
└── outputs
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment