| import numpy as np |
| import cv2 as cv |
| import joblib |
| import gradio as gr |
|
|
| |
| loaded_model = joblib.load('model.pkl') |
|
|
| |
| class_names = ['glioma_tumor', 'meningioma_tumor', 'normal', 'pituitary_tumor'] |
|
|
| def preprocess_image(image, target_size=(30, 10)): |
| """Preprocess the input image for model prediction.""" |
| img = np.array(image) |
| |
| img_resized = cv.resize(image, target_size) |
| |
| gray_img = cv.cvtColor(img_resized, cv.COLOR_BGR2GRAY) |
| |
| img_flattened = gray_img.flatten() |
| |
| if len(img_flattened) != 300: |
| raise ValueError(f"Expected 300 features, but got {len(img_flattened)} features.") |
| return gray_img, img_flattened |
|
|
| def predict_image(image): |
| """Predict the class of the input image using the trained model and return the processed image.""" |
| |
| processed_image, img_preprocessed = preprocess_image(image) |
| |
| prediction = loaded_model.predict([img_preprocessed]) |
| |
| predicted_class = class_names[prediction[0]] |
| |
| processed_image_rgb = cv.cvtColor(processed_image, cv.COLOR_GRAY2RGB) |
| return predicted_class, processed_image_rgb |
|
|
| |
| demo = gr.Interface( |
| fn=predict_image, |
| inputs=gr.Image(type="numpy"), |
| outputs=[gr.Text(), gr.Image()] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |