Spaces:
Runtime error
Runtime error
| # ----------------------------- | |
| # Install pydicom if not present | |
| # ----------------------------- | |
| import subprocess | |
| import sys | |
| try: | |
| import pydicom | |
| except ImportError: | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "pydicom"]) | |
| import pydicom | |
| import streamlit as st | |
| import numpy as np | |
| import requests | |
| import cv2 | |
| import tempfile | |
| # ----------------------------- | |
| # Backend API URL | |
| # ----------------------------- | |
| API_URL = "https://viveksardey-pneumoniadetectionpredictionbackend.hf.space/v1/predict" | |
| IMG_SIZE = 224 | |
| st.title("🩺 Pneumonia Detection from Chest X-ray (DICOM)") | |
| st.write(""" | |
| Upload a **Chest X-ray DICOM (.dcm)** file. | |
| The image will be converted to **NumPy (.npy)** format and sent to the backend model. | |
| """) | |
| # ----------------------------- | |
| # Upload file | |
| # ----------------------------- | |
| uploaded_file = st.file_uploader( | |
| "Upload DICOM Chest X-ray", | |
| type=["dcm"] | |
| ) | |
| # ----------------------------- | |
| # Convert DICOM → Image | |
| # ----------------------------- | |
| def dicom_to_image(dicom_file): | |
| dicom = pydicom.dcmread(dicom_file) | |
| img = dicom.pixel_array | |
| return img | |
| # ----------------------------- | |
| # Preprocess image | |
| # ----------------------------- | |
| def preprocess_image(img): | |
| # Resize image | |
| img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) | |
| # Normalize | |
| img = img.astype("float32") | |
| if img.max() > 0: | |
| img = img / img.max() | |
| return img | |
| # ----------------------------- | |
| # If file uploaded | |
| # ----------------------------- | |
| if uploaded_file is not None: | |
| try: | |
| img = dicom_to_image(uploaded_file) | |
| st.subheader("X-ray Preview") | |
| st.image(img, caption="Uploaded DICOM Image", use_column_width=True) | |
| processed_img = preprocess_image(img) | |
| npy_array = np.array(processed_img) | |
| st.success("DICOM successfully converted to NumPy format") | |
| except Exception as e: | |
| st.error(f"Error processing DICOM file: {e}") | |
| # ----------------------------- | |
| # Predict button | |
| # ----------------------------- | |
| if st.button("Predict Pneumonia"): | |
| if uploaded_file is None: | |
| st.warning("Please upload a DICOM image first") | |
| else: | |
| try: | |
| with st.spinner("Running Pneumonia Detection..."): | |
| # Save temporary npy file | |
| with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as tmp: | |
| np.save(tmp.name, npy_array) | |
| tmp_path = tmp.name | |
| # Send request to backend API | |
| with open(tmp_path, "rb") as f: | |
| response = requests.post( | |
| API_URL, | |
| files={"file": f} | |
| ) | |
| if response.status_code == 200: | |
| result = response.json() | |
| predicted_class = result["Predicted_Class"] | |
| probability = result["Probability_Pneumonia"] | |
| st.subheader("Prediction Result") | |
| if predicted_class == "Pneumonia": | |
| st.error("⚠️ Pneumonia Detected") | |
| else: | |
| st.success("✅ Normal Lungs") | |
| st.write(f"Probability of Pneumonia: **{probability:.4f}**") | |
| else: | |
| st.error(f"Prediction failed. Status Code: {response.status_code}") | |
| except Exception as e: | |
| st.error(f"Error during prediction: {e}") | |