import tkinter as tk
from tkinter import filedialog, messagebox
import cv2
import numpy as np
import pydicom
from tensorflow.keras.models import load_model
import matplotlib.pyplot as plt
Load the pre-trained model
model = load_model(‘path_to_your_model.h5’)
def load_image(file_path):
if file_path.endswith(‘.dcm’):
ds = pydicom.dcmread(file_path)
image = ds.pixel_array
else:
image = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
return image
def preprocess_image(image):
image = cv2.resize(image, (256, 256))
image = image / 255.0 # Normalize to [0, 1]
return image
def predict_lesion(image):
image = np.expand_dims(image, axis=0) # Add batch dimension
prediction = model.predict(image)
return prediction
def visualize_results(image, prediction):
plt.imshow(image, cmap=‘gray’)
plt.imshow(prediction[0], alpha=0.5, cmap=‘jet’) # Overlay prediction
plt.axis(‘off’)
plt.show()
def open_file():
file_path = filedialog.askopenfilename()
if file_path:
try:
image = load_image(file_path)
processed_image = preprocess_image(image)
prediction = predict_lesion(processed_image)
visualize_results(image, prediction)
except Exception as e:
messagebox.showerror(“Error”, str(e))
Create the main application window
app = tk.Tk()
app.title(“MRI Lesion Analyzer”)
Create a button to open files
open_button = tk.Button(app, text=“Open MRI Image”, command=open_file)
open_button.pack(pady=20)
Start the GUI event loop
app.mainloop()