File size: 13,085 Bytes
2020af8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import os
os.environ["GRADIO_TEMP_DIR"] = "./tmp"

import sys
import torch
import gradio as gr
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from transformers import (
    DFineForObjectDetection,
    RTDetrImageProcessor,
)

# == select device ==
device = 'cuda' if torch.cuda.is_available() else 'cpu'

# Available models
MODELS = {
    "Egret XLarge": "ds4sd/docling-layout-egret-xlarge",
    "Egret Large": "ds4sd/docling-layout-egret-large", 
    "Egret Medium": "ds4sd/docling-layout-egret-medium",
    "Heron 101": "ds4sd/docling-layout-heron-101",
    "Heron": "ds4sd/docling-layout-heron"
}

# Classes mapping for the docling model
classes_map = {
    0: "Caption",
    1: "Footnote", 
    2: "Formula",
    3: "List-item",
    4: "Page-footer",
    5: "Page-header",
    6: "Picture",
    7: "Section-header",
    8: "Table",
    9: "Text",
    10: "Title",
    11: "Document Index",
    12: "Code",
    13: "Checkbox-Selected",
    14: "Checkbox-Unselected",
    15: "Form",
    16: "Key-Value Region",
}

# Color mapping for visualization
colors = [
    "#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FECA57",
    "#FF9FF3", "#54A0FF", "#5F27CD", "#00D2D3", "#FF9F43",
    "#10AC84", "#EE5A24", "#0ABDE3", "#006BA6", "#F79F1F",
    "#A3CB38", "#FDA7DF"
]

# Global variables for model
current_model = None
current_processor = None
current_model_name = None

def iomin(box1, box2):
    """
    Intersection over Minimum (IoMin)
    box1: Tensor[1, 4]
    box2: Tensor[N, 4]
    Returns: Tensor[N]
    """
    # Intersection
    x1 = torch.max(box1[:, 0], box2[:, 0])
    y1 = torch.max(box1[:, 1], box2[:, 1])
    x2 = torch.min(box1[:, 2], box2[:, 2])
    y2 = torch.min(box1[:, 3], box2[:, 3])
    inter_area = torch.clamp(x2 - x1, min=0) * torch.clamp(y2 - y1, min=0)

    # Areas
    box1_area = (box1[:, 2] - box1[:, 0]) * (box1[:, 3] - box1[:, 1])
    box2_area = (box2[:, 2] - box2[:, 0]) * (box2[:, 3] - box2[:, 1])
    min_area = torch.min(box1_area, box2_area)

    return inter_area / min_area

def nms(boxes, scores, iou_threshold=0.5):
    """
    Custom NMS implementation using IoMin
    """
    keep = []
    _, order = scores.sort(descending=True)

    while order.numel() > 0:
        i = order[0]
        keep.append(i.item())

        if order.numel() == 1:
            break

        box_i = boxes[i].unsqueeze(0)  # [1, 4]
        rest = order[1:]
        ious = iomin(box_i, boxes[rest])

        mask = (ious <= iou_threshold)
        order = order[1:][mask]

    return torch.tensor(keep, dtype=torch.long)

def load_model(model_name):
    """
    Load the selected model
    """
    global current_model, current_processor, current_model_name
    
    if current_model_name == model_name:
        return f"βœ… Model {model_name} is already loaded!"
    
    try:
        print(f"Loading model: {model_name}")
        model_path = MODELS[model_name]
        
        processor = RTDetrImageProcessor.from_pretrained(model_path)
        model = DFineForObjectDetection.from_pretrained(model_path)
        model = model.to(device)
        model.eval()
        
        current_processor = processor
        current_model = model
        current_model_name = model_name
        
        return f"βœ… Successfully loaded {model_name}!"
        
    except Exception as e:
        return f"❌ Error loading {model_name}: {str(e)}"

def visualize_bbox(image, boxes, labels, scores, classes_map, colors):
    """
    Visualize bounding boxes on image
    """
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    elif not isinstance(image, Image.Image):
        raise ValueError("Input image must be PIL Image or numpy array")
    
    # Create a copy to draw on
    draw_image = image.copy()
    draw = ImageDraw.Draw(draw_image)
    
    # Try to use a font, fallback to default if not available
    try:
        font = ImageFont.truetype("arial.ttf", 20)
    except:
        try:
            font = ImageFont.load_default()
        except:
            font = None
    
    for box, label_id, score in zip(boxes, labels, scores):
        # Convert tensor to int if needed
        if torch.is_tensor(label_id):
            label_id = label_id.item()
        if torch.is_tensor(score):
            score = score.item()
            
        label = classes_map.get(int(label_id), f"Class_{label_id}")
        color = colors[int(label_id) % len(colors)]
        
        # Convert box coordinates to integers
        x1, y1, x2, y2 = [int(coord) for coord in box]
        
        # Draw rectangle
        draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
        
        # Draw label background
        text = f"{label}: {score:.2f}"
        if font:
            bbox = draw.textbbox((x1, y1), text, font=font)
            text_width = bbox[2] - bbox[0]
            text_height = bbox[3] - bbox[1]
        else:
            # Estimate text size if no font available
            text_width = len(text) * 10
            text_height = 20
            
        draw.rectangle([x1, y1-text_height-4, x1+text_width+4, y1], fill=color)
        draw.text((x1+2, y1-text_height-2), text, fill="white", font=font)
    
    return np.array(draw_image)

def recognize_image(input_img, conf_threshold, iou_threshold, nms_method):
    """
    Process image with docling layout model
    """
    if input_img is None:
        return None, "Please upload an image first."
        
    if current_model is None or current_processor is None:
        return None, "Please load a model first."
        
    try:
        # Ensure image is PIL Image
        if isinstance(input_img, np.ndarray):
            input_img = Image.fromarray(input_img)
        
        # Convert to RGB if needed
        if input_img.mode != 'RGB':
            input_img = input_img.convert('RGB')
        
        # Process image
        inputs = current_processor(images=[input_img], return_tensors="pt")
        inputs = {k: v.to(device) for k, v in inputs.items()}
        
        # Run inference
        with torch.no_grad():
            outputs = current_model(**inputs)
            
        # Post-process results
        results = current_processor.post_process_object_detection(
            outputs,
            target_sizes=torch.tensor([input_img.size[::-1]]),
            threshold=conf_threshold,
        )
        
        if not results or len(results) == 0:
            return np.array(input_img), "No detections found."
            
        result = results[0]
        
        # Get results
        boxes = result["boxes"]
        scores = result["scores"] 
        labels = result["labels"]
        
        if len(boxes) == 0:
            return np.array(input_img), "No detections above confidence threshold."
        
        # Apply NMS if requested
        if iou_threshold < 1.0:
            if nms_method == "Custom IoMin":
                # Use custom NMS with IoMin
                keep_indices = nms(
                    boxes=boxes,
                    scores=scores, 
                    iou_threshold=iou_threshold
                )
            else:
                # Use standard torchvision NMS
                keep_indices = torch.ops.torchvision.nms(
                    boxes=boxes,
                    scores=scores,
                    iou_threshold=iou_threshold
                )
            
            boxes = boxes[keep_indices]
            scores = scores[keep_indices]
            labels = labels[keep_indices]
        
        # Handle single detection case
        if len(boxes.shape) == 1:
            boxes = boxes.unsqueeze(0)
            scores = scores.unsqueeze(0)
            labels = labels.unsqueeze(0)
            
        # Visualize results
        output = visualize_bbox(
            input_img, 
            boxes, 
            labels, 
            scores, 
            classes_map,
            colors
        )
        
        detection_info = f"Found {len(boxes)} detections after NMS ({nms_method})"
        return output, detection_info
            
    except Exception as e:
        print(f"[ERROR] recognize_image failed: {e}")
        error_msg = f"Error during processing: {str(e)}"
        # Return original image on error
        if input_img is not None:
            return np.array(input_img), error_msg
        return np.zeros((512, 512, 3), dtype=np.uint8), error_msg

def gradio_reset():
    return gr.update(value=None), gr.update(value=None), gr.update(value="")

if __name__ == "__main__":
    print(f"Using device: {device}")
    
    # Create header HTML
    header_html = """
    <div style="text-align: center; margin-bottom: 20px;">
        <h1>πŸ” Document Layout Analysis</h1>
        <p>Using Docling Layout Models for document structure detection</p>
        <p>Select a model, upload an image and adjust the parameters to detect document elements</p>
    </div>
    """
    
    with gr.Blocks(title="Document Layout Analysis", theme=gr.themes.Soft()) as demo:
        gr.HTML(header_html)
        
        with gr.Row():
            with gr.Column():
                # Model selection
                model_dropdown = gr.Dropdown(
                    choices=list(MODELS.keys()),
                    value="Egret XLarge",
                    label="πŸ€– Select Model",
                    info="Choose which Docling model to use"
                )
                
                load_btn = gr.Button("πŸ“₯ Load Model", variant="secondary")
                model_status = gr.Textbox(
                    label="Model Status", 
                    interactive=False,
                    value="No model loaded"
                )
                
                input_img = gr.Image(
                    label="πŸ“„ Upload Document Image", 
                    interactive=True,
                    type="pil"
                )
                
                with gr.Row():
                    clear = gr.Button("πŸ—‘οΈ Clear")
                    predict = gr.Button("πŸ” Detect Layout", interactive=True, variant="primary")
                    
                with gr.Row():
                    conf_threshold = gr.Slider(
                        label="Confidence Threshold",
                        minimum=0.0,
                        maximum=1.0,
                        step=0.05,
                        value=0.6,
                        info="Minimum confidence score for detections"
                    )
                    
                with gr.Row():
                    iou_threshold = gr.Slider(
                        label="NMS IoU Threshold", 
                        minimum=0.0,
                        maximum=1.0,
                        step=0.05,
                        value=0.5,
                        info="IoU threshold for Non-Maximum Suppression"
                    )
                
                nms_method = gr.Radio(
                    choices=["Custom IoMin", "Standard IoU"],
                    value="Custom IoMin",
                    label="NMS Method",
                    info="Choose NMS algorithm"
                )
                
                # Legend
                with gr.Accordion("πŸ“‹ Detected Classes", open=False):
                    legend_html = "<div style='display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;'>"
                    for class_id, class_name in classes_map.items():
                        color = colors[class_id % len(colors)]
                        legend_html += f"""
                        <div style='display: flex; align-items: center; padding: 5px;'>
                            <div style='width: 20px; height: 20px; background-color: {color}; margin-right: 10px; border: 1px solid #ccc;'></div>
                            <span>{class_name}</span>
                        </div>
                        """
                    legend_html += "</div>"
                    gr.HTML(legend_html)
                    
            with gr.Column():
                gr.HTML("<h3>🎯 Detection Results</h3>")
                output_img = gr.Image(
                    label="Detected Layout", 
                    interactive=False,
                    type="numpy"
                )
                
                detection_info = gr.Textbox(
                    label="Detection Info",
                    interactive=False,
                    value=""
                )
        
        # Event handlers
        load_btn.click(
            load_model,
            inputs=[model_dropdown],
            outputs=[model_status]
        )
        
        clear.click(
            gradio_reset, 
            inputs=None, 
            outputs=[input_img, output_img, detection_info]
        )
        
        predict.click(
            recognize_image, 
            inputs=[input_img, conf_threshold, iou_threshold, nms_method], 
            outputs=[output_img, detection_info]
        )
    
    # Launch the demo
    demo.launch(
        server_name="0.0.0.0", 
        server_port=7860, 
        debug=True,
        share=False
    )