cxr_classifier / app.py
foxojs
"update"
4235c9c
from fastai.vision.all import *
import gradio as gr
from PIL import Image
import pydicom
import numpy as np
path = "my_exported_model.pkl"
def process_dicom(fn):
# Read DICOM file
dcm = pydicom.dcmread(fn)
# Access pixel data and scale it to 8-bit (0-255) range
pixel_array = dcm.pixel_array
pixel_array = pixel_array - np.min(pixel_array)
pixel_array = pixel_array / np.max(pixel_array)
pixel_array = (pixel_array * 255).astype(np.uint8)
# Convert to PIL Image for further processing
img = Image.fromarray(pixel_array).convert('L')
# Resize the image (example: resize to 224x224)
img = img.resize((512, 512))
return PILImage(img)
def get_x(x):
return process_dicom(images_path/f"{x['image_id']}")
def get_y(x):
return x["class_name"]
# Function to process PNG images, mimicking your training preprocessing
def process_png(img):
# If the image is a NumPy array, convert it to a PIL Image
if isinstance(img, np.ndarray):
img = Image.fromarray(img)
# Convert to grayscale
img = img.convert('L')
# Resize the image to 512x512
img = img.resize((512, 512))
return PILImage(img)
# Prediction function
def predict_image(image):
# Process the input image
processed_img = process_png(image)
learn = load_learner(path)
# Get predictions
pred, idx, probs = learn.predict(processed_img)
categories = learn.dls.vocab
return dict(zip(categories, map(float, probs)))
description = "This model achieved 0.906 accuracy on a test set of 600 images."
iface = gr.Interface(fn=predict_image, inputs="image", outputs=gr.Label(),
description=description)
iface.launch()