magiclabel 0.2.1__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,424 @@
1
+ Metadata-Version: 2.4
2
+ Name: magiclabel
3
+ Version: 0.3.0
4
+ Summary: AI-assisted image annotation CLI for computer vision datasets
5
+ Author: Rohan Rustagi
6
+ License: MIT
7
+ Keywords: annotation,labeling,computer-vision,yolo,dataset,object-detection
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Requires-Python: >=3.8
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: ultralytics>=8.0.0
19
+ Requires-Dist: typer>=0.9.0
20
+ Requires-Dist: rich
21
+ Requires-Dist: opencv-python
22
+ Requires-Dist: gradio
23
+ Requires-Dist: gradio_image_annotation
24
+ Requires-Dist: torch
25
+ Requires-Dist: torchvision
26
+ Dynamic: license-file
27
+
28
+ # MagicLabel
29
+
30
+ > AI‑assisted image labeling CLI — go from raw images to your own trained model, end to end.
31
+
32
+ Good models start with good data, and labeling that data by hand is the slowest part of any computer‑vision project. **MagicLabel** runs the whole loop from one command line:
33
+
34
+ 1. **Auto‑detect** objects in a folder of images — including custom objects you describe in plain words (no training needed).
35
+ 2. **Review & fix** the labels in a browser — draw, move, resize, relabel, delete.
36
+ 3. **Train** a custom YOLO model on your labeled data.
37
+ 4. **Reuse** your named model (`uav.pt`, `rohan.pt`, …) to auto‑label the next batch — the loop closes.
38
+
39
+ Everything runs **locally and free** — no account, no cloud upload, no Docker. Works in **Google Colab** too (`--share`).
40
+
41
+ ![Python](https://img.shields.io/badge/python-3.8%2B-blue)
42
+ ![License](https://img.shields.io/badge/license-MIT-green)
43
+ ![PyPI](https://img.shields.io/pypi/v/magiclabel)
44
+
45
+ ---
46
+
47
+ ## Features
48
+
49
+ - 🔍 **Auto‑detect** — label an image folder with a YOLO model (the 80 COCO classes out of the box).
50
+ - 🗣️ **Open‑vocabulary detect** — `--prompt "drone,missile"` detects *anything you name*, with no training (YOLO‑World).
51
+ - 🖊️ **Visual review** — a browser UI to draw / move / resize / relabel / delete boxes. Type **any custom class name** you want.
52
+ - 🎓 **Train** — fine‑tune a custom model (YOLO26 by default) on your labeled data and save it as a named `.pt`.
53
+ - 🎬 **Predict** — test any model on an image, video, or folder; annotated results saved automatically.
54
+ - 🔄 **Export** — convert YOLO labels to COCO JSON.
55
+ - 🏷️ **Inspect** — print the class names a model knows.
56
+ - ☁️ **Colab‑ready** — `--share` gives you a public link to the review UI from any remote machine.
57
+
58
+ ---
59
+
60
+ ## Installation
61
+
62
+ ```bash
63
+ pip install magiclabel
64
+ ```
65
+
66
+ > **Heads‑up:** MagicLabel depends on `torch` and `ultralytics`, so installation pulls ~1 GB of packages. That's normal for a computer‑vision tool.
67
+
68
+ From source:
69
+
70
+ ```bash
71
+ git clone https://github.com/your-username/magiclabel.git
72
+ cd magiclabel
73
+ python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
74
+ pip install -e .
75
+ ```
76
+
77
+ Verify:
78
+
79
+ ```bash
80
+ magiclabel --help
81
+ ```
82
+
83
+ ---
84
+
85
+ ## Quick start — the full loop
86
+
87
+ ```bash
88
+ # 1. Auto-label a folder of images (80 COCO classes)
89
+ magiclabel detect ./images --output ./labels
90
+
91
+ # …or detect a CUSTOM object by describing it (no training):
92
+ magiclabel detect ./images --prompt "drone, quadcopter" --output ./labels
93
+
94
+ # 2. Review and fix the labels in your browser
95
+ magiclabel review ./images ./labels
96
+
97
+ # 3. Train your own model on the corrected dataset
98
+ magiclabel train ./images ./labels --name uav.pt --epochs 100
99
+
100
+ # 4. Test it — run it on an image, folder, or video and eyeball the results
101
+ magiclabel predict ./drone-video.mp4 --model uav.pt
102
+
103
+ # 5. Reuse your model to auto-label the next batch — the loop closes
104
+ magiclabel detect ./new_images --model uav.pt --output ./labels2
105
+ ```
106
+
107
+ ---
108
+
109
+ ## Command reference
110
+
111
+ Run `magiclabel <command> --help` at any time for the full option list.
112
+
113
+ ### `detect` — auto‑annotate images with bounding boxes
114
+
115
+ Runs a model over every image in a directory. Writes one YOLO‑format `.txt` per image **plus a `classes.txt`** (the class list, one name per line, id = line number). `classes.txt` is **merged, never overwritten** — re‑running `detect` or adding custom classes in `review` keeps ids consistent, and only classes that are actually detected get an id.
116
+
117
+ ```bash
118
+ magiclabel detect SOURCE [OPTIONS]
119
+ ```
120
+
121
+ | Option | Default | Description |
122
+ | ---------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------- |
123
+ | `SOURCE` | *(required)* | Directory of images (`.jpg`, `.jpeg`, `.png`, `.bmp`, `.tif`, `.tiff`) |
124
+ | `--prompt` | *(none)* | Comma‑separated object names for **open‑vocabulary** detection, e.g. `"drone,missile"`. No training needed. |
125
+ | `--model` | `yolo26n.pt` / `yolov8s-world.pt` | Model file. Defaults to `yolo26n.pt` (closed‑set) or `yolov8s-world.pt` when `--prompt` is given. Pass your own trained `.pt` to use it. |
126
+ | `--output` | `./labels_detect` | Output directory for label files + `classes.txt` |
127
+ | `--conf` | `0.25` | Confidence threshold (0–1); higher = fewer, surer boxes |
128
+
129
+ **Examples**
130
+
131
+ ```bash
132
+ # Closed-set: detect the 80 COCO classes
133
+ magiclabel detect ./images --output ./labels --conf 0.3
134
+
135
+ # Open-vocabulary: detect objects YOLO was never trained on
136
+ magiclabel detect ./images --prompt "drone, fixed-wing aircraft" --conf 0.1 --output ./labels
137
+
138
+ # Use a bigger pretrained model for better accuracy
139
+ magiclabel detect ./images --model yolov8x.pt --output ./labels
140
+
141
+ # Use YOUR OWN trained model (see `train` below)
142
+ magiclabel detect ./new_images --model uav.pt --output ./predictions --conf 0.25
143
+ ```
144
+
145
+ > **Tip:** open‑vocabulary detection is great on common/clear objects and hit‑or‑miss on unusual ones. For hard objects, hand‑label a batch in `review`, `train` a model, then `detect --model your.pt` to auto‑label the rest.
146
+
147
+ ---
148
+
149
+ ### `review` — draw, fix, and label boxes in your browser
150
+
151
+ Launches a local web app (default <http://127.0.0.1:7860>) to inspect and correct labels.
152
+
153
+ ```bash
154
+ magiclabel review IMAGES_DIR LABELS_DIR [OPTIONS]
155
+ ```
156
+
157
+ | Argument / Option | Default | Description |
158
+ | ---------------------- | -------------- | --------------------------------------------------------------------------------------------------- |
159
+ | `IMAGES_DIR` | *(required)* | Directory of images |
160
+ | `LABELS_DIR` | *(required)* | Directory of YOLO `.txt` labels (and `classes.txt`) |
161
+ | `--model` | `yolo26n.pt` | Model whose class names label the boxes. Use **`--model none`** to start a clean custom dataset with no preset names. |
162
+ | `--share / --no-share` | auto | Create a public `*.gradio.live` link. **Auto‑enabled on Google Colab**; use `--share` on any other remote machine. |
163
+
164
+ **Examples**
165
+
166
+ ```bash
167
+ # Review labels locally
168
+ magiclabel review ./images ./labels
169
+
170
+ # Start a CLEAN custom dataset (first class you type gets id 0 — no COCO baggage)
171
+ magiclabel review ./images ./my_labels --model none
172
+
173
+ # On Google Colab / a remote VM — get a public link
174
+ magiclabel review ./images ./labels --share
175
+ ```
176
+
177
+ **In the UI you can:**
178
+
179
+ - **Draw** a new box — click and drag on the image.
180
+ - **Move / resize** — click a box and drag it or its corner handles.
181
+ - **Set the class** — double‑click a box, **type any class name** (`drone`, `crack`, `logo`), then click the editor's apply/✓ to commit. New names are appended to `classes.txt` automatically.
182
+ - **Delete** — the box's remove button, the <kbd>Delete</kbd> key, or **Clear All Boxes**.
183
+ - **Switch images** — the dropdown at the top (opens on the first image that already has boxes).
184
+ - **Save Changes** — writes boxes back to the `.txt`; the view reloads from disk so you can confirm exactly what was saved.
185
+
186
+ > Edits live only in the browser until you click **Save Changes**. Saving an image with no boxes writes an empty label file (a valid "no objects" annotation).
187
+
188
+ ---
189
+
190
+ ### `train` — train your own custom model
191
+
192
+ Fine‑tunes a YOLO model on your labeled dataset and saves it under any name you choose. This is **transfer learning**: the base model provides general visual knowledge; your data teaches it *your* classes. The output `.pt` contains **only your classes**.
193
+
194
+ The dataset is automatically **compacted**: only class ids that actually appear in your labels are trained, remapped to a contiguous `0..K‑1` and named from `classes.txt`. Stray or out‑of‑range ids can't crash training.
195
+
196
+ ```bash
197
+ magiclabel train IMAGES_DIR LABELS_DIR [OPTIONS]
198
+ ```
199
+
200
+ | Option | Default | Description |
201
+ | ------------- | ------------ | -------------------------------------------------------- |
202
+ | `IMAGES_DIR` | *(required)* | Directory of images |
203
+ | `LABELS_DIR` | *(required)* | Directory of YOLO labels + `classes.txt` |
204
+ | `--name` | `custom.pt` | Output model name, e.g. `uav.pt`, `rohan.pt` |
205
+ | `--base` | `yolo26n.pt` | Base model to fine‑tune (or your own `.pt` to keep improving it) |
206
+ | `--epochs` | `50` | Training epochs |
207
+ | `--imgsz` | `640` | Training image size |
208
+ | `--val-split` | `0.2` | Fraction of images held out for validation |
209
+
210
+ **Examples**
211
+
212
+ ```bash
213
+ # Train a drone detector
214
+ magiclabel train ./images ./labels --name uav.pt --epochs 100
215
+
216
+ # Bigger base model = more accurate (needs more data/compute)
217
+ magiclabel train ./images ./labels --name uav.pt --base yolo26s.pt --epochs 100
218
+
219
+ # Continue improving YOUR model with new data
220
+ magiclabel train ./more_images ./more_labels --name uav2.pt --base uav.pt
221
+ ```
222
+
223
+ Output: your named `.pt` in the current directory, plus a `<name>_dataset/` folder containing the train/val split and `data.yaml`.
224
+
225
+ **Then test it:**
226
+
227
+ ```bash
228
+ magiclabel predict ./test_images --model uav.pt # annotated images saved for you
229
+ ```
230
+
231
+ > A useful model needs **dozens‑to‑hundreds of labeled images** and enough epochs. Training is much faster on a GPU (CUDA/Apple‑Silicon used automatically). Test on images the model has never seen.
232
+
233
+ ---
234
+
235
+ ### `predict` — test a model on images or video
236
+
237
+ Runs inference on an image, a **video**, or a folder of images and saves the **annotated results** (boxes drawn on) automatically. This is the one‑liner version of the usual "load the model, call `.predict(source, save=True)`" Python snippet.
238
+
239
+ ```bash
240
+ magiclabel predict SOURCE [OPTIONS]
241
+ ```
242
+
243
+ | Option | Default | Description |
244
+ | ---------- | ---------------- | -------------------------------------------------- |
245
+ | `SOURCE` | *(required)* | An image, a video (`.mp4`, `.avi`, …), or a directory of images |
246
+ | `--model` | `yolo26n.pt` | Model to run — e.g. your trained `uav.pt` |
247
+ | `--conf` | `0.25` | Confidence threshold |
248
+ | `--output` | `./predictions` | Directory for annotated results (saved in `run/`) |
249
+
250
+ **Examples**
251
+
252
+ ```bash
253
+ # Test your drone model on a video — saves an annotated video
254
+ magiclabel predict ./drone-comp.mp4 --model uav.pt
255
+
256
+ # Test on a folder of new images
257
+ magiclabel predict ./test_images --model uav.pt --conf 0.25
258
+
259
+ # Weak model finding nothing? Lower the threshold to see what it's unsure about
260
+ magiclabel predict ./test_images --model uav.pt --conf 0.1
261
+ ```
262
+
263
+ Output: annotated copies of your input in `./predictions/run/`, plus a summary (`✓ 87 detections over 240 frame(s)/image(s)`).
264
+
265
+ > `predict` draws boxes on media for **eyeballing results**; `detect` writes YOLO label files for **building datasets**. Use `predict` to judge your model, `detect` to label with it.
266
+
267
+ ---
268
+
269
+ ### `yolo2coco` — export labels to COCO JSON
270
+
271
+ Converts a YOLO detection dataset into a single COCO‑format JSON (for Detectron2, MMDetection, etc.).
272
+
273
+ ```bash
274
+ magiclabel yolo2coco IMAGES_DIR LABELS_DIR OUTPUT_JSON --class NAME [--class NAME ...]
275
+ ```
276
+
277
+ | Argument / Option | Description |
278
+ | ----------------- | --------------------------------------------------------- |
279
+ | `IMAGES_DIR` | Directory of images |
280
+ | `LABELS_DIR` | Directory of YOLO detection labels |
281
+ | `OUTPUT_JSON` | Path to write the COCO JSON file |
282
+ | `--class` | Class names **in class‑id order** (0, 1, 2, …), repeated |
283
+
284
+ **Example**
285
+
286
+ ```bash
287
+ magiclabel yolo2coco ./images ./labels annotations.json --class drone
288
+ # ✓ COCO JSON saved to annotations.json (1359 images, 696 annotations, 1 categories)
289
+ ```
290
+
291
+ Any class id found in the labels without a `--class` name is auto‑named `class_<id>` (with a warning), so the export always succeeds.
292
+
293
+ ---
294
+
295
+ ### `export-classes` — list a model's class names
296
+
297
+ ```bash
298
+ magiclabel export-classes [MODEL]
299
+ ```
300
+
301
+ **Examples**
302
+
303
+ ```bash
304
+ magiclabel export-classes yolo26n.pt
305
+ # 0: person
306
+ # 1: bicycle
307
+ # ...
308
+
309
+ magiclabel export-classes uav.pt
310
+ # 0: drone ← proof your trained model contains only YOUR classes
311
+ ```
312
+
313
+ ---
314
+
315
+ ## Using MagicLabel in Google Colab
316
+
317
+ ```python
318
+ # Cell 1 — install
319
+ !pip install magiclabel
320
+
321
+ # Cell 2 — mount Drive so your work survives the session
322
+ from google.colab import drive
323
+ drive.mount('/content/drive')
324
+
325
+ # Cell 3 — auto-label
326
+ !magiclabel detect /content/drive/MyDrive/data/images --output /content/drive/MyDrive/data/labels
327
+
328
+ # Cell 4 — review with a public link (auto-enabled on Colab; --share to be explicit)
329
+ !magiclabel review /content/drive/MyDrive/data/images /content/drive/MyDrive/data/labels --share
330
+ # → click the printed https://xxxx.gradio.live link
331
+
332
+ # Cell 5 — train (Runtime → Change runtime type → T4 GPU for speed)
333
+ !magiclabel train /content/drive/MyDrive/data/images /content/drive/MyDrive/data/labels \
334
+ --name uav.pt --epochs 100
335
+
336
+ # Cell 6 — test the model on a video or new images (annotated output saved)
337
+ !magiclabel predict /content/drive/MyDrive/data/drone-comp.mp4 --model uav.pt \
338
+ --output /content/drive/MyDrive/data/predictions
339
+ ```
340
+
341
+ - Use the **`gradio.live` URL**, not `127.0.0.1` — local addresses point at the Colab VM.
342
+ - The `review` cell stays "running" while the server is live; stop the cell to shut it down.
343
+ - **Save to Drive** — Colab wipes the VM when the session ends.
344
+ - The share link tunnels through Gradio's servers; for sensitive images, review locally instead.
345
+
346
+ ---
347
+
348
+ ## Label format
349
+
350
+ MagicLabel reads and writes the **YOLO detection** format: one `.txt` per image, sharing the image's base name (`drone1.jpg` → `drone1.txt`). Each line is one box:
351
+
352
+ ```text
353
+ <class_id> <x_center> <y_center> <width> <height>
354
+ ```
355
+
356
+ All coordinates are **normalized to 0–1**. Example:
357
+
358
+ ```text
359
+ 0 0.501818 0.467213 0.669091 0.573770
360
+ ```
361
+
362
+ A `classes.txt` alongside the labels maps ids to names (id = line number):
363
+
364
+ ```text
365
+ drone
366
+ ```
367
+
368
+ Images without a `.txt` (or with an empty one) are valid — they count as background/negative samples during training.
369
+
370
+ ---
371
+
372
+ ## Tips & troubleshooting
373
+
374
+ - **Model downloads:** the first run of a new model downloads its weights into the current directory and reuses them after.
375
+ - **No boxes from your trained model?** Lower the threshold: `--conf 0.1` (or `0.05`) to surface weak predictions.
376
+ - **Speed:** detection is fine on CPU; training really wants a GPU (used automatically when present).
377
+ - **"No images found":** the source must be a directory of images with a supported extension.
378
+ - **Port 7860 already in use:** an old review server is still running:
379
+ ```bash
380
+ kill $(lsof -nP -tiTCP:7860 -sTCP:LISTEN)
381
+ ```
382
+ - **Review doesn't hot-reload** — stop it (<kbd>Ctrl</kbd>+<kbd>C</kbd>) and relaunch after changing files on disk.
383
+
384
+ ---
385
+
386
+ ## Changelog
387
+
388
+ ### 0.3.0
389
+ - New `predict` command — test a model on an image, **video**, or folder; annotated results saved automatically.
390
+ - **YOLO26 by default** — `detect`, `train --base`, `review --model`, and `export-classes` now default to `yolo26n.pt` (newer generation than YOLOv8).
391
+ - Documentation overhaul: full command reference with examples, Google Colab guide.
392
+
393
+ ### 0.2.1
394
+ - **Fixed:** `detect` no longer overwrites `classes.txt` — the class list is merged and kept compact (only detected classes get ids), so custom classes added in `review` stay consistent.
395
+ - **Fixed:** `train` now compacts the dataset to the classes actually used, remapping ids to `0..K‑1` — out‑of‑range label ids can no longer crash training ("Label class N exceeds dataset class count").
396
+
397
+ ### 0.2.0
398
+ - New `train` command — fine‑tune a named custom `.pt` on your labeled dataset.
399
+ - Open‑vocabulary detection: `detect --prompt "anything,you,name"` (YOLO‑World).
400
+ - `review`: free‑text custom class names with a persistent `classes.txt` registry; `--share` for Colab/remote use (auto‑detected on Colab); class names shown instead of raw ids; per‑box delete, Clear All, and save‑then‑reload verification.
401
+ - Rewritten `yolo2coco` (correct YOLO→COCO conversion, auto‑named missing classes).
402
+
403
+ ### 0.1.0
404
+ - Initial release: `detect`, `review`, `yolo2coco`, `export-classes`.
405
+
406
+ ---
407
+
408
+ ## Project structure
409
+
410
+ ```text
411
+ magiclabel/
412
+ ├── cli.py # Typer CLI entry point (detect, review, train, predict, yolo2coco, export-classes)
413
+ ├── annotator.py # YOLO / YOLO-World detection → label files + classes.txt; predict helper
414
+ ├── review_app.py # Gradio review/annotation web app
415
+ ├── trainer.py # dataset build + custom model training
416
+ ├── convertor.py # YOLO → COCO conversion
417
+ └── utils.py # image discovery + helpers
418
+ ```
419
+
420
+ ---
421
+
422
+ ## License
423
+
424
+ Released under the [MIT License](LICENSE).