Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load the model from Hugging Face Hub
|
| 6 |
+
detector = pipeline(
|
| 7 |
+
task="object-detection",
|
| 8 |
+
model="jayanthapoojary1989/rsna-pneumonia-yolov8s", # Change model name if you want to use another
|
| 9 |
+
threshold=0.5 # Set your detection confidence threshold
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def detect_objects(image):
|
| 13 |
+
# Run object detection
|
| 14 |
+
results = detector(image)
|
| 15 |
+
# Draw bounding boxes
|
| 16 |
+
image = image.copy()
|
| 17 |
+
for result in results:
|
| 18 |
+
box = result['box']
|
| 19 |
+
label = result['label']
|
| 20 |
+
score = result['score']
|
| 21 |
+
draw = ImageDraw.Draw(image)
|
| 22 |
+
draw.rectangle([box['xmin'], box['ymin'], box['xmax'], box['ymax']], outline="red", width=3)
|
| 23 |
+
draw.text((box['xmin'], box['ymin'] - 10), f"{label} ({score:.2f})", fill="red")
|
| 24 |
+
return image
|
| 25 |
+
|
| 26 |
+
# Create Gradio Interface
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=detect_objects,
|
| 29 |
+
inputs=gr.Image(type="pil"),
|
| 30 |
+
outputs=gr.Image(type="pil"),
|
| 31 |
+
title="Pneumonia Detection on Chest X-rays",
|
| 32 |
+
description="Upload a chest X-ray image. The model will predict bounding boxes for pneumonia regions."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the app
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
interface.launch()
|