File size: 8,597 Bytes
a326b94 78d26e0 3bb1400 88fb5fa 04a7bfd 78d26e0 0000f4a 04a7bfd 78d26e0 6cc7ff9 04a7bfd 78d26e0 04a7bfd 005d8cf 78d26e0 005d8cf 88fb5fa 78d26e0 806ecee 78d26e0 3bb1400 04a7bfd 78d26e0 88fb5fa 04a7bfd 88fb5fa 04a7bfd 88fb5fa 78d26e0 88fb5fa 04a7bfd 6cc7ff9 88fb5fa 78d26e0 04a7bfd 0000f4a 78d26e0 04a7bfd 78d26e0 04a7bfd 78d26e0 04a7bfd 78d26e0 04a7bfd 78d26e0 04a7bfd 78d26e0 04a7bfd 78d26e0 04a7bfd 78d26e0 04a7bfd 78d26e0 04a7bfd 88fb5fa 78d26e0 88fb5fa 78d26e0 88fb5fa 78d26e0 6ea5ee2 3bb1400 |
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 |
import streamlit as st
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse
from transformers import pipeline
import torch
from PIL import Image, ImageDraw
import io
import base64
import numpy as np
import json
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# FastAPI app
app = FastAPI(
title="Fracture Detection API",
description="API for detecting fractures in X-ray images using multiple ML models",
version="1.0.0"
)
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"]
)
# Load models with caching
@st.cache_resource
def load_models():
logger.info("Loading ML models...")
try:
return {
"D3STRON": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
"Heem2": pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray"),
"Nandodeomkar": pipeline(
"image-classification",
model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388"
)
}
except Exception as e:
logger.error(f"Error loading models: {str(e)}")
raise
# Initialize models
try:
models = load_models()
logger.info("Models loaded successfully")
except Exception as e:
logger.error(f"Failed to load models: {str(e)}")
models = None
def draw_boxes(image, predictions, threshold=0.6):
"""
Draw bounding boxes and labels on the image for detected fractures.
Args:
image (PIL.Image): Input image
predictions (list): List of predictions from the model
threshold (float): Confidence threshold for filtering predictions
Returns:
tuple: (annotated image, filtered predictions)
"""
draw = ImageDraw.Draw(image)
filtered_preds = [p for p in predictions if p['score'] >= threshold]
for pred in filtered_preds:
box = pred['box']
label = f"{pred['label']} ({pred['score']:.2%})"
# Draw bounding box
draw.rectangle(
[(box['xmin'], box['ymin']), (box['xmax'], box['ymax'])],
outline="red",
width=2
)
# Draw label
draw.text(
(box['xmin'], box['ymin'] - 10),
label,
fill="red"
)
return image, filtered_preds
def process_image(image, confidence_threshold):
"""
Process an image through all models and return combined results.
Args:
image (PIL.Image): Input image
confidence_threshold (float): Confidence threshold for filtering predictions
Returns:
dict: Combined results from all models
"""
try:
# Object detection
detection_preds = models["D3STRON"](image)
result_image = image.copy()
result_image, filtered_detections = draw_boxes(
result_image,
detection_preds,
confidence_threshold
)
# Save annotated image
img_byte_arr = io.BytesIO()
result_image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
img_b64 = base64.b64encode(img_byte_arr).decode()
# Classification results
class_results = {}
# Heem2 model
try:
heem2_result = models["Heem2"](image)
class_results["Heem2"] = heem2_result
except Exception as e:
logger.error(f"Error in Heem2 model: {str(e)}")
class_results["Heem2"] = {"error": str(e)}
# Nandodeomkar model
try:
nando_result = models["Nandodeomkar"](image)
class_results["Nandodeomkar"] = nando_result
except Exception as e:
logger.error(f"Error in Nandodeomkar model: {str(e)}")
class_results["Nandodeomkar"] = {"error": str(e)}
return {
"success": True,
"detections": filtered_detections,
"classifications": class_results,
"image": img_b64
}
except Exception as e:
logger.error(f"Error processing image: {str(e)}")
raise
# API Endpoints
@app.post("/detect")
@app.post("/api/predict")
async def detect_fracture(
file: UploadFile = File(...),
confidence: float = Form(default=0.6)
):
"""
Endpoint for fracture detection in X-ray images.
Args:
file (UploadFile): Uploaded image file
confidence (float): Confidence threshold for predictions
Returns:
JSONResponse: Detection results including annotated image
"""
logger.info(f"Received request with confidence threshold: {confidence}")
try:
# Validate confidence threshold
if not 0 <= confidence <= 1:
return JSONResponse(
status_code=400,
content={
"success": False,
"error": "Confidence threshold must be between 0 and 1"
}
)
# Read and validate image
contents = await file.read()
try:
image = Image.open(io.BytesIO(contents))
except Exception as e:
return JSONResponse(
status_code=400,
content={
"success": False,
"error": f"Invalid image file: {str(e)}"
}
)
# Process image
try:
results = process_image(image, confidence)
logger.info("Image processed successfully")
return JSONResponse(content=results)
except Exception as e:
logger.error(f"Error processing image: {str(e)}")
return JSONResponse(
status_code=500,
content={
"success": False,
"error": f"Error processing image: {str(e)}"
}
)
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
return JSONResponse(
status_code=500,
content={
"success": False,
"error": f"Unexpected error: {str(e)}"
}
)
# Streamlit UI
def main():
st.title("🦴 Fracture Detection System")
st.write("Upload an X-ray image to detect potential fractures")
# File uploader
uploaded_file = st.file_uploader(
"Upload X-ray image",
type=['png', 'jpg', 'jpeg']
)
# Confidence threshold slider
confidence = st.slider(
"Confidence Threshold",
min_value=0.0,
max_value=1.0,
value=0.6,
step=0.05
)
if uploaded_file is not None:
# Display original image
image = Image.open(uploaded_file)
st.image(image, caption="Original Image", use_column_width=True)
if st.button("Analyze Image"):
try:
# Process image
results = process_image(image, confidence)
if results["success"]:
# Display results
st.success("Analysis completed successfully!")
# Show annotated image
annotated_image = Image.open(io.BytesIO(base64.b64decode(results["image"])))
st.image(annotated_image, caption="Detected Fractures", use_column_width=True)
# Show detections
if results["detections"]:
st.subheader("Detected Fractures")
for det in results["detections"]:
st.write(f"- {det['label']}: {det['score']:.2%} confidence")
# Show classifications
st.subheader("Classification Results")
for model, preds in results["classifications"].items():
st.write(f"**{model} Model:**")
st.json(preds)
else:
st.error("Analysis failed. Please try again.")
except Exception as e:
st.error(f"Error during analysis: {str(e)}")
if __name__ == "__main__":
main() |