File size: 5,717 Bytes
e656561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Example inference script for Heartformer model

This script demonstrates how to use the Heartformer model to detect
heart anatomy types in images.
"""

import sys
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
import json

# You'll need to install rf-detr first:
# pip install git+https://github.com/roboflow/rf-detr.git

try:
    from rfdetr import RFDETRNano
except ImportError:
    print("❌ Error: RF-DETR not installed")
    print("Please install: pip install git+https://github.com/roboflow/rf-detr.git")
    sys.exit(1)

# Class names (matching the model training)
CLASS_NAMES = [
    "heart-anatomy-images",  # Parent category at index 0
    "heart_cadaver",
    "heart_cell",
    "heart_ct_scan",
    "heart_drawing",
    "heart_textbook",
    "heart_wall",
    "heart_xray"
]

# Class descriptions
CLASS_DESCRIPTIONS = {
    "heart_cadaver": "Real anatomical specimen from dissection",
    "heart_cell": "Microscopic/cellular view of cardiac tissue",
    "heart_ct_scan": "CT imaging of the heart",
    "heart_drawing": "Hand-drawn or digital medical illustration",
    "heart_textbook": "Educational anatomy image from textbooks",
    "heart_wall": "Cross-sectional view showing heart wall layers",
    "heart_xray": "Radiographic chest/heart image"
}

# Colors for bounding boxes
COLORS = [
    (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
    (255, 0, 255), (0, 255, 255), (128, 0, 128), (255, 128, 0)
]


def load_model(checkpoint_path):
    """Load the Heartformer model"""
    print("🤖 Loading Heartformer model...")
    model = RFDETRNano(
        pretrain_weights=checkpoint_path,
        num_classes=len(CLASS_NAMES)
    )
    print("✅ Model loaded successfully")
    return model


def run_inference(model, image_path, threshold=0.3):
    """Run inference on an image"""
    print(f"\n🔍 Running inference on: {image_path}")
    print(f"   Confidence threshold: {threshold}")

    # Run detection
    detections = model.predict(str(image_path), threshold=threshold)

    # Parse results
    results = []
    for bbox, conf, class_id in zip(
        detections.xyxy,
        detections.confidence,
        detections.class_id
    ):
        class_id = int(class_id)

        # Skip parent category
        if class_id == 0:
            continue

        class_name = CLASS_NAMES[class_id]
        results.append({
            "class_id": class_id,
            "class_name": class_name,
            "confidence": float(conf),
            "bbox": [float(x) for x in bbox],
            "description": CLASS_DESCRIPTIONS.get(class_name, "")
        })

    return results


def visualize_results(image_path, results, output_path=None):
    """Draw bounding boxes on image"""
    # Load image
    image = Image.open(image_path).convert('RGB')
    draw = ImageDraw.Draw(image)

    # Draw each detection
    for detection in results:
        x1, y1, x2, y2 = detection['bbox']
        color = COLORS[detection['class_id'] % len(COLORS)]

        # Draw bounding box
        draw.rectangle([x1, y1, x2, y2], outline=color, width=3)

        # Draw label
        label = f"{detection['class_name']}: {detection['confidence']:.2f}"
        text_bbox = draw.textbbox((x1, y1), label)
        draw.rectangle(text_bbox, fill=color)
        draw.text((x1, y1 - 20), label, fill=(255, 255, 255))

    # Save or show
    if output_path:
        image.save(output_path)
        print(f"💾 Saved visualization to: {output_path}")
    else:
        image.show()

    return image


def main():
    """Main entry point"""
    import argparse

    parser = argparse.ArgumentParser(description="Run Heartformer inference")
    parser.add_argument("image", help="Path to input image")
    parser.add_argument(
        "--checkpoint",
        default="checkpoint_best_ema.pth",
        help="Path to model checkpoint"
    )
    parser.add_argument(
        "--threshold",
        type=float,
        default=0.3,
        help="Confidence threshold (default: 0.3)"
    )
    parser.add_argument(
        "--output",
        help="Path to save visualization (default: show in window)"
    )
    parser.add_argument(
        "--json",
        help="Path to save detection results as JSON"
    )

    args = parser.parse_args()

    # Validate inputs
    if not Path(args.image).exists():
        print(f"❌ Error: Image not found: {args.image}")
        return 1

    if not Path(args.checkpoint).exists():
        print(f"❌ Error: Checkpoint not found: {args.checkpoint}")
        print("\n💡 Download the checkpoint from:")
        print("   https://huggingface.co/giannisan/heartformer")
        return 1

    # Load model
    model = load_model(args.checkpoint)

    # Run inference
    results = run_inference(model, args.image, args.threshold)

    # Print results
    print(f"\n🎯 Found {len(results)} detection(s):")
    print("-" * 60)
    for i, det in enumerate(results, 1):
        print(f"\n{i}. {det['class_name']}")
        print(f"   Confidence: {det['confidence']:.1%}")
        print(f"   BBox: [{det['bbox'][0]:.0f}, {det['bbox'][1]:.0f}, "
              f"{det['bbox'][2]:.0f}, {det['bbox'][3]:.0f}]")
        print(f"   {det['description']}")

    # Save JSON if requested
    if args.json:
        with open(args.json, 'w') as f:
            json.dump(results, f, indent=2)
        print(f"\n💾 Saved results to: {args.json}")

    # Visualize
    if len(results) > 0:
        print("\n📊 Creating visualization...")
        visualize_results(args.image, results, args.output)
    else:
        print("\n⚠️  No detections found. Try lowering the threshold.")

    return 0


if __name__ == "__main__":
    exit(main())