hbb2obb 1.0.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.
hbb2obb-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Robert Fonod
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
hbb2obb-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,496 @@
1
+ Metadata-Version: 2.4
2
+ Name: hbb2obb
3
+ Version: 1.0.0
4
+ Summary: Toolkit for converting horizontal bounding boxes to oriented bounding boxes using segmentation models and evaluation of the conversion against ground truth.
5
+ Author-email: Robert Fonod <robert.fonod@ieee.org>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 Robert Fonod
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/rfonod/hbb2obb/
29
+ Project-URL: Repository, https://github.com/rfonod/hbb2obb/
30
+ Project-URL: Changelog, https://github.com/rfonod/hbb2obb/releases
31
+ Project-URL: Issues, https://github.com/rfonod/hbb2obb/issues/
32
+ Keywords: bounding-box,oriented-bounding-box,rotated-bounding-box,horizontal-bounding-box,axis-aligned-bounding-box,object detection,segmentation,evaluation,computer-vision,aerial-imagery,drone-footage,remote-sensing,annotation-tools,label-conversion,YOLO-format,COCO-format,evaluation-metrics,iou-evaluation,hyperparameter-optimization
33
+ Classifier: Development Status :: 5 - Production/Stable
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Operating System :: OS Independent
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3.9
38
+ Classifier: Programming Language :: Python :: 3.10
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Classifier: Programming Language :: Python :: 3.13
42
+ Classifier: Intended Audience :: Developers
43
+ Classifier: Intended Audience :: Science/Research
44
+ Classifier: Topic :: Scientific/Engineering
45
+ Classifier: Topic :: Software Development
46
+ Requires-Python: >=3.9
47
+ Description-Content-Type: text/markdown
48
+ License-File: LICENSE
49
+ Requires-Dist: ultralytics>=8.3
50
+ Requires-Dist: shapely>=2.0.7
51
+ Provides-Extra: dev
52
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
53
+ Requires-Dist: ruff>=0.11.0; extra == "dev"
54
+ Dynamic: license-file
55
+
56
+ # HBB2OBB: Horizontal to Oriented Bounding Box Conversion and Evaluation Tool
57
+
58
+ [![PyPI Version](https://img.shields.io/pypi/v/hbb2obb)](https://pypi.org/project/hbb2obb/) [![GitHub Release](https://img.shields.io/github/v/release/rfonod/hbb2obb?include_prereleases)](https://github.com/rfonod/hbb2obb/releases) [![Python](https://img.shields.io/badge/python-3.9%2B-blue)](https://www.python.org/) [![License](https://img.shields.io/github/license/rfonod/hbb2obb)](https://github.com/rfonod/hbb2obb/blob/main/LICENSE) [![DOI](https://zenodo.org/badge/960660341.svg)](https://doi.org/10.5281/zenodo.15151143) [![Development Status](https://img.shields.io/badge/development-active-brightgreen)](https://github.com/rfonod/hbb2obb) ![PyPi - Total Downloads](https://img.shields.io/pepy/dt/hbb2obb?label=total%20downloads) ![PyPI - Downloads per Month](https://img.shields.io/pypi/dm/hbb2obb?color=%234c1)
59
+
60
+ **HBB2OBB** is a Python tool designed to convert horizontal bounding boxes (HBBs), also known as axis-aligned bounding boxes, into oriented bounding boxes (OBBs), also referred to as rotated bounding boxes, using segmentation models from the SAM (Segment Anything Model) family. This tool addresses a critical need in object detection tasks where objects appear in arbitrary orientations, such as in aerial imagery, satellite data, or traffic monitoring scenarios. The conversion utilizes user-provided HBB annotations as prompts for SAM models, leveraging their state-of-the-art segmentation capabilities to accurately delineate object boundaries and generate precise OBBs that better encapsulate non-upright objects.
61
+
62
+ The conversion process employs a model ensemble approach that combines masks from multiple segmentation models through majority voting, resulting in enhanced accuracy and robustness. The system implements spatial constraint techniques including region-specific masking and contour refinement to ensure the segmentation remains within relevant object boundaries. The library supports flexible scaling of input HBBs (both positive and negative factors) to accommodate for potentially cropped object parts or overly conservative annotations. If no valid mask is detected, a fallback strategy maintains the original HBB as the OBB, ensuring consistent outputs.
63
+
64
+ Beyond conversion, HBB2OBB offers comprehensive evaluation tools to assess OBB accuracy against ground truth annotations, hyperparameter optimization capabilities to fine-tune the conversion process for specific datasets, and utilities for format conversion between COCO JSON and YOLO TXT. The package includes intuitive visualization features that render the conversion process transparently, displaying the progression from original HBBs through segmentation masks to final OBBs. Designed with ease of use in mind, HBB2OBB provides both an intuitive command-line interface and a flexible Python API for seamless integration into existing workflows.
65
+
66
+ ![HBB to OBB Conversion Example](https://raw.githubusercontent.com/rfonod/hbb2obb/main/assets/hbb2obb_illustration.gif?raw=True)
67
+
68
+
69
+ ## Features
70
+
71
+ - **Conversion from HBB to OBB**: Automatically converts YOLO format horizontal bounding boxes to oriented bounding boxes
72
+ - **Segmentation-Based Approach**: Uses state-of-the-art segmentation models for accurate object boundary detection
73
+ - **Multiple Model Support**: Compatible with various SAM model variants (SAM, SAM2, SAM2.1, Mobile SAM, and FastSAM families, see [ultralytics documentation](https://docs.ultralytics.com/models/sam/) for details)
74
+ - **Model Ensemble**: Ability to combine outputs from multiple segmentation models for enhanced accuracy through majority voting
75
+ - **Evaluation Tools**: Includes tools to evaluate OBB accuracy against ground truth using IoU metrics
76
+ - **Hyperparameter Optimization Tool**: Finds optimal hyperparameters for HBB2OBB conversion by evaluating different combinations of SAM inference resolutions and scale factors used to enlarge/shrink HBBs
77
+ - **Visualization Tools**: Tools to visualize the conversion process, including HBBs, segmentation masks, derived contours, and the resulting OBBs, as well as the evaluation results
78
+ - **Format Conversion Utilities**: Tools to convert between COCO JSON and YOLO TXT formats for both HBB and OBB annotations
79
+
80
+ <details>
81
+ <summary><b>🚀 Planned Enhancements</b></summary>
82
+
83
+ - **Support for Other Formats**: Add support for other annotation formats (e.g., COCO, Pascal VOC)
84
+ - **Improved Morphological Operations**: Implement more advanced morphological operations for better mask refinement
85
+ - **Integration with Other Libraries**: Integrate with popular object detection libraries to alleviate the need for HBB annotations
86
+ - **Support for Other Segmentation Models**: Extend compatibility to other segmentation models
87
+
88
+ </details>
89
+
90
+ ## Installation
91
+
92
+ It is recommended to create and activate a **Python Virtual Environment** (Python >= 3.9) first using e.g., [Miniconda3](https://docs.anaconda.com/free/miniconda/):
93
+ ```bash
94
+ conda create -n hbb2obb python=3.11 -y
95
+ conda activate hbb2obb
96
+ ```
97
+
98
+ Then, install the hbb2obb library using one of the following options:
99
+
100
+ ### Option 1: Install from PyPI
101
+ ```bash
102
+ pip install hbb2obb
103
+ ```
104
+
105
+ ### Option 2: Install from Local Source
106
+
107
+ You can also clone the repository and install the package from the local source:
108
+
109
+ ```bash
110
+ git clone https://github.com/rfonod/hbb2obb.git
111
+ cd hbb2obb && pip install .
112
+ ```
113
+
114
+ If you want the changes you make in the repo to be reflected in your install, use `pip install -e .` instead of `pip install .`.
115
+
116
+
117
+ ## CLI Usage
118
+
119
+ ### Converting HBB to OBB
120
+
121
+ To convert HBBs to OBBs using default parameters (single SAM model `sam_b`), run:
122
+
123
+ ```bash
124
+ hbb2obb /path/to/images --hbb_dir /path/to/hbb/annotations
125
+ ```
126
+
127
+ For enhanced accuracy using multiple segmentation models (model ensemble):
128
+
129
+ ```bash
130
+ hbb2obb /path/to/images --hbb_dir /path/to/hbb/annotations --sam_models sam_b sam_l sam2_b sam2.1_b
131
+ ```
132
+
133
+ To adjust scale factors (useful for recovering cropped object parts or handling conservative HBBs):
134
+
135
+ ```bash
136
+ # Positive scale factor to expand HBBs (helps recover cropped parts)
137
+ hbb2obb /path/to/images --scale_factors 0.1
138
+
139
+ # Negative scale factor to shrink HBBs (useful when HBBs are too conservative)
140
+ hbb2obb /path/to/images --scale_factors -0.02
141
+
142
+ # Different scale factors for short and long sides of the HBB
143
+ hbb2obb /path/to/images --scale_factors 0.1 0.05
144
+ ```
145
+
146
+ To visualize the conversion process, add the `--save_img` flag:
147
+
148
+ ```bash
149
+ hbb2obb /path/to/images --save_img
150
+ ```
151
+
152
+ <details>
153
+ <summary><b>More CLI Arguments</b></summary>
154
+
155
+ For a complete list of CLI arguments and their descriptions, run:
156
+ ```bash
157
+ hbb2obb --help
158
+ ```
159
+
160
+ Key arguments include:
161
+ - `--hbb_dir`: Directory containing HBB annotations (YOLO TXT format)
162
+ - `--obb_dir`: Directory to save OBB annotations (default: `labels_obb` in the parent directory of source images)
163
+ - `--sam_models`: List of SAM models to use (e.g., sam_b, sam_l, sam2_b, sam2.1_b, mobile_sam, FastSAM-s)
164
+ - `--imgsz`: SAM inference resolution
165
+ - `--scale_factors`: Factors to scale HBBs (can be single value or separate for x and y)
166
+ - `--opening_kernel_percentage`: Size of the morphological opening kernel as a percentage of the mask's smaller dimension
167
+ - `--save_img`: Whether to save visualization images
168
+ - `--viz_dir`: Directory to save visualization images (default: same as `--obb_dir`)
169
+ - `--hide_hbb`, `--hide_obb`, `--hide_masks`, `--hide_segments`, `--hide_labels`: Control what gets visualized
170
+ - `--model_kwargs`: Additional keyword arguments for SAM models, see [ultralytics documentation](https://docs.ultralytics.com/models/sam/) for details
171
+ </details>
172
+
173
+ ### Evaluating OBB Predictions
174
+
175
+ To evaluate the accuracy of OBB predictions against ground truth annotations:
176
+
177
+ ```bash
178
+ hbb2obb-eval /path/to/ground_truth /path/to/predictions
179
+ ```
180
+
181
+ <details>
182
+ <summary><b>More Evaluation Arguments</b></summary>
183
+
184
+ For a complete list of evaluation arguments, run:
185
+ ```bash
186
+ hbb2obb-eval --help
187
+ ```
188
+
189
+ Key arguments include:
190
+ - `--excluded_classes`: List of class IDs to exclude from evaluation
191
+ - `--iou_threshold`: IoU threshold for considering a ground truth and prediction pair as a match
192
+ - `--class_agnostic`: Whether to ignore class label matching requirement (useful for re-classified objects in GT)
193
+ - `--exclude_edge_cases`: Whether to exclude cases where the OBB is too close to the image edge
194
+ - `--edge_tolerance`: Tolerance for edge cases in pixels
195
+ - `--img_width`, `--img_height`: Image dimensions (for edge case detection)
196
+ - `--label_map`: Path to label map YAML file that maps class IDs to class names
197
+ </details>
198
+
199
+
200
+ ## Python API Usage
201
+
202
+ <details>
203
+ <summary><b>Converting HBB to OBB</b></summary>
204
+
205
+ ```python
206
+ from hbb2obb.converter import hbb2obb, save_obb_annotations
207
+
208
+ # Basic usage with a single SAM model
209
+ results = hbb2obb(
210
+ img_path="/path/to/images",
211
+ hbb_dir="/path/to/hbb/annotations",
212
+ sam_models="sam_b",
213
+ imgsz=1280,
214
+ scale_factors=0.05,
215
+ opening_kernel_percentage=0.15,
216
+ save_img=True,
217
+ viz_dir="/path/to/save/visualizations",
218
+ show_hbb=True,
219
+ show_masks=True,
220
+ show_segments=True,
221
+ show_obb=True,
222
+ show_labels=True,
223
+ )
224
+
225
+ # Enhanced accuracy using multiple SAM models (model ensemble)
226
+ results = hbb2obb(
227
+ img_path="/path/to/images",
228
+ hbb_dir="/path/to/hbb/annotations",
229
+ sam_models=["sam_b", "sam_l", "sam2_b", "sam2.1_b"],
230
+ imgsz=1280,
231
+ scale_factors=[0.1, 0.05], # Different scale factors for short and long sides
232
+ opening_kernel_percentage=0.15,
233
+ save_img=True,
234
+ viz_dir="/path/to/save/visualizations",
235
+ )
236
+
237
+ # Save the resulting OBB annotations
238
+ save_obb_annotations(results["obb_annotations"], "/path/to/save/obb/annotations")
239
+ ```
240
+ </details>
241
+
242
+
243
+ <details>
244
+ <summary><b>Evaluating OBB Predictions</b></summary>
245
+
246
+ ```python
247
+ from hbb2obb.evaluator import evaluate_obb, print_results
248
+
249
+ # Basic evaluation
250
+ results = evaluate_obb(
251
+ gt_dir="/path/to/ground_truth_annotations",
252
+ pred_dir="/path/to/predictions",
253
+ iou_threshold=0.1,
254
+ )
255
+
256
+ # Class-agnostic evaluation (useful when GT has re-classified objects)
257
+ results = evaluate_obb(
258
+ gt_dir="/path/to/ground_truth_annotations",
259
+ pred_dir="/path/to/predictions",
260
+ iou_threshold=0.1,
261
+ class_agnostic=True,
262
+ exclude_edge_cases=True,
263
+ edge_tolerance=1,
264
+ img_width=3840,
265
+ img_height=2160,
266
+ )
267
+
268
+ # Print evaluation results with class names from label map
269
+ print_results(results, "/path/to/label_map.yaml")
270
+ ```
271
+
272
+ </details>
273
+
274
+
275
+ ## Utility Scripts
276
+
277
+ ### Format Conversion
278
+
279
+ **COCO JSON to YOLO TXT** (supports both HBB and OBB annotations):
280
+
281
+ ```bash
282
+ python scripts/json2yolo.py /path/to/json -mp /path/to/label_map.yaml
283
+ ```
284
+ The `-mp` flag is optional and can be used to specify a label map file. If not provided, the script will create a default (first-come-first-serve) label map.
285
+
286
+ **YOLO TXT to COCO JSON** (supports both HBB and OBB annotations):
287
+ ```bash
288
+ python scripts/yolo2json.py /path/to/yolo /path/to/label_map.yaml
289
+ ```
290
+ Here, the label map file is required to convert the numerical class IDs to class names in the JSON output. The output JSON format is compatible with annotation tools like [LabelMe](https://github.com/wkentaro/labelme).
291
+
292
+ ### Hyperparameter Optimization
293
+
294
+ To find the optimal hyperparameters for the default SAM model (`sam_b`), run:
295
+
296
+ ```bash
297
+ python scripts/optimize_hbb2obb.py /path/to/images path/to/ground_truth_annotations
298
+ ```
299
+
300
+ This evaluates different combinations of:
301
+ - SAM inference resolutions
302
+ - Scale factors to enlarge/shrink HBBs
303
+
304
+ The script can be run for different SAM models or combinations of models. For example, to evaluate multiple SAM models:
305
+ ```bash
306
+ python scripts/optimize_hbb2obb.py /path/to/images path/to/ground_truth_annotations -sm sam_b sam_l sam2_b sam2.1_b -n multi_sam
307
+ ```
308
+
309
+ To visualize optimization results:
310
+ ```bash
311
+ python scripts/plot_optimization_results.py /path/to/optimization_results
312
+ ```
313
+
314
+
315
+ ## Data Format
316
+
317
+ ### HBB Annotations (Input)
318
+
319
+ HBB annotations should be in YOLO TXT format (one file per image):
320
+ ```
321
+ class_id x_center y_center width height
322
+ ```
323
+ The coordinates can be in relative format (0-1) or absolute pixel coordinates.
324
+
325
+ ### OBB Annotations (Output)
326
+
327
+ OBB annotations are saved in the following YOLO TXT format (one file per image):
328
+ ```
329
+ class_id x1 y1 x2 y2 x3 y3 x4 y4
330
+ ```
331
+ Where (x1,y1), (x2,y2), (x3,y3), (x4,y4) are the four corner coordinates of the rotated bounding box in absolute pixel coordinates.
332
+
333
+ ### Label Map (Optional)
334
+
335
+ Label map is a YAML file mapping class IDs to class names. For example:
336
+ ```yaml
337
+ 0: Car
338
+ 1: Bus
339
+ 2: Truck
340
+ 3: Motorcycle
341
+ # ...
342
+ ```
343
+
344
+ ## Example Workflow
345
+
346
+ ### Basic Workflow
347
+ Below is a simple example of how to use the HBB2OBB tool for converting HBB annotations to OBB annotations and evaluating the results. This example assumes you have a dataset with images and HBB annotations in YOLO format. Steps 2-4 are optional and can be skipped if you only want to convert HBBs to OBBs.
348
+
349
+ 0. **Prepare HBB annotations in YOLO format**
350
+ ```
351
+ dataset/
352
+ ├── images/
353
+ │ ├── img1.jpg
354
+ │ ├── img2.jpg
355
+ │ └── ...
356
+ ├── labels_hbb/
357
+ │ ├── img1.txt
358
+ │ ├── img2.txt
359
+ │ └── ...
360
+ ├── labels_obb_gt/ (optional)
361
+ │ ├── img1.txt
362
+ │ ├── img2.txt
363
+ │ └── ...
364
+ └── classes.yaml (optional)
365
+ ```
366
+
367
+ 💡 **Note:** The [data](data/) folder in this repository contains a sample dataset to test the conversion and evaluation processes as well as the parameter optimization and visualization scripts. The README file inside the data folder contains detailed instructions and commands on how to reproduce the results.
368
+
369
+ 1. **Convert HBB to OBB annotations and visualize the transformation using default parameters**
370
+ ```bash
371
+ hbb2obb data/images --save_img
372
+ ```
373
+
374
+ 2. **Evaluate OBB predictions against ground truth annotations**
375
+ ```bash
376
+ hbb2obb-eval data/labels_obb_gt data/labels_obb -lm data/classes.yaml
377
+ ```
378
+
379
+ 3. **Optimize hyperparameters for HBB2OBB conversion using a light-weight SAM model**
380
+ ```bash
381
+ python scripts/optimize_hbb2obb.py data/images data/labels_obb_gt -sm sam2_s -n sam2_s
382
+ ```
383
+ 4. **Visualize optimization results**
384
+ ```bash
385
+ python scripts/plot_optimization_results.py data/benchmark_results/sam2_s
386
+ ```
387
+
388
+ ### Complete Workflow with LabelMe JSON Annotations
389
+
390
+ <details>
391
+ <summary><b>Detailed Step-by-Step Guide with LabelMe</b></summary>
392
+
393
+ 1. **Start with LabelMe JSON annotations for HBB and OBB ground truth**
394
+ ```
395
+ project/
396
+ ├── images/
397
+ │ ├── img1.jpg
398
+ │ ├── img2.jpg
399
+ │ └── ...
400
+ ├── json_hbb/
401
+ │ ├── img1.json
402
+ │ ├── img2.json
403
+ │ └── ...
404
+ └── json_obb_gt/ (ground truth)
405
+ ├── img1.json
406
+ ├── img2.json
407
+ └── ...
408
+ ```
409
+ 💡 [LabelMe](https://github.com/wkentaro/labelme) is a popular annotation tool that can be used to create both horizontal and oriented bounding box annotations in JSON format. It supports polygonal annotations which can be converted to OBB format.
410
+
411
+ 2. **Convert JSON annotations to YOLO format**
412
+ ```bash
413
+ # Convert HBB JSON to YOLO TXT
414
+ python scripts/json2yolo.py project/json_hbb -o project/labels_hbb
415
+
416
+ # Convert OBB ground truth JSON to YOLO TXT
417
+ python scripts/json2yolo.py project/json_obb_gt -o project/labels_obb_gt
418
+ ```
419
+
420
+ 3. **Run hyperparameter optimization to find the best settings**
421
+ ```bash
422
+ python scripts/optimize_hbb2obb.py project/images project/labels_obb_gt -sm sam_b sam_l sam2_b sam2.1_b -n multi_sam
423
+ ```
424
+
425
+ 4. **Generate OBBs using the optimal parameters from the results**
426
+ ```bash
427
+ # Check the best parameters from the optimization results
428
+ cat project/benchmark_results/multi_sam/summary.txt
429
+
430
+ # Use those parameters for conversion (example values)
431
+ hbb2obb project/images --hbb_dir project/labels_hbb --obb_dir project/labels_obb \
432
+ --sam_models sam_b sam_l sam2_b --imgsz 1280 --scale_factors 0.05 \
433
+ --opening_kernel_percentage 0.15 --save_img --viz_dir project/visualizations
434
+ ```
435
+
436
+ 5. **Evaluate the OBB predictions against ground truth**
437
+ ```bash
438
+ hbb2obb-eval project/labels_obb_gt project/labels_obb -mp project/label_map.yaml
439
+ ```
440
+
441
+ 6. **Convert the generated OBB annotations back to JSON format for visualization in LabelMe**
442
+ ```bash
443
+ python scripts/yolo2json.py project/labels_obb project/label_map.yaml -jd project/json_obb
444
+ ```
445
+
446
+ 7. **Open the visualizations or JSON annotations in LabelMe for manual review**
447
+ ```bash
448
+ labelme project/images --output project/json_obb --nodata
449
+ ```
450
+ </details>
451
+
452
+ ## Technical Details
453
+
454
+ The HBB to OBB conversion process involves the following steps:
455
+
456
+ 1. **Load HBB annotations**: Parse YOLO TXT format annotations
457
+ 2. **Scale bounding boxes**: Scale HBB slightly to ensure complete object coverage
458
+ - Positive scale factors: Expand HBBs to recover potentially cropped object parts
459
+ - Negative scale factors: Shrink HBBs when they are overly conservative
460
+ - Different scale factors can be applied to shorter vs. longer sides of the HBB
461
+ 3. **Segmentation**: Use SAM model(s) to generate object masks based on the HBB prompts
462
+ 4. **Mask aggregation**:
463
+ - When using multiple models (model ensemble), masks are combined through majority voting
464
+ - The aggregated mask is clipped to the scaled HBB region
465
+ - Morphological opening is applied to refine the mask
466
+ 5. **Contour extraction**: Extract contours from the largest refined mask per object
467
+ 6. **OBB computation**: Calculate minimum-area oriented bounding boxes from the contours
468
+ 7. **Fall-back strategy**: If no valid mask is detected inside an HBB, the original HBB is used as the OBB
469
+ 8. **Visualization (optional)**: Generate images with HBBs, segmentation masks, contours, and OBB overlays
470
+
471
+ Key characteristics:
472
+ - **Label preservation**: OBBs inherit the class labels from their corresponding HBBs (no re-classification)
473
+ - **Corrective effects**: The transformation may correct errors in the original HBBs by:
474
+ - Recovering cropped object parts through positive scale factors
475
+ - Creating tighter bounding boxes through precise segmentation
476
+
477
+ ## Best Practices
478
+
479
+ - For optimal results, use a combination of SAM models, e.g., `--sam_models sam_b sam_l sam2_b sam2.1_b`
480
+ - Experiment with different scale factors and inference resolutions based on your dataset characteristics
481
+ - Run the hyperparameter optimization script to find the best settings for your specific data
482
+ - Use class-agnostic evaluation when comparing with manually annotated ground truth that might have different class labels than the original HBBs
483
+ - Visualize the conversion process to understand how the model is interpreting the HBBs and generating OBBs
484
+ - Regularly check for updates to the library and SAM models for improved performance and new features
485
+
486
+ ## Limitations
487
+ - The tool relies on the quality of the HBB annotations and the SAM models used for segmentation. Poorly annotated HBBs or low-quality segmentation models may lead to inaccurate OBBs.
488
+ - The conversion process may not work well for highly occluded or complex objects where the HBB does not provide sufficient context for the SAM model to generate accurate masks.
489
+
490
+ ## Contributing
491
+
492
+ Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open a GitHub Issue or submit a pull request.
493
+
494
+ ## License
495
+
496
+ This project is distributed under the MIT License. See the [LICENSE](LICENSE) file for more details.