File size: 31,638 Bytes
e581bf6
d3b24af
 
 
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
e581bf6
 
 
 
 
d3b24af
 
e581bf6
 
 
d3b24af
 
 
 
 
e581bf6
d3b24af
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
 
e581bf6
 
 
 
 
d3b24af
e581bf6
 
 
 
d3b24af
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
 
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
 
 
e581bf6
d3b24af
e581bf6
 
d3b24af
e581bf6
d3b24af
e581bf6
d3b24af
 
 
 
 
 
 
 
 
e581bf6
d3b24af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e581bf6
d3b24af
 
 
e581bf6
 
d3b24af
 
 
 
 
e581bf6
 
 
 
 
 
d3b24af
 
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
e581bf6
d3b24af
 
e581bf6
d3b24af
e581bf6
d3b24af
e581bf6
 
d3b24af
 
 
 
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
e581bf6
d3b24af
e581bf6
 
 
 
d3b24af
e581bf6
d3b24af
 
 
 
 
 
 
 
 
 
 
e581bf6
 
 
 
d3b24af
e581bf6
 
 
 
d3b24af
e581bf6
d3b24af
 
 
 
 
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
 
 
 
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
 
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
e581bf6
d3b24af
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
e581bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3b24af
 
e581bf6
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
import argparse
import base64
import logging
import os
import sys
import traceback
import threading
from collections import Counter
from io import BytesIO
from typing import Dict, List, Optional, Tuple

import gradio as gr
import pandas as pd
import requests
import torch
import uvicorn
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.responses import JSONResponse
from PIL import Image, ImageDraw, ImageStat
from transformers import (
    DetrForObjectDetection,
    DetrForSegmentation,
    DetrImageProcessor,
    YolosForObjectDetection,
    YolosImageProcessor,
)
import nest_asyncio

# ------------------------------
# Configuration
# ------------------------------

# Logging configuration
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

# Model and processing constants
CONFIDENCE_THRESHOLD: float = 0.5
VALID_MODELS: List[str] = [
    "facebook/detr-resnet-50",
    "facebook/detr-resnet-101",
    "facebook/detr-resnet-50-panoptic",
    "facebook/detr-resnet-101-panoptic",
    "hustvl/yolos-tiny",
    "hustvl/yolos-base",
]
MODEL_DESCRIPTIONS: Dict[str, str] = {
    "facebook/detr-resnet-50": (
        "DETR with ResNet-50 backbone for object detection. Fast and accurate for general use."
    ),
    "facebook/detr-resnet-101": (
        "DETR with ResNet-101 backbone for object detection. More accurate but slower than ResNet-50."
    ),
    "facebook/detr-resnet-50-panoptic": (
        "DETR with ResNet-50 for panoptic segmentation. Detects objects and segments scenes."
    ),
    "facebook/detr-resnet-101-panoptic": (
        "DETR with ResNet-101 for panoptic segmentation. High accuracy for complex scenes."
    ),
    "hustvl/yolos-tiny": (
        "YOLOS Tiny model. Lightweight and fast, ideal for resource-constrained environments."
    ),
    "hustvl/yolos-base": (
        "YOLOS Base model. Balances speed and accuracy for object detection."
    ),
}

# Port configuration
DEFAULT_GRADIO_PORT: int = 7860
DEFAULT_FASTAPI_PORT: int = 8000
PORT_RANGE: range = range(7860, 7870)  # Try ports 7860-7869
MAX_PORT_ATTEMPTS: int = 10

# Thread-safe storage for lazy-loaded models and processors
models: Dict[str, any] = {}
processors: Dict[str, any] = {}
model_lock = threading.Lock()

# ------------------------------
# Model Loading
# ------------------------------

def load_model_and_processor(model_name: str) -> Tuple[any, any]:
    """
    Load and cache the specified model and processor thread-safely.

    Args:
        model_name: Name of the model to load (must be in VALID_MODELS).

    Returns:
        Tuple containing the loaded model and processor.

    Raises:
        ValueError: If the model_name is invalid or loading fails.
    """
    with model_lock:
        if model_name not in models:
            logger.info(f"Loading model: {model_name}")
            try:
                if "yolos" in model_name:
                    models[model_name] = YolosForObjectDetection.from_pretrained(model_name)
                    processors[model_name] = YolosImageProcessor.from_pretrained(model_name)
                elif "panoptic" in model_name:
                    models[model_name] = DetrForSegmentation.from_pretrained(model_name)
                    processors[model_name] = DetrImageProcessor.from_pretrained(model_name)
                else:
                    models[model_name] = DetrForObjectDetection.from_pretrained(model_name)
                    processors[model_name] = DetrImageProcessor.from_pretrained(model_name)
                logger.debug(f"Model {model_name} loaded successfully")
            except Exception as e:
                logger.error(f"Failed to load model {model_name}: {str(e)}")
                raise ValueError(f"Failed to load model: {str(e)}")
        return models[model_name], processors[model_name]

# ------------------------------
# Image Processing
# ------------------------------

def process(image: Image.Image, model_name: str) -> Tuple[Image.Image, List[str], List[float], List[str], List[float], Dict[str, str]]:
    """
    Process an image for object detection or panoptic segmentation.

    Args:
        image: PIL Image to process.
        model_name: Name of the model to use (must be in VALID_MODELS).

    Returns:
        Tuple containing:
        - Annotated image (PIL Image).
        - List of detected object names.
        - List of confidence scores for detected objects.
        - List of unique object names.
        - List of confidence scores for unique objects.
        - Dictionary of image properties (format, size, etc.).

    Raises:
        ValueError: If the model_name is invalid.
        RuntimeError: If processing fails due to model or image issues.
    """
    if model_name not in VALID_MODELS:
        raise ValueError(f"Invalid model: {model_name}. Choose from: {VALID_MODELS}")

    try:
        # Load model and processor
        model, processor = load_model_and_processor(model_name)
        logger.debug(f"Processing image with model: {model_name}")

        # Prepare image for processing
        inputs = processor(images=image, return_tensors="pt")
        with torch.no_grad():
            outputs = model(**inputs)

        # Initialize drawing context
        draw = ImageDraw.Draw(image)
        object_names: List[str] = []
        confidence_scores: List[float] = []
        object_counter = Counter()
        target_sizes = torch.tensor([image.size[::-1]])

        # Process panoptic segmentation or object detection
        if "panoptic" in model_name:
            processed_sizes = torch.tensor([[inputs["pixel_values"].shape[2], inputs["pixel_values"].shape[3]]])
            results = processor.post_process_panoptic(outputs, target_sizes=target_sizes, processed_sizes=processed_sizes)[0]

            for segment in results["segments_info"]:
                label = segment["label_id"]
                label_name = model.config.id2label.get(label, "Unknown")
                score = segment.get("score", 1.0)

                # Apply segmentation mask if available
                if "masks" in results and segment["id"] < len(results["masks"]):
                    mask = results["masks"][segment["id"]].cpu().numpy()
                    if mask.shape[0] > 0 and mask.shape[1] > 0:
                        mask_image = Image.fromarray((mask * 255).astype("uint8"))
                        colored_mask = Image.new("RGBA", image.size, (0, 0, 0, 0))
                        mask_draw = ImageDraw.Draw(colored_mask)
                        r, g, b = (segment["id"] * 50) % 255, (segment["id"] * 100) % 255, (segment["id"] * 150) % 255
                        mask_draw.bitmap((0, 0), mask_image, fill=(r, g, b, 128))
                        image = Image.alpha_composite(image.convert("RGBA"), colored_mask).convert("RGB")
                        draw = ImageDraw.Draw(image)

                if score > CONFIDENCE_THRESHOLD:
                    object_names.append(label_name)
                    confidence_scores.append(float(score))
                    object_counter[label_name] = float(score)
        else:
            results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)[0]

            for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
                if score > CONFIDENCE_THRESHOLD:
                    x, y, x2, y2 = box.tolist()
                    draw.rectangle([x, y, x2, y2], outline="#32CD32", width=2)
                    label_name = model.config.id2label.get(label.item(), "Unknown")
                    text = f"{label_name}: {score:.2f}"
                    text_bbox = draw.textbbox((0, 0), text)
                    text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
                    draw.text((x2 - text_width - 2, y - text_height - 2), text, fill="#32CD32")
                    object_names.append(label_name)
                    confidence_scores.append(float(score))
                    object_counter[label_name] = float(score)

        # Compile unique objects and confidences
        unique_objects = list(object_counter.keys())
        unique_confidences = [object_counter[obj] for obj in unique_objects]

        # Calculate image properties
        properties: Dict[str, str] = {
            "Format": image.format if hasattr(image, "format") and image.format else "Unknown",
            "Size": f"{image.width}x{image.height}",
            "Width": f"{image.width} px",
            "Height": f"{image.height} px",
            "Mode": image.mode,
            "Aspect Ratio": (
                f"{round(image.width / image.height, 2)}" if image.height != 0 else "Undefined"
            ),
            "File Size": "Unknown",
            "Mean (R,G,B)": "Unknown",
            "StdDev (R,G,B)": "Unknown",
        }

        # Compute file size
        try:
            buffered = BytesIO()
            image.save(buffered, format="PNG")
            properties["File Size"] = f"{len(buffered.getvalue()) / 1024:.2f} KB"
        except Exception as e:
            logger.error(f"Error calculating file size: {str(e)}")

        # Compute color statistics
        try:
            stat = ImageStat.Stat(image)
            properties["Mean (R,G,B)"] = ", ".join(f"{m:.2f}" for m in stat.mean)
            properties["StdDev (R,G,B)"] = ", ".join(f"{s:.2f}" for s in stat.stddev)
        except Exception as e:
            logger.error(f"Error calculating color statistics: {str(e)}")

        return image, object_names, confidence_scores, unique_objects, unique_confidences, properties

    except Exception as e:
        logger.error(f"Error in process: {str(e)}\n{traceback.format_exc()}")
        raise RuntimeError(f"Failed to process image: {str(e)}")

# ------------------------------
# FastAPI Setup
# ------------------------------

app = FastAPI(title="Object Detection API")

@app.post("/detect")
async def detect_objects_endpoint(
    file: Optional[UploadFile] = File(None),
    image_url: Optional[str] = Form(None),
    model_name: str = Form(VALID_MODELS[0]),
) -> JSONResponse:
    """
    FastAPI endpoint to detect objects in an image from file upload or URL.

    Args:
        file: Uploaded image file (optional).
        image_url: URL of the image (optional).
        model_name: Model to use for detection (default: first VALID_MODELS entry).

    Returns:
        JSONResponse containing the processed image (base64), detected objects, and confidences.

    Raises:
        HTTPException: If input validation fails or processing errors occur.
    """
    try:
        # Validate input
        if (file is None and not image_url) or (file is not None and image_url):
            raise HTTPException(
                status_code=400,
                detail="Provide either an image file or an image URL, not both.",
            )

        # Load image
        if file:
            if not file.content_type.startswith("image/"):
                raise HTTPException(status_code=400, detail="File must be an image")
            contents = await file.read()
            image = Image.open(BytesIO(contents)).convert("RGB")
        else:
            response = requests.get(image_url, timeout=10)
            response.raise_for_status()
            image = Image.open(BytesIO(response.content)).convert("RGB")

        if model_name not in VALID_MODELS:
            raise HTTPException(
                status_code=400,
                detail=f"Invalid model. Choose from: {VALID_MODELS}",
            )

        # Process image
        detected_image, detected_objects, detected_confidences, unique_objects, unique_confidences, _ = process(
            image, model_name
        )

        # Encode image as base64
        buffered = BytesIO()
        detected_image.save(buffered, format="PNG")
        img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
        img_url = f"data:image/png;base64,{img_base64}"

        return JSONResponse(
            content={
                "image_url": img_url,
                "detected_objects": detected_objects,
                "confidence_scores": detected_confidences,
                "unique_objects": unique_objects,
                "unique_confidence_scores": unique_confidences,
            }
        )

    except requests.RequestException as e:
        logger.error(f"Error fetching image from URL: {str(e)}")
        raise HTTPException(status_code=400, detail=f"Failed to fetch image: {str(e)}")
    except Exception as e:
        logger.error(f"Error in FastAPI endpoint: {str(e)}\n{traceback.format_exc()}")
        raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")

# ------------------------------
# Gradio UI Setup
# ------------------------------

def create_gradio_ui() -> gr.Blocks:
    """
    Create and configure the Gradio UI for object detection.

    Returns:
        Gradio Blocks object representing the UI.

    Raises:
        RuntimeError: If UI creation fails.
    """
    try:
        with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="gray")) as app:
            gr.Markdown(
                f"""
                # πŸš€ Object Detection App
                Upload an image or provide a URL to detect objects using state-of-the-art transformer models (DETR, YOLOS).
                Running on port: {os.getenv('GRADIO_SERVER_PORT', 'auto-selected')}
                """
            )

            with gr.Tabs():
                with gr.Tab("πŸ“· Image Upload"):
                    with gr.Row():
                        with gr.Column(scale=1):
                            gr.Markdown("### Input")
                            model_choice = gr.Dropdown(
                                choices=VALID_MODELS,
                                value=VALID_MODELS[0],
                                label="πŸ”Ž Select Model",
                                info="Choose a model for object detection or panoptic segmentation.",
                            )
                            model_info = gr.Markdown(
                                f"**Model Info**: {MODEL_DESCRIPTIONS[VALID_MODELS[0]]}",
                                visible=True,
                            )
                            image_input = gr.Image(type="pil", label="πŸ“· Upload Image")
                            image_url_input = gr.Textbox(
                                label="πŸ”— Image URL",
                                placeholder="https://example.com/image.jpg",
                            )
                            with gr.Row():
                                submit_btn = gr.Button("✨ Detect", variant="primary")
                                clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")

                            model_choice.change(
                                fn=lambda model_name: (
                                    f"**Model Info**: {MODEL_DESCRIPTIONS.get(model_name, 'No description available.')}"
                                ),
                                inputs=model_choice,
                                outputs=model_info,
                            )

                        with gr.Column(scale=2):
                            gr.Markdown("### Results")
                            error_output = gr.Textbox(
                                label="⚠️ Errors",
                                visible=False,
                                lines=3,
                                max_lines=5,
                            )
                            output_image = gr.Image(
                                type="pil",
                                label="🎯 Detected Image",
                                interactive=False,
                            )
                            with gr.Row():
                                objects_output = gr.DataFrame(
                                    label="πŸ“‹ Detected Objects",
                                    interactive=False,
                                    value=None,
                                )
                                unique_objects_output = gr.DataFrame(
                                    label="πŸ” Unique Objects",
                                    interactive=False,
                                    value=None,
                                )
                            properties_output = gr.DataFrame(
                                label="πŸ“„ Image Properties",
                                interactive=False,
                                value=None,
                            )

                    def process_for_gradio(image: Optional[Image.Image], url: Optional[str], model_name: str) -> Tuple[
                        Optional[Image.Image], Optional[pd.DataFrame], Optional[pd.DataFrame], Optional[pd.DataFrame], str
                    ]:
                        """
                        Process image for Gradio UI and return results.

                        Args:
                            image: Uploaded PIL Image (optional).
                            url: Image URL (optional).
                            model_name: Model to use for detection.

                        Returns:
                            Tuple of detected image, objects DataFrame, unique objects DataFrame, properties DataFrame, and error message.
                        """
                        try:
                            if image is None and not url:
                                return None, None, None, None, "Please provide an image or URL"
                            if image and url:
                                return None, None, None, None, "Please provide either an image or URL, not both"

                            if url:
                                response = requests.get(url, timeout=10)
                                response.raise_for_status()
                                image = Image.open(BytesIO(response.content)).convert("RGB")

                            detected_image, objects, scores, unique_objects, unique_scores, properties = process(
                                image, model_name
                            )
                            objects_df = (
                                pd.DataFrame(
                                    {
                                        "Object": objects,
                                        "Confidence Score": [f"{score:.2f}" for score in scores],
                                    }
                                )
                                if objects
                                else pd.DataFrame(columns=["Object", "Confidence Score"])
                            )
                            unique_objects_df = (
                                pd.DataFrame(
                                    {
                                        "Unique Object": unique_objects,
                                        "Confidence Score": [f"{score:.2f}" for score in unique_scores],
                                    }
                                )
                                if unique_objects
                                else pd.DataFrame(columns=["Unique Object", "Confidence Score"])
                            )
                            properties_df = (
                                pd.DataFrame([properties])
                                if properties
                                else pd.DataFrame(columns=properties.keys())
                            )
                            return detected_image, objects_df, unique_objects_df, properties_df, ""

                        except requests.RequestException as e:
                            error_msg = f"Error fetching image from URL: {str(e)}"
                            logger.error(f"{error_msg}\n{traceback.format_exc()}")
                            return None, None, None, None, error_msg
                        except Exception as e:
                            error_msg = f"Error processing image: {str(e)}"
                            logger.error(f"{error_msg}\n{traceback.format_exc()}")
                            return None, None, None, None, error_msg

                    submit_btn.click(
                        fn=process_for_gradio,
                        inputs=[image_input, image_url_input, model_choice],
                        outputs=[output_image, objects_output, unique_objects_output, properties_output, error_output],
                    )

                    clear_btn.click(
                        fn=lambda: [None, "", None, None, None, None],
                        inputs=None,
                        outputs=[
                            image_input,
                            image_url_input,
                            output_image,
                            objects_output,
                            unique_objects_output,
                            properties_output,
                            error_output,
                        ],
                    )

                with gr.Tab("πŸ”— JSON Output"):
                    gr.Markdown("### Process Image for JSON Output")
                    image_input_json = gr.Image(type="pil", label="πŸ“· Upload Image")
                    image_url_input_json = gr.Textbox(
                        label="πŸ”— Image URL",
                        placeholder="https://example.com/image.jpg",
                    )
                    url_model_choice = gr.Dropdown(
                        choices=VALID_MODELS,
                        value=VALID_MODELS[0],
                        label="πŸ”Ž Select Model",
                    )
                    url_model_info = gr.Markdown(
                        f"**Model Info**: {MODEL_DESCRIPTIONS[VALID_MODELS[0]]}",
                        visible=True,
                    )
                    url_submit_btn = gr.Button("πŸ”„ Process", variant="primary")
                    url_output = gr.JSON(label="API Response")

                    url_model_choice.change(
                        fn=lambda model_name: (
                            f"**Model Info**: {MODEL_DESCRIPTIONS.get(model_name, 'No description available.')}"
                        ),
                        inputs=url_model_choice,
                        outputs=url_model_info,
                    )

                    def process_url_for_gradio(image: Optional[Image.Image], url: Optional[str], model_name: str) -> Dict:
                        """
                        Process image from file or URL for Gradio UI and return JSON response.

                        Args:
                            image: Uploaded PIL Image (optional).
                            url: Image URL (optional).
                            model_name: Model to use for detection.

                        Returns:
                            Dictionary with processed image (base64), detected objects, and confidences.
                        """
                        try:
                            if image is None and not url:
                                return {"error": "Please provide an image or URL"}
                            if image and url:
                                return {"error": "Please provide either an image or URL, not both"}

                            if url:
                                response = requests.get(url, timeout=10)
                                response.raise_for_status()
                                image = Image.open(BytesIO(response.content)).convert("RGB")

                            detected_image, objects, scores, unique_objects, unique_scores, _ = process(
                                image, model_name
                            )
                            buffered = BytesIO()
                            detected_image.save(buffered, format="PNG")
                            img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
                            return {
                                "image_url": f"data:image/png;base64,{img_base64}",
                                "detected_objects": objects,
                                "confidence_scores": scores,
                                "unique_objects": unique_objects,
                                "unique_confidence_scores": unique_scores,
                            }
                        except requests.RequestException as e:
                            error_msg = f"Error fetching image from URL: {str(e)}"
                            logger.error(f"{error_msg}\n{traceback.format_exc()}")
                            return {"error": error_msg}
                        except Exception as e:
                            error_msg = f"Error processing image: {str(e)}"
                            logger.error(f"{error_msg}\n{traceback.format_exc()}")
                            return {"error": error_msg}

                    url_submit_btn.click(
                        fn=process_url_for_gradio,
                        inputs=[image_input_json, image_url_input_json, url_model_choice],
                        outputs=[url_output],
                    )

                with gr.Tab("ℹ️ Help"):
                    gr.Markdown(
                        """
                        ## How to Use
                        - **Image Upload**: Select a model, upload an image or provide a URL, and click "Detect" to see detected objects and image properties.
                        - **JSON Output**: Upload an image or enter a URL, select a model, and click "Process" to get results in JSON format.
                        - **Models**: Choose from DETR (object detection or panoptic segmentation) or YOLOS (lightweight detection).
                        - **Clear**: Reset all inputs and outputs using the "Clear" button in the Image Upload tab.
                        - **Errors**: Check the error box (Image Upload) or JSON response (JSON Output) for issues.
                        
                        ## Tips
                        - Use high-quality images for better detection results.
                        - Panoptic models (e.g., DETR-ResNet-50-panoptic) provide segmentation masks for complex scenes.
                        - For faster processing, try YOLOS-Tiny on resource-constrained devices.
                        """
                    )

        return app

    except Exception as e:
        logger.error(f"Error creating Gradio UI: {str(e)}\n{traceback.format_exc()}")
        raise RuntimeError(f"Failed to create Gradio UI: {str(e)}")

# ------------------------------
# Launcher
# ------------------------------

def parse_args() -> argparse.Namespace:
    """
    Parse command-line arguments with defaults and ignore unrecognized arguments.

    Returns:
        Parsed arguments as a Namespace object.

    Raises:
        SystemExit: If argument parsing fails (handled by argparse).
    """
    parser = argparse.ArgumentParser(
        description="Launcher for Object Detection App with Gradio UI and optional FastAPI server."
    )
    parser.add_argument(
        "--gradio-port",
        type=int,
        default=DEFAULT_GRADIO_PORT,
        help=f"Port for the Gradio UI (default: {DEFAULT_GRADIO_PORT}).",
    )
    parser.add_argument(
        "--enable-fastapi",
        action="store_true",
        default=False,
        help="Enable the FastAPI server (disabled by default).",
    )
    parser.add_argument(
        "--fastapi-port",
        type=int,
        default=DEFAULT_FASTAPI_PORT,
        help=f"Port for the FastAPI server if enabled (default: {DEFAULT_FASTAPI_PORT}).",
    )

    # Parse known arguments and ignore unrecognized ones (e.g., Jupyter kernel args)
    args, _ = parser.parse_known_args()
    return args

def find_available_port(start_port: int, port_range: range, max_attempts: int) -> Optional[int]:
    """
    Find an available port within the specified range.

    Args:
        start_port: Initial port to try (e.g., from args or environment).
        port_range: Range of ports to attempt.
        max_attempts: Maximum number of ports to try.

    Returns:
        Available port number, or None if no port is found.

    Raises:
        OSError: If port binding fails for reasons other than port in use.
    """
    import socket

    port = start_port
    attempts = 0

    # Check environment variable GRADIO_SERVER_PORT
    env_port = os.getenv("GRADIO_SERVER_PORT")
    if env_port and env_port.isdigit():
        port = int(env_port)
        logger.info(f"Using GRADIO_SERVER_PORT from environment: {port}")

    while attempts < max_attempts:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            try:
                s.bind(("0.0.0.0", port))
                logger.debug(f"Port {port} is available")
                return port
            except OSError as e:
                if e.errno == 98:  # Port in use
                    logger.debug(f"Port {port} is in use")
                    port = port + 1 if port < max(port_range) else min(port_range)
                    attempts += 1
                else:
                    raise
            except Exception as e:
                logger.error(f"Error checking port {port}: {str(e)}")
                raise
    logger.error(f"No available port found in range {min(port_range)}-{max(port_range)} after {max_attempts} attempts")
    return None

def run_fastapi_server(host: str, port: int) -> None:
    """
    Run the FastAPI server using Uvicorn.

    Args:
        host: Host address for the FastAPI server.
        port: Port for the FastAPI server.
    """
    try:
        uvicorn.run(app, host=host, port=port)
    except Exception as e:
        logger.error(f"Error running FastAPI server: {str(e)}\n{traceback.format_exc()}")
        sys.exit(1)

def main() -> None:
    """
    Main function to launch Gradio UI and optional FastAPI server.

    Raises:
        SystemExit: If the application is interrupted or encounters an error.
    """
    try:
        # Apply nest_asyncio to allow nested event loops in Jupyter/Colab
        nest_asyncio.apply()

        # Parse command-line arguments
        args = parse_args()
        logger.info(f"Parsed arguments: {args}")

        # Find available port for Gradio
        gradio_port = find_available_port(args.gradio_port, PORT_RANGE, MAX_PORT_ATTEMPTS)
        if gradio_port is None:
            logger.error("Failed to find an available port for Gradio UI")
            sys.exit(1)

        # Launch FastAPI server in a separate thread if enabled
        if args.enable_fastapi:
            logger.info(f"Starting FastAPI server on port {args.fastapi_port}")
            fastapi_thread = threading.Thread(
                target=run_fastapi_server,
                args=("0.0.0.0", args.fastapi_port),
                daemon=True
            )
            fastapi_thread.start()

        # Launch Gradio UI
        logger.info(f"Starting Gradio UI on port {gradio_port}")
        app = create_gradio_ui()
        app.launch(server_port=gradio_port, server_name="0.0.0.0")

    except KeyboardInterrupt:
        logger.info("Application terminated by user.")
        sys.exit(0)
    except OSError as e:
        logger.error(f"Port binding error: {str(e)}")
        sys.exit(1)
    except Exception as e:
        logger.error(f"Error running application: {str(e)}\n{traceback.format_exc()}")
        sys.exit(1)

if __name__ == "__main__":
    main()