amirTamer's picture
Me3za
e219644
Raw History Blame
7.65 kB
import gradio as gr
import numpy as np
from skimage.feature import hog
import cv2
import pickle
def load_pickle_file(file_name):
try:
with open(file_name, "rb") as f:
loaded_file = pickle.load(f)
return loaded_file
except Exception:
return None
gnb_package = load_pickle_file("gnb_digit6_package.pkl")
pca_multi = load_pickle_file("pcaMulti.pkl")
rf_model = load_pickle_file("rf_hog_model.pkl")
pca_multi = pca_multi['pca']
if gnb_package is not None:
gnb_binary_model = gnb_package["model"]
pca = gnb_package["pca"]
best_weight = gnb_package["best_weight"]
hog_params = gnb_package["hog_params"]
else:
gnb_binary_model = None
pca = None
best_weight = None
hog_params = None
print("Loading Decision Tree...")
dt_package = load_pickle_file("dt.pkl")
if dt_package is not None:
dt_binary_model = dt_package["model"]
else:
dt_binary_model = None
print("Loading Logistic Regression...")
logreg_binary_model = load_pickle_file("custom_logreg_hog.pkl")
svm_package = load_pickle_file("svm2.pkl")
if svm_package is not None:
svm_binary_model = svm_package["model"]
else:
svm_binary_model = None
svm_scaler = load_pickle_file("svmScaler.pkl")
knn_model = load_pickle_file('knn_final_model.pkl')
gnb_multimode_model = load_pickle_file('gnbMulti.pkl')
gnb_multimode_model = gnb_multimode_model["model"]
dt_multimode_model = load_pickle_file('dt_Multi.pkl')
dt_multimode_model = dt_multimode_model['model']
svm_multimode_model = None
logreg_multimode_model = load_pickle_file('multiLog2_hog.pkl')
knn_multimode_model = None
def preprocess_image(image, model_choice, mode_choice):
image = np.array(image)
image_hog = hog(
image,
**hog_params
)
if mode_choice == "Binary":
if model_choice == "GNB" or model_choice == "KNN":
image_hog = image_hog.reshape(1, -1)
image_pca = pca.transform(image_hog)
return image_pca
else:
image_hog = image_hog.reshape(1, -1)
return image_hog
else:
if model_choice == "GNB":
image_hog = image_hog.reshape(1, -1)
image_pca = pca_multi.transform(image_hog)
return image_pca
elif model_choice == "DT" or model_choice == "Logistic Regression":
image_hog = image_hog.reshape(1, -1)
return image_hog
def classify_binary_gnb(image):
processed_image = preprocess_image(image, "GNB", "Binary")
prediction = gnb_binary_model.predict(
processed_image,
class_weights={0: 1.0, 1: best_weight}
)
if prediction[0] == 1:
return "GNB Binary: Is 6"
else:
return "GNB Binary: Not 6"
def classify_binary_dt(image):
processed_image = preprocess_image(image, "DT", "Binary")
prediction = dt_binary_model.predict(processed_image)
if prediction[0] == 1:
return "Decision Tree Binary: Is 6"
else:
return "Decision Tree Binary: Not 6"
def classify_binary_svm(image):
processed_image = preprocess_image(image, "SVM", "Binary")
prediction = svm_binary_model.predict(processed_image)
if prediction[0] == 1:
return "SVM Binary: Is 6"
else:
return "SVM Binary: Not 6"
def classify_binary_logreg(image):
processed_image = preprocess_image(image, "Logistic Regression", "Binary")
prediction = logreg_binary_model.predict(processed_image)
if prediction[0] == 1:
return "Logistic Regression Binary: Is 6"
else:
return "Logistic Regression Binary: Not 6"
def classify_knn(image):
if knn_model is None:
return "KNN Binary model placeholder: model is not inserted yet."
processed_image = preprocess_image(image, "KNN", "Binary")
prediction = knn_model.predict(processed_image)
if prediction[0] == 1:
return "KNN Binary: Is 6"
else:
return "KNN Binary: Not 6"
def classify_multimode_gnb(image):
processed_image = preprocess_image(image, "GNB", "Multimode")
prediction = gnb_multimode_model.predict(processed_image)
return "GNB Multimode prediction: " + str(prediction[0])
def classify_multimode_dt(image):
processed_image = preprocess_image(image, "DT", "Multimode")
prediction = dt_multimode_model.predict(processed_image)
return "Decision Tree Multimode prediction: " + str(prediction[0])
def classify_multimode_svm(image):
if svm_multimode_model is None:
return "SVM Multimode model placeholder: model is not inserted yet."
processed_image = preprocess_image(image, "SVM", "Multimode")
prediction = svm_multimode_model.predict(processed_image)
return "SVM Multimode prediction: " + str(prediction[0])
def classify_multimode_logreg(image):
if logreg_multimode_model is None:
return "Logistic Regression Multimode model placeholder: model is not inserted yet."
processed_image = preprocess_image(image, "Logistic Regression", "Multimode")
prediction = logreg_multimode_model.predict(processed_image)
return "Logistic Regression Multimode prediction: " + str(prediction[0])
def classify_multimode_knn(image):
processed_image = preprocess_image(image, "KNN", "Binary")
prediction = knn_model.predict(processed_image)
return "KNN Multimode prediction: " + str(prediction[0])
def classify_rf(image):
processed_image = preprocess_image(image , "DT" , "Multimode")
prediction = rf_model.predict(processed_image)
return "RF Prediction: " + str(prediction[0])
def predict_with_selected_model(image, model_choice, mode_choice):
if image is None:
return "Please upload an image."
if mode_choice == "Binary":
if model_choice == "GNB":
return classify_binary_gnb(image)
elif model_choice == "DT":
return classify_binary_dt(image)
elif model_choice == "SVM":
return classify_binary_svm(image)
elif model_choice == "Logistic Regression":
return classify_binary_logreg(image)
elif model_choice == "KNN":
return classify_knn(image)
else:
return "Unknown binary model selected."
elif mode_choice == "Multimode":
if model_choice == "GNB":
return classify_multimode_gnb(image)
elif model_choice == "DT":
return classify_multimode_dt(image)
elif model_choice == "SVM":
return classify_multimode_svm(image)
elif model_choice == "Logistic Regression":
return classify_multimode_logreg(image)
elif model_choice == "KNN":
return classify_multimode_knn(image)
elif model_choice == "Random Forest":
return classify_rf(image)
else:
return "Unknown multimode model selected."
else:
return "Unknown mode selected."
demo = gr.Interface(
fn=predict_with_selected_model,
inputs=[
gr.Image(
image_mode="L",
type="numpy",
label="Upload Digit Image"
),
gr.Radio(
choices=["GNB", "DT", "SVM", "Logistic Regression", "KNN" , "Random Forest"],
value="GNB",
label="Choose Model Type"
),
gr.Radio(
choices=["Binary", "Multimode"],
value="Binary",
label="Choose Classification Mode"
)
],
outputs=gr.Textbox(label="Prediction"),
title="MNIST Classifier",
description="Upload an image, select the model type, choose binary or multimode, and get a prediction."
)
if __name__ == "__main__":
demo.launch()