Spaces:
Sleeping
Sleeping
Update app.py
Browse files12312345678891011111213131415141516171819202122232425262717282930313221333435362337383940import gradio as grfrom ultralytics import YOLOimport cv2import torchfrom ultralytics import YOLOfrom ultralytics.nn.tasks import DetectionModel # ✅ Correct import for PyTorch 2.6 fiximport gradio as gr
# Fix for PyTorch 2.6 safe loadingtorch.serialization.add_safe_globals([DetectionModel])
# Load your YOLOv8 model# Load YOLOv8 modelmodel = YOLO("best.pt")
# Prediction function# Detection functiondef detect_fracture(image): results = model.predict(source=image, save=False) # Run inference annotated_img = results[0].plot() # Draw detections return annotated_img # Run detection results = model.predict(source=image, save=False) annotated_img = results[0].plot() # Annotated image with bounding boxes
# Extract labels & confidence scores detections = [] for box in results[0].boxes: cls_id = int(box.cls[0]) label = model.names[cls_id] conf = float(box.conf[0]) detections.append(f"{label}: {conf:.2f}")
return annotated_img, "\n".join(detections) if detections else "No fracture detected"
# Gradio interface# Gradio appdemo = gr.Interface( fn=detect_fracture, inputs=gr.Image(type="numpy", label="Upload Bone X-ray"), outputs=[ outputs=gr.Image(type="numpy", label="Detection Result"), gr.Image(type="numpy", label="Detection Result"), gr.Textbox(label="Detected Fractures & Confidence Scores") ], title="Human Bone Fracture Detection", description="Upload an X-ray image to detect types of human bone fractures using YOLOv8." description="Upload an X-ray image to detect types of human bone fractures using a YOLOv8 model.")
if __name__ == "__main__":app.py+25-11