File size: 12,071 Bytes
0a216c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
Run old models (Line, Border, Zones) and convert predictions to COCO format.
"""
import os
import json
import numpy as np
from pathlib import Path
from ultralytics import YOLO, YOLOE
import tempfile
from typing import Dict, List
import pycocotools.mask as mask_util
import cv2


# Model files (same as app_original_app_with_three_models.py)
MODEL_FILES = {
    "Line Detection": "best_line_detection_yoloe (1).pt",
    "Border Detection": "border_model_weights.pt",
    "Zones Detection": "zones_model_weights.pt"
}

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = os.path.dirname(os.path.dirname(SCRIPT_DIR))


def load_old_models():
    """Load the three old models."""
    models = {}
    for name, model_file in MODEL_FILES.items():
        model_path = os.path.join(PROJECT_ROOT, model_file)
        if os.path.exists(model_path):
            try:
                if name == "Line Detection":
                    models[name] = YOLOE(model_path)
                else:
                    models[name] = YOLO(model_path)
                print(f"✓ Loaded {name} model")
            except Exception as e:
                print(f"✗ Error loading {name} model: {e}")
                models[name] = None
        else:
            print(f"✗ Model file not found: {model_path}")
            models[name] = None
    return models


def results_to_coco(result, model_name, image_id, image_width, image_height, category_map):
    """
    Convert YOLO result to COCO format annotations.
    Handles masks properly for YOLOE Line Detection model (like app.py).
    
    Args:
        result: YOLO Results object (single result, not list)
        model_name: Name of the model (for special handling)
        image_id: COCO image ID
        image_width: Image width
        image_height: Image height
        category_map: Dict mapping class names to COCO category IDs
    
    Returns:
        List of COCO annotation dictionaries
    """
    annotations = []
    ann_id = 1
    
    if result is None:
        return annotations
    
    # Get boxes and masks
    boxes = result.boxes
    if boxes is None:
        return annotations
    
    # Get masks if available
    masks = result.masks
    has_masks = masks is not None and len(masks) > 0
    
    num_detections = len(boxes)
    
    for i in range(num_detections):
        # Get box coordinates
        box = boxes.xyxy[i].cpu().numpy()  # [x1, y1, x2, y2]
        x1, y1, x2, y2 = box
        
        # Get class
        cls_id = int(boxes.cls[i].cpu().numpy())
        cls_name = result.names[cls_id]
        
        # Map "object" to "line" for Line Detection model (like app.py)
        if model_name == "Line Detection" and cls_name == "object":
            cls_name = "line"
        
        # Skip if class not in category map
        if cls_name not in category_map:
            continue
        
        # Get confidence
        conf = float(boxes.conf[i].cpu().numpy())
        
        # Convert bbox to COCO format [x, y, width, height]
        bbox = [float(x1), float(y1), float(x2 - x1), float(y2 - y1)]
        
        # Get segmentation
        segmentation = None
        area = bbox[2] * bbox[3]  # Default to bbox area
        
        if has_masks and i < len(masks.data):
            try:
                # Get mask (like app.py handles YOLOE masks)
                mask = masks.data[i].cpu().numpy()
                
                # Handle mask resizing similar to app.py
                if mask.shape != (image_height, image_width):
                    # Resize mask to image size using cv2 (like app.py)
                    mask_np = (mask > 0).astype(np.uint8)
                    resized_mask = cv2.resize(
                        mask_np,
                        (image_width, image_height),
                        interpolation=cv2.INTER_NEAREST
                    )
                    mask = resized_mask.astype(np.uint8)
                else:
                    mask = (mask > 0.5).astype(np.uint8)
                
                # Convert to COCO RLE format
                rle = mask_util.encode(np.asfortranarray(mask))
                if isinstance(rle['counts'], bytes):
                    rle['counts'] = rle['counts'].decode('utf-8')
                segmentation = rle
                area = float(mask_util.area(rle))
            except Exception as e:
                print(f"Warning: Failed to convert mask to RLE for detection {i}: {e}")
                # Fall back to bbox
                pass
        
        # Create COCO annotation
        ann = {
            "id": ann_id,
            "image_id": image_id,
            "category_id": category_map[cls_name],
            "bbox": bbox,
            "area": area,
            "iscrowd": 0,
            "score": conf
        }
        
        if segmentation is not None:
            ann["segmentation"] = segmentation
        
        annotations.append(ann)
        ann_id += 1
    
    return annotations


def run_old_models_on_image(image_path, models, conf_threshold=0.25, iou_threshold=0.45):
    """
    Run old models on a single image and return COCO format predictions.
    Matches the behavior of app.py for consistent results.
    
    Args:
        image_path: Path to image file
        models: Dict of loaded models
        conf_threshold: Confidence threshold
        iou_threshold: IoU threshold
    
    Returns:
        COCO format dictionary with predictions
    """
    # Load image as numpy array (like app.py does)
    image = cv2.imread(image_path)
    if image is None:
        raise ValueError(f"Failed to load image: {image_path}")
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_height, image_width = image.shape[:2]
    
    # Create category map (map all detected classes to sequential IDs)
    all_classes = set()
    results_dict = {}
    
    # Run each model
    for model_name, model in models.items():
        if model is None:
            continue
        
        try:
            # Use numpy array for prediction (like app.py)
            # Access result as [0] immediately (like app.py)
            result = model.predict(
                image,
                conf=conf_threshold,
                iou=iou_threshold
            )[0]
            
            # Collect class names and map "object" to "line" for Line Detection
            if result.names:
                for cls_id, cls_name in result.names.items():
                    # Map "object" to "line" for Line Detection model (like app.py)
                    if model_name == "Line Detection" and cls_name == "object":
                        all_classes.add("line")
                    else:
                        all_classes.add(cls_name)
            
            results_dict[model_name] = result
        except Exception as e:
            print(f"Error running {model_name}: {e}")
            import traceback
            traceback.print_exc()
            results_dict[model_name] = None
    
    # Create category mapping
    category_map = {cls_name: idx + 1 for idx, cls_name in enumerate(sorted(all_classes))}
    
    # Convert all results to COCO format
    all_annotations = []
    ann_id = 1
    
    for model_name, result in results_dict.items():
        if result is None:
            continue
        
        annotations = results_to_coco(
            result,
            model_name,
            image_id=1,  # Will be set later
            image_width=image_width,
            image_height=image_height,
            category_map=category_map
        )
        
        # Update annotation IDs
        for ann in annotations:
            ann["id"] = ann_id
            ann_id += 1
        
        all_annotations.extend(annotations)
    
    # Create COCO format
    coco = {
        "info": {"description": "Old models predictions"},
        "licenses": [],
        "images": [{
            "id": 1,
            "width": image_width,
            "height": image_height,
            "file_name": os.path.basename(image_path)
        }],
        "annotations": all_annotations,
        "categories": [
            {"id": cid, "name": name, "supercategory": ""}
            for name, cid in sorted(category_map.items(), key=lambda x: x[1])
        ]
    }
    
    return coco


def process_dataset(images_dir, output_dir, conf_threshold=0.25, iou_threshold=0.45):
    """
    Process all images in a directory with old models.
    
    Args:
        images_dir: Directory containing images
        output_dir: Directory to save COCO JSON files
        conf_threshold: Confidence threshold
        iou_threshold: IoU threshold
    
    Returns:
        Merged COCO format dictionary for all images
    """
    # Load models
    models = load_old_models()
    
    # Get all image files
    image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff'}
    image_files = [
        f for f in os.listdir(images_dir)
        if os.path.splitext(f)[1].lower() in image_extensions
    ]
    
    all_coco_dicts = []
    image_id = 1
    
    for image_file in sorted(image_files):
        image_path = os.path.join(images_dir, image_file)
        print(f"Processing {image_file}...")
        
        try:
            coco = run_old_models_on_image(
                image_path,
                models,
                conf_threshold=conf_threshold,
                iou_threshold=iou_threshold
            )
            
            # Update image ID
            coco["images"][0]["id"] = image_id
            
            # Update annotation image_ids
            for ann in coco["annotations"]:
                ann["image_id"] = image_id
            
            all_coco_dicts.append(coco)
            image_id += 1
            
            # Save individual file
            output_path = os.path.join(output_dir, f"{Path(image_file).stem}_old.json")
            with open(output_path, 'w') as f:
                json.dump(coco, f, indent=2)
        
        except Exception as e:
            print(f"Error processing {image_file}: {e}")
            continue
    
    # Merge all COCO dicts
    merged = {
        "info": {"description": "Old models predictions - merged"},
        "licenses": [],
        "images": [],
        "annotations": [],
        "categories": []
    }
    
    # Collect all categories
    all_categories = {}
    for coco in all_coco_dicts:
        for cat in coco["categories"]:
            if cat["name"] not in all_categories:
                all_categories[cat["name"]] = cat["id"]
    
    # Update category IDs to be sequential
    category_map = {name: idx + 1 for idx, name in enumerate(sorted(all_categories.keys()))}
    reverse_map = {old_id: category_map[name] for name, old_id in all_categories.items()}
    
    merged["categories"] = [
        {"id": cid, "name": name, "supercategory": ""}
        for name, cid in sorted(category_map.items(), key=lambda x: x[1])
    ]
    
    # Merge images and annotations
    ann_id = 1
    for coco in all_coco_dicts:
        merged["images"].extend(coco["images"])
        
        for ann in coco["annotations"]:
            new_ann = ann.copy()
            new_ann["id"] = ann_id
            # Update category_id using reverse_map
            old_cat_id = ann["category_id"]
            # Find category name
            cat_name = next((c["name"] for c in coco["categories"] if c["id"] == old_cat_id), None)
            if cat_name and cat_name in category_map:
                new_ann["category_id"] = category_map[cat_name]
            merged["annotations"].append(new_ann)
            ann_id += 1
    
    return merged


if __name__ == "__main__":
    # Test on single image
    test_image = "../../e-codices_bbb-0219_044r_max.jpg"
    models = load_old_models()
    coco = run_old_models_on_image(test_image, models)
    
    print(f"Predictions: {len(coco['annotations'])} annotations")
    print(f"Categories: {[c['name'] for c in coco['categories']]}")
    
    with open("test_old_models.json", "w") as f:
        json.dump(coco, f, indent=2)