From bc537984d6c7b25b87a436dafb07241d07f1f600 Mon Sep 17 00:00:00 2001 From: jetson Date: Wed, 4 Nov 2026 10:39:49 +0700 Subject: [PATCH] docs: sync README, ERD, changelog with model upload/conversion feature --- CHANGELOG.md | 18 ++++++++++++++++++ ERD.md | 21 ++++++++++++++++++++- README.md | 12 ++++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7146f5..d183b21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,24 @@ version tags are cut in this repo — `main` is the release line). ### Added - **Annotated output filename carries the source clip name** (`e963ba0`): `merged_annotated.mp4` -> `merged_annotated_.mp4` (single model: `_annotated_.mp4`). Client upload name wins; stored file name is the fallback (`/upload/reuse`). Unsafe chars replaced, stem capped at 80 chars. Download name follows the file (`app.py` `download_name=`). +- **Upload models and convert to `.engine` from the web UI** (`1b032ed`): upload + `.pt`/`.onnx`/`.engine` weights through `/api/models/upload` (aliases + `/api/model/upload`, `/api/upload/model`, `/models/upload`, `/upload/model`); + new weights appear in the model groups immediately. A `.pt`/`.onnx` source + can be converted to a TensorRT `.engine` from the UI or via + `/api/models//convert` (aliases accept the stem in the URL or body); + conversion runs in a background thread in two stages (`.pt` -> `.onnx` via + Ultralytics `export(format="onnx", imgsz=640)`, then `.onnx` -> `.engine` + via `export(format="engine", device=0, half=True, imgsz=640)`), with status + polling on `/api/models/conversions/` (list `/api/models/conversions`, + cancel `/.../cancel`). Known classes are derived from model metadata when + the stem is not in `KNOWN_MODEL_CLASSES` and cached by path/mtime/size. + Conversion and analysis are mutually exclusive: an in-flight conversion + blocks `/upload` and `/api/analyze` (409), and an active analysis job blocks + conversion (409); only one conversion runs at a time. New + `src/model_conversion.py` (`ConversionManager`, `ConversionJob`); updated + `src/model_registry.py` (`derive_known_classes`, `_resolve_known_classes`, + `find_truck_detector` via groups). - **`POST /api/analyze` machine endpoint** (`540e1b5`): multipart video + `fcp_mode` letter (A–D) or explicit `model_stems`; returns **202** `{"job_id","status_url"}`; **409 busy** guard when any job is diff --git a/ERD.md b/ERD.md index 18292e7..9edeaf6 100644 --- a/ERD.md +++ b/ERD.md @@ -12,6 +12,7 @@ string paths, zone presets are name strings, model stems are dict keys. erDiagram upload_video ||--o{ job : "re-analyze, N runs per file" model_group ||--o{ job_result : "selected format runs as" + model_group ||--o{ conversion_job : "source stem converts to" zone_preset ||--o{ job : "soft name ref, zone_mode=fixed" job ||--o{ job_result : "1 for single model, 1 merged row for many" job ||--|| output_dir : "1:1, output/job-" @@ -63,6 +64,19 @@ erDiagram TEXT default_format "engine if present" } + conversion_job { + TEXT job_id PK "conv-, in-memory only" + TEXT stem FK "model stem being converted" + TEXT source_path "uploaded .pt/.onnx source" + TEXT status "pending | running | completed | failed | cancelled" + TEXT stage "queued | exporting onnx | exporting engine | done" + FLOAT progress "0.0 - 100.0" + TEXT error "exception str when FAILED, nullable" + FLOAT started_at "NULL while queued" + FLOAT completed_at "NULL while running" + TEXT output_paths "produced .onnx/.engine paths" + } + zone_preset { TEXT name PK "key under zones.json presets" FLOATS polygon "TruckROI points, video-pixel space" @@ -134,6 +148,7 @@ PENDING ──▶ RUNNING ──▶ COMPLETED | `$OUTPUT_DIR/job-/samples/.cached` | newline list of extracted sample frame paths | | `$OUTPUT_DIR/job-/.mp4` | the annotated video: compressed **in place** when the job finishes (same name, `os.replace`); swept 5 days after the job | | `$OUTPUT_DIR/job-/.*.compress.tmp` | temp of an encode in flight; a stale one (killed encode) is swept after 2 h | +| `$MODELS_DIR/.{engine,pt,onnx}` | weight files grouped by stem; uploaded models land here; conversion writes `.onnx`/`.engine` beside the source | | `/tmp/feedmill_preview_.jpg` | live MJPEG frame, deleted in worker `finally` | | `zones.json` | `presets[]` + `active_preset`, geometry only | | `models/.{engine,pt,onnx}` | weight files grouped by stem | @@ -149,6 +164,9 @@ PENDING ──▶ RUNNING ──▶ COMPLETED (`app.py` `/upload`), so `upload_video.height` is pre-transcode. ## Hazards +- **Conversion jobs are volatile**: `ConversionManager` keeps jobs in memory + (`src/model_conversion.py`); restart loses all conversion state. Only one + conversion runs at a time and it blocks analysis/uploads while running. - **Jobs are volatile**: restart loses all `Job` objects — only `output/job-*` dirs survive. `_register_orphans` (age > 300 s) re-attaches @@ -167,7 +185,8 @@ PENDING ──▶ RUNNING ──▶ COMPLETED ## Sources - Entity defs: `src/job.py:27-73` (`JobResult`, `Job`), - `src/model_registry.py:22-34` (`ModelConfig`, `ModelGroup`). + `src/model_registry.py:68-94` (`ModelConfig`, `ModelGroup`), + `src/model_conversion.py:14-33` (`ConversionJob`). - Lifecycle: `src/job.py:114-148` (`add_job`), `src/job.py:203-343` (`_run_job`), `src/job.py:88-112` (`_register_orphans`). - HTTP surface: `app.py:110-209` (upload/reuse), `app.py:317-471` (APIs). diff --git a/README.md b/README.md index 9b7063b..f5502b3 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Built on top of [karung_counter_semarang](https://git.proit.id/andrew/karung-cou - **Live Preview**: MJPEG streaming — watch processing in real-time (15+ FPS at 640p) - **Multiple Models**: Run multiple model configurations on the same video for comparison - **Model Groups**: Models grouped by stem (e.g., `best.pt`, `best.engine`) with format dropdown +- **Model Upload & Conversion**: Upload `.pt`/`.onnx`/`.engine` weights from the web UI; convert `.pt`/`.onnx` sources to TensorRT `.engine` in the background with progress polling - **Class Filtering**: Choose which classes to count (sack, box, truck) - **Auto Truck Detection**: Automatically loads `truck-detector` when selected model lacks truck class - **Counting Zone Modes**: Auto truck detection, fixed zone presets (zones.json), or auto-detect then freeze @@ -85,9 +86,11 @@ recounter --video PATH Input video file | `/preview/thumb/` | GET | Thumbnail JPEG for a job's video | | `/api/preview/` | GET | MJPEG live stream during processing | | `/api/models` | GET | List model groups (JSON) | -| `/api/models/upload` | POST | Upload a model file (`.pt`/`.onnx`/`.engine`) | -| `/api/models//convert` | POST | Start `.pt`/`.onnx` -> `.engine` conversion | +| `/api/models/upload` | POST | Upload a model file (`.pt`/`.onnx`/`.engine`); aliases `/api/model/upload`, `/api/upload/model`, `/models/upload`, `/upload/model` | +| `/api/models//convert` | POST | Start `.pt`/`.onnx` -> `.engine` conversion; aliases accept the stem in the URL or request body | +| `/api/models/conversions` | GET | List conversion jobs | | `/api/models/conversions/` | GET | Conversion job status | +| `/api/models/conversions//cancel` | POST | Cancel a conversion | | `/api/videos` | GET | List uploaded videos (JSON) | | `/api/videos/` | DELETE | Delete an uploaded video | | `/api/jobs` | GET | List all jobs (JSON) | @@ -125,6 +128,7 @@ src/ ├── dashboard.py # Frame annotation overlay ├── video_writer.py # Annotated video writer ├── model_registry.py # Model discovery, grouping, class metadata +├── model_conversion.py # Background .pt/.onnx -> .engine conversion (ConversionManager) ├── pipeline.py # Video processing pipeline ├── preview.py # Video probing, thumbnails, sample frames └── job.py # Async job queue @@ -142,6 +146,10 @@ Models are grouped by stem name. Each group has available formats: Format preference order: `.engine` > `.pt` > `.onnx` +Weights can be uploaded from the web UI (`/api/models/upload`) and a `.pt`/`.onnx` +source can be converted to `.engine` from the UI or API; conversion runs in the +background and blocks analysis while it runs. + ## Truck Detection The pipeline requires truck detection to start counting. If the selected model lacks a "truck" class (e.g., `best.engine` which only has "sack"), the system automatically loads the `truck-detector` model for truck detection.