File size: 2,594 Bytes
58ead1c
8eaf800
8b79120
8eaf800
58ead1c
8eaf800
 
 
 
 
 
8b79120
8eaf800
 
 
5d20b8f
8eaf800
 
5d20b8f
8eaf800
 
5d20b8f
8eaf800
 
 
5d20b8f
8eaf800
5d20b8f
8eaf800
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d20b8f
8eaf800
58ead1c
8eaf800
 
 
 
58ead1c
 
8eaf800
 
 
8b79120
8eaf800
 
 
5d20b8f
8eaf800
 
 
 
 
 
 
 
 
8b79120
58ead1c
74fcd5b
8b79120
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
import gradio as gr
from transformers import pipeline
from PIL import Image, ImageDraw
import torch

# Initialize the detection pipeline using the DETR architecture
# This model runs locally within the Space environment
try:
    detector = pipeline("object-detection", model="facebook/detr-resnet-50")
except Exception as e:
    detector = None

def analyze_system(image):
    if image is None:
        return None, {"status": "error", "message": "No input signal detected."}
    
    if detector is None:
        return image, {"status": "error", "message": "Model initialization failed."}
    
    # Perform high-precision inference
    predictions = detector(image)
    
    # Prepare drawing context for visual telemetry
    annotated_image = image.copy()
    draw = ImageDraw.Draw(annotated_image)
    
    telemetry_report = []
    
    for pred in predictions:
        box = pred["box"]
        label = pred["label"]
        score = pred["score"]
        
        # Extract spatial coordinates
        xmin, ymin, xmax, ymax = box["xmin"], box["ymin"], box["xmax"], box["ymax"]
        
        # Draw identification borders using a high-contrast industrial green
        draw.rectangle([xmin, ymin, xmax, ymax], outline="#00FF00", width=4)
        
        # Compile telemetry data
        telemetry_report.append({
            "component_class": label,
            "confidence_rating": round(float(score), 4),
            "spatial_coordinates": {
                "xmin": xmin,
                "ymin": ymin,
                "xmax": xmax,
                "ymax": ymax
            }
        })
    
    return annotated_image, telemetry_report

# Construct the Gradio Interface with a technical, utility-focused theme
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="blue")) as demo:
    gr.Markdown("# 🛰️ Neural Industrial Inspector")
    gr.Markdown("**System Status**: Operational | **Core**: DETR-ResNet-50 Transformer")
    
    with gr.Row():
        with gr.Column(scale=1):
            input_img = gr.Image(type="pil", label="Optical System Feed")
            run_btn = gr.Button("INITIATE SYSTEM SCAN", variant="primary")
        
        with gr.Column(scale=1):
            output_img = gr.Image(type="pil", label="Visual Diagnostic Overlay")
            output_data = gr.JSON(label="Structured Telemetry Data")
            
    gr.Examples(
        examples=[],
        inputs=input_img
    )
    
    run_btn.click(
        fn=analyze_system, 
        inputs=input_img, 
        outputs=[output_img, output_data]
    )

if __name__ == "__main__":
    demo.launch()