Tools for preparing walnut images and manually annotating walnut centers for training or evaluation.
| Script | Purpose |
|---|---|
crop_image_quadrants.py |
Split every image in a folder into four equal quadrants each (useful for tiling large photos). |
walnut_annotator.py |
Interactive GUI to click walnut centers and save coordinates to text files. |
Typical workflow
- (Optional) Crop large images into quadrants with
crop_image_quadrants.py. - Annotate walnut centers with
walnut_annotator.py. - Use the generated
.txtannotation files downstream (training, validation, etc.).
Both scripts are documented in two places:
- Module docstrings (top of each file) — usage, options, and behavior at a glance.
- Inline comments — explain non-obvious logic (coordinate transforms, zoom/pan, save format, quadrant naming).
Read the file headers before running; they mirror what --help shows and add context (e.g. quadrant naming q00–q11, annotation file format).
- Python 3.9+
- Dependencies:
opencv-python,numpy(seerequirements.txt)
OpenCV needs a display for walnut_annotator.py (not ideal over headless SSH). Use opencv-python (not opencv-python-headless) if the GUI window does not open.
python3 -m venv .venvOn Windows (Command Prompt):
python -m venv .venvmacOS / Linux (bash/zsh):
source .venv/bin/activateWindows — PowerShell:
.\.venv\Scripts\Activate.ps1Windows — Command Prompt:
.venv\Scripts\activate.batIf PowerShell refuses to run Activate.ps1 with an execution-policy error, allow scripts for your user (one-time):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserThen activate again:
.\.venv\Scripts\Activate.ps1RemoteSignedlets local scripts run; downloaded scripts still need signing.- You can revert later with
Set-ExecutionPolicy -ExecutionPolicy Restricted -Scope CurrentUserif you prefer. - Alternative: use Command Prompt and
activate.batabove (no PowerShell script policy).
pip install --upgrade pip
pip install -r requirements.txtpython -c "import cv2, numpy; print('opencv', cv2.__version__, 'numpy', numpy.__version__)"
python crop_image_quadrants.py --help
python walnut_annotator.py --helpSplit every image in a directory into four equal tiles each (.jpg, .jpeg, .png, case-insensitive).
python crop_image_quadrants.py /path/to/images -o /path/to/output| Argument / option | Description |
|---|---|
input_dir |
Folder containing images to crop |
-o, --output |
Output directory (default: same folder as input_dir) |
Odd dimensions are padded automatically (one black pixel on the bottom and/or right) so each quadrant is equal size.
Output names: <stem>_q00, _q01, _q10, _q11 + original extension
| Suffix | Region |
|---|---|
q00 |
top-left |
q01 |
top-right |
q10 |
bottom-left |
q11 |
bottom-right |
Example:
python crop_image_quadrants.py image -o image/quadrantsInteractive annotation over all images in a folder.
python walnut_annotator.py /path/to/images -o /path/to/annotations| Option | Description |
|---|---|
image_folder |
Folder of .jpg / .jpeg / .png images |
-o, --output |
Where .txt annotations are saved (default: image_folder/annotations) |
-r, --radius |
Circle radius for markers (default: 8) |
Controls
| Action | Effect |
|---|---|
| Left click | Add walnut center |
| Right click | Remove nearest annotation |
| Ctrl + left click + drag | Pan |
Mouse wheel or + / - |
Zoom in/out (+/- work when the wheel does not, e.g. on macOS) |
s |
Save current image |
n / p |
Next / previous image (saves first) |
r |
Reset zoom and pan |
z |
Clear annotations on current image |
q or Esc |
Quit (saves before exit) |
Annotation files: one <image_stem>.txt per image in the output folder. Header lines start with #; data lines are x y in image pixel coordinates.
Walnut_annotation/
├── crop_image_quadrants.py
├── walnut_annotator.py
├── requirements.txt
├── Readme.md
└── image/ # example images (optional)
| Issue | What to try |
|---|---|
| PowerShell won’t activate venv | Set-ExecutionPolicy RemoteSigned -Scope CurrentUser, or use activate.bat in cmd |
import cv2 fails |
Activate venv, run pip install -r requirements.txt |
| Annotator window doesn’t appear | Use opencv-python, run locally with a display |
| Zoom doesn’t work with mouse wheel | Click the window to focus it, or use + / - on the keyboard (common on macOS) |
| Uneven split on odd-sized images | Padding is automatic (black pixel on bottom/right) |