From 8804b4506799280d10cfdced4a539265ed5e586b Mon Sep 17 00:00:00 2001 From: dillonj Date: Tue, 23 Dec 2025 18:24:40 -0700 Subject: [PATCH] removed gradio --- GUI_README.md | 148 ----- README.md | 40 +- RTDETR_README.md | 28 +- TRAINING_README.md | 121 ++++ annotation_gui.py | 1270 ------------------------------------- annotation_gui.py.broken | 708 --------------------- convert_for_deployment.py | 247 ++++++++ requirements.txt | 3 +- run_gui.sh | 96 --- run_tk_gui.sh | 11 + tk_annotation_gui.py | 645 +++++++++++++++++++ train_model.py | 317 +++++++++ 12 files changed, 1363 insertions(+), 2271 deletions(-) delete mode 100644 GUI_README.md create mode 100644 TRAINING_README.md delete mode 100644 annotation_gui.py delete mode 100644 annotation_gui.py.broken create mode 100755 convert_for_deployment.py delete mode 100755 run_gui.sh create mode 100755 run_tk_gui.sh create mode 100644 tk_annotation_gui.py create mode 100755 train_model.py diff --git a/GUI_README.md b/GUI_README.md deleted file mode 100644 index 0f3e936c..00000000 --- a/GUI_README.md +++ /dev/null @@ -1,148 +0,0 @@ -# Custom Annotation GUI - -A simple, **fully customizable** annotation tool built with Gradio (pure Python). - -## Features - -✅ **Auto-labeling** with your trained RF-DETR model -✅ **Manual annotation** by entering box coordinates -✅ **Edit/delete** annotations easily -✅ **Navigation** between images -✅ **Export** to COCO JSON format -✅ **100% Python** - easy to modify and extend - -## Quick Start - -### 1. Install dependencies -```bash -/home/dillon/_code/saw_mill_knot_detection/.venv/bin/python -m pip install gradio>=4.0.0 -``` - -### 2. Run the GUI - -**With auto-labeling (requires trained model):** -```bash -/home/dillon/_code/saw_mill_knot_detection/.venv/bin/python annotation_gui.py \ - --images-dir /path/to/images \ - --model-weights runs/knot_rfdetr_medium/checkpoint_best_total.pth -``` - -**Manual annotation only:** -```bash -/home/dillon/_code/saw_mill_knot_detection/.venv/bin/python annotation_gui.py \ - --images-dir /path/to/images -``` - -### 3. Open in browser -Opens automatically at http://localhost:7860 - -## Usage - -1. **Auto-Label**: Click "🤖 Auto-Label" to detect knots with your model -2. **Adjust threshold**: Lower = more detections, Higher = only confident ones -3. **Manual boxes**: Enter coordinates (x1, y1, x2, y2) and click "➕ Add Box" -4. **Delete mistakes**: Click "🗑️ Delete Last" to remove last box -5. **Navigate**: Use "Previous" / "Next" buttons -6. **Export**: Click "💾 Export COCO" when done - -## Customization Examples - -### Add keyboard shortcuts -```python -# In create_ui(), add: -image_display.keyboard_shortcuts = { - "d": delete_btn.click, # Press 'd' to delete - "n": next_btn.click, # Press 'n' for next -} -``` - -### Add interactive drawing -```python -# Replace manual coordinates with image annotator: -from gradio_image_annotation import image_annotator - -annotator = image_annotator( - label="Draw boxes", - type="numpy" -) -``` - -### Change box colors by confidence -```python -# In draw_boxes_on_image(): -color = "green" if conf > 0.8 else "yellow" if conf > 0.5 else "red" -draw.rectangle([x1, y1, x2, y2], outline=color, width=3) -``` - -### Add multiple label classes -```python -# Add a dropdown: -label_choice = gr.Dropdown( - choices=["knot", "crack", "hole"], - value="knot", - label="Label Type" -) - -# Update box dict: -box = { - "bbox": [x1, y1, x2, y2], - "label": label_choice_value, # from the dropdown - "confidence": 1.0 -} -``` - -### Save checkpoints automatically -```python -# In _save_annotations(), add: -import shutil -backup_path = self.ann_file.with_suffix('.backup.json') -shutil.copy(self.ann_file, backup_path) -``` - -### Add image filters/preprocessing -```python -# Add before annotation: -def preprocess_image(img: Image.Image) -> Image.Image: - from PIL import ImageEnhance - enhancer = ImageEnhance.Contrast(img) - return enhancer.enhance(1.5) # Increase contrast -``` - -## File Structure - -``` -annotation_gui.py -├── AnnotationApp # Main logic (easy to extend) -│ ├── auto_label_current() # Modify for different models -│ ├── add_box_manual() # Customize annotation format -│ ├── export_to_coco() # Change export format -│ └── draw_boxes_on_image() # Customize visualization -└── create_ui() # Gradio interface (add components) -``` - -## Advantages vs Label Studio - -| Feature | Custom GUI | Label Studio | -|---------|-----------|--------------| -| **Modify code** | ✅ Easy (pure Python) | ❌ Complex (React + Python) | -| **Add features** | ✅ ~10-50 lines | ❌ Hundreds of lines | -| **Custom models** | ✅ Direct integration | ⚠️ Need ONNX export | -| **Learning curve** | ✅ Simple Gradio | ⚠️ Larger codebase | -| **Setup** | ✅ pip install | ⚠️ Docker/complex | - -## Troubleshooting - -**Port already in use:** -```bash -python annotation_gui.py --images-dir /path --port 7861 -``` - -**Model not loading:** -- Check the weights path exists -- Verify it's a valid checkpoint file -- Try without `--model-weights` for manual-only mode - -**Need more features?** -- Check Gradio docs: https://www.gradio.app/docs/ -- Add custom components easily -- Fork and modify the code freely! diff --git a/README.md b/README.md index e90ac85d..d2e046a1 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Saw Mill Knot Detection -This repository contains a complete wood defect detection system with a web-based annotation GUI and separate training/deployment scripts. Supports multiple model frameworks (RF-DETR, RT-DETR, YOLOv6, YOLOX) and is optimized for deployment on OAK-D cameras. +This repository contains a complete wood defect detection system with a Tkinter-based annotation GUI and separate training/deployment scripts. Supports multiple model frameworks (RF-DETR, RT-DETR, YOLOv6, YOLOX) and is optimized for deployment on OAK-D cameras. ## 🎯 Project Overview - **Models**: RF-DETR, RT-DETR, YOLOv6, YOLOX (all MIT/Apache 2.0 licensed) - **Dataset**: 20,276 wood surface defect images -- **Annotation GUI**: Gradio-based web interface for manual annotation +- **Annotation GUI**: Tkinter desktop app for manual annotation - **Training Scripts**: Separate Python scripts for model training - **Deployment**: OAK-D camera optimization with OpenVINO conversion - **License**: All models free for commercial use @@ -47,21 +47,16 @@ pip install -r requirements.txt ### 2. Run the Annotation GUI -The repository includes an automated script that handles virtual environment activation: - ```bash -# Run the GUI (automatically detects and activates venv/conda environment) -./run_gui.sh - -# Or run manually -source .venv/bin/activate # or conda activate your_env -python annotation_gui.py +./run_tk_gui.sh --images-dir IMAGE/ +# or +python tk_annotation_gui.py --images-dir IMAGE/ ``` -Install dependencies: +Auto-label requires Ultralytics for YOLO/RT-DETR weights: + ```bash -pip install -U pip -pip install ultralytics gradio rfdetr +pip install ultralytics ``` ### 2. Setup Datasets @@ -78,24 +73,13 @@ python setup_datasets.py # Creates dataset_coco/ and updates configs ### 3. Launch Annotation GUI -```bash -python annotation_gui.py -``` - -Tkinter version (new): - ```bash python tk_annotation_gui.py # or ./run_tk_gui.sh ``` -Open http://localhost:7860 in your browser to access the web-based annotation interface with: -- Image navigation with index display -- Auto-labeling with trained models -- Manual annotation tools with delete buttons -- Real-time result visualization -- Export to COCO format +The Tkinter GUI supports image navigation, autosave annotations, and optional auto-label. ### 4. Train Models @@ -127,7 +111,8 @@ python convert_for_deployment.py --model runs/training/weights/best.pt --output ``` saw_mill_knot_detection/ -├── annotation_gui.py # Gradio web interface for annotation +├── tk_annotation_gui.py # Tkinter desktop annotation GUI +├── run_tk_gui.sh # Convenience launcher ├── train_model.py # Unified training script for all frameworks ├── convert_for_deployment.py # Model conversion for OAK-D deployment ├── TRAINING_README.md # Detailed training and deployment guide @@ -174,7 +159,7 @@ saw_mill_knot_detection/ ### Annotation GUI Features -The Gradio-based annotation interface provides: +The Tkinter annotation GUI provides: - **Image Navigation**: Browse through dataset with current index display - **Auto-Labeling**: One-click defect detection using trained YOLOX model @@ -267,4 +252,3 @@ This project uses the Kaggle Wood Surface Defects dataset. Please refer to the o - Kaggle for providing the wood surface defects dataset - Ultralytics for the YOLO framework -- Gradio for the web interface framework diff --git a/RTDETR_README.md b/RTDETR_README.md index 3bd9396d..e2b5ec80 100644 --- a/RTDETR_README.md +++ b/RTDETR_README.md @@ -14,9 +14,9 @@ RT-DETR (Real-Time Detection Transformer) is Apache 2.0 licensed - **free for co ### 1. Annotate Images -Use the annotation GUI: +Use the Tkinter annotation GUI: ```bash -.venv/bin/python annotation_gui.py +.venv/bin/python tk_annotation_gui.py --images-dir IMAGE/ ``` - Load your images from Settings @@ -25,24 +25,14 @@ Use the annotation GUI: ### 2. Train Model -From the GUI: -1. Go to **Training** tab -2. Click "Prepare Dataset" (creates train/valid/test splits) -3. Select **RT-DETR** framework -4. Choose model size: - - `nano` (r18): Fastest, 30-40 FPS on OAK - - `small` (r34): Balanced - - `medium` (r50): More accurate - - `base` (l): Best accuracy, slower -5. Click "Start Training" - -Or from command line: +Train from the command line: ```bash -.venv/bin/python train_rtdetr.py \ - --dataset-dir dataset_prepared \ - --model rtdetr-r18 \ - --epochs 100 \ - --batch-size 8 +.venv/bin/python train_model.py \ + --framework rtdetr \ + --dataset dataset_prepared \ + --output runs/rtdetr_training \ + --model-size small \ + --epochs 100 ``` ### 3. Test Model diff --git a/TRAINING_README.md b/TRAINING_README.md new file mode 100644 index 00000000..0db6fb06 --- /dev/null +++ b/TRAINING_README.md @@ -0,0 +1,121 @@ +# Training and Deployment Scripts + +This directory contains separate scripts for training models and converting them for deployment, extracted from the annotation GUI. + +## Training Script + +### `train_model.py` + +Train object detection models for wood knot detection. + +**Supported frameworks:** +- RF-DETR (MIT license) +- RT-DETR (Apache 2.0 license) +- YOLOv6 (MIT license) +- YOLOX (MIT license) + +**Usage:** + +```bash +# Basic usage +python train_model.py --framework rtdetr --dataset dataset_prepared --output runs/training + +# Full options +python train_model.py \ + --framework rtdetr \ + --dataset dataset_prepared \ + --output runs/training \ + --model-size small \ + --epochs 20 \ + --batch-size 4 \ + --lr 0.001 \ + --prepare-dataset \ + --images-dir IMAGE \ + --annotations annotations.json +``` + +**Options:** +- `--framework`: Model framework (rf-detr, rt-detr, yolov6, yolox) +- `--dataset`: Path to prepared dataset directory +- `--output`: Output directory for trained model +- `--model-size`: Model size/variant (nano, small, medium, base) +- `--epochs`: Number of training epochs +- `--batch-size`: Batch size for training +- `--lr`: Learning rate +- `--prepare-dataset`: Prepare dataset from annotations first +- `--images-dir`: Images directory (for --prepare-dataset) +- `--annotations`: Annotations file (for --prepare-dataset) + +## Deployment Conversion Script + +### `convert_for_deployment.py` + +Convert trained models for OAK-D deployment. + +**Supported conversions:** +- ONNX export +- OpenVINO IR export +- Model optimization for edge devices + +**Usage:** + +```bash +# Basic usage +python convert_for_deployment.py --model runs/training/weights/best.pt --output oak_d_deployment + +# Full options +python convert_for_deployment.py \ + --model runs/training/weights/best.pt \ + --output oak_d_deployment \ + --img-size 640 \ + --framework auto +``` + +**Options:** +- `--model`: Path to trained model weights (.pt file) +- `--output`: Output directory for converted models +- `--img-size`: Input image size for the model (320, 416, 512, 640, 800, 1024) +- `--framework`: Model framework (auto-detect if not specified) + +## Workflow + +1. **Annotate images** using the Tkinter GUI (`./run_tk_gui.sh` or `python tk_annotation_gui.py`) +2. **Export annotations** to COCO format from the GUI +3. **Prepare dataset** (optional, can be done by training script): + ```bash + python train_model.py --prepare-dataset --images-dir IMAGE --annotations annotations.json --dataset dataset_prepared + ``` +4. **Train model**: + ```bash + python train_model.py --framework rtdetr --dataset dataset_prepared --output runs/training + ``` +5. **Convert for deployment**: + ```bash + python convert_for_deployment.py --model runs/training/weights/best.pt --output oak_d_deployment + ``` + +## Next Steps After Conversion + +After running `convert_for_deployment.py`: + +1. **Test OpenVINO Model** (optional): + ```bash + python -c "from openvino.runtime import Core; core = Core(); model = core.read_model('model.xml'); print('✓ Model loaded')" + ``` + +2. **Convert to RVC compiled format** (recommended by Luxonis): + - Online: HubAI conversion (fastest setup) + - Offline: ModelConverter (requires Docker) + - Docs: https://docs.luxonis.com/software-v3/ai-inference/conversion/ + +3. **Deploy to OAK-D**: + - Use DepthAI Python API + - Or use OAK-D examples with your blob + +## Tips + +- **Nano models** work best on edge devices +- If you quantize, use real calibration images for best accuracy +- Test inference speed vs accuracy trade-off +- All models are MIT/Apache 2.0 licensed - free for commercial use! +/home/dillon/_code/saw_mill_knot_detection/TRAINING_README.md \ No newline at end of file diff --git a/annotation_gui.py b/annotation_gui.py deleted file mode 100644 index f0529045..00000000 --- a/annotation_gui.py +++ /dev/null @@ -1,1270 +0,0 @@ -""" -Simple customizable annotation GUI with auto-labeling support. - -Built with Gradio - easy to modify and extend. -Run: python annotation_gui.py - -To set default paths, edit config.py -""" - -from __future__ import annotations - -import argparse -import html -import json -import subprocess -import threading -from pathlib import Path -from typing import Any - -import gradio as gr -from PIL import Image, ImageDraw - -# Try to load config, use fallbacks if not available -try: - from config import ( - DEFAULT_IMAGES_DIR, DEFAULT_MODEL_WEIGHTS, DEFAULT_PORT, - DEFAULT_DETECTION_THRESHOLD, DEFAULT_TRAIN_EPOCHS, - DEFAULT_BATCH_SIZE, DEFAULT_LEARNING_RATE, DEFAULT_MODEL_SIZE - ) -except ImportError: - DEFAULT_IMAGES_DIR = None - DEFAULT_MODEL_WEIGHTS = None - DEFAULT_PORT = 7860 - DEFAULT_DETECTION_THRESHOLD = 0.5 - DEFAULT_TRAIN_EPOCHS = 20 - DEFAULT_BATCH_SIZE = 4 - DEFAULT_LEARNING_RATE = 1e-4 - DEFAULT_MODEL_SIZE = "small" - - -# Gradio 6 sanitizes