CoolFace
Apppublic

ncuiew7/Advanced_Bone-Fracture_Detection_and_Localized_Attention_Mapping

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
app.py60 linesDownload Raw Back to root
1import gradio as gr2import torch3import numpy as np4import cv25from PIL import Image6 7def predict_and_visualize(input_image):8    # Safety check for global variables9    if 'model' not in globals() or 'processor' not in globals():10        return "Error: Model or Processor not loaded. Please run the setup cells first.", None11 12    if input_image is None:13        return None, None14 15    # 1. Preprocess16    img_rgb = input_image.convert('RGB')17    inputs = processor(img_rgb, return_tensors="pt").to(model.device)18    inputs['pixel_values'].requires_grad = True19 20    # 2. Prediction21    model.eval()22    outputs = model(inputs['pixel_values'])23    logits = outputs.logits24    probs = torch.sigmoid(logits).detach().cpu().numpy()[0]25 26    fracture_prob = float(probs[1])27    results_dict = {28        "Abnormal (Fracture Detected)": fracture_prob,29        "Normal (No Fracture)": 1.0 - fracture_prob30    }31 32    # 3. Grad-CAM33    try:34        target_layer = model.swin.encoder.layers[-1].blocks[-1].layernorm_before35        heatmap = get_gradcam(model, inputs['pixel_values'], target_layer, task_index=1)36 37        img_np = np.array(img_rgb.resize((224, 224)))38        heatmap_color = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)39        overlay = cv2.addWeighted(img_np, 0.6, heatmap_color, 0.4, 0)40        output_img = Image.fromarray(overlay)41    except Exception as e:42        print(f"Grad-CAM Error: {e}")43        output_img = img_rgb # Fallback to original image if heatmap fails44 45    return results_dict, output_img46 47# Define the web interface layout48demo = gr.Interface(49    fn=predict_and_visualize,50    inputs=gr.Image(type="pil", label="Upload Patient Radiograph (X-Ray)"),51    outputs=[52        gr.Label(num_top_classes=2, label="Diagnostic Output Confidence"),53        gr.Image(label="Anatomical Attention Heatmap Overlay")54    ],55    title="Orthopaedic Decision Support System",56    description="Upload a shoulder or humerus radiograph to detect fractures."57)58 59# Launch60demo.launch(share=True, debug=True)