Spaces:
Runtime error
Runtime error
Download app.py from trs/Image_Classification_with_pre-traind_Models: direct link, hf CLI and curl.
- Browser
- Download file 2.76 kB
-
https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/685fa671b8c4cd146d3ec6f0197a7e3cc040b435/app.py
- Command line
-
hf download hf://spaces/trs/Image_Classification_with_pre-traind_Models@685fa671b8c4cd146d3ec6f0197a7e3cc040b435/app.py
-
curl -L -o app.py https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/685fa671b8c4cd146d3ec6f0197a7e3cc040b435/app.py
2.76 kB
| import numpy as np | |
| import os | |
| import cv2 | |
| import gradio as gr | |
| import tensorflow as tf | |
| inception_model_path = './inceptionv3_-saved-model-97-loss-0.00.hdf5' | |
| vggnet_model_path = './resnet50-saved-model-79-val_acc-1.00.hdf5' | |
| resnet_model_path = './vgg_16_-saved-model-89-acc-0.93.hdf5' | |
| efficientnet_model_path = './efficientnet-saved-model-06-val_acc-0.98.hdf5' | |
| inception_model = tf.keras.models.load_model(inception_model_path) | |
| vggnet_model = tf.keras.models.load_model(vggnet_model_path) | |
| resnet_model = tf.keras.models.load_model(resnet_model_path) | |
| efficientnet_model = tf.keras.models.load_model(efficientnet_model_path) | |
| # Define class names corresponding to your dataset | |
| class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names | |
| # Define the classify_image function | |
| def classify_image(img, model_choice): | |
| # Resize the input image based on the selected model's expected input shape | |
| if model_choice == "ResNet": | |
| img = tf.image.resize(img, (150, 150)) # ResNet50 input size | |
| img = image.img_to_array(img) | |
| img = img.copy() # Make a copy of the image array | |
| img = resnet_preprocess_input(img) | |
| elif model_choice == "EfficientNet": | |
| img = tf.image.resize(img, (150, 150)) | |
| img = image.img_to_array(img) | |
| img = preprocess_input(img) | |
| elif model_choice == "VGGNet": | |
| img = tf.image.resize(img, (150, 150)) | |
| img = image.img_to_array(img) | |
| img = preprocess_input(img) | |
| elif model_choice == "InceptionV3": | |
| img = tf.image.resize(img, (150, 150)) | |
| img = image.img_to_array(img) | |
| img = preprocess_input(img) | |
| else: | |
| return "Invalid model choice" | |
| img = tf.expand_dims(img, 0) # Add batch dimension | |
| # Make predictions based on the selected model | |
| if model_choice == "ResNet": | |
| predictions = resnet_model.predict(img) | |
| elif model_choice == "EfficientNet": | |
| predictions = efficientnet_model.predict(img) | |
| elif model_choice == "VGGNet": | |
| predictions = VGGNet_model.predict(img) | |
| elif model_choice == "InceptionV3": | |
| predictions = InceptionV3_model.predict(img) | |
| class_idx = tf.argmax(predictions[0]) | |
| # Map class index to class name | |
| class_name = class_names[class_idx] | |
| # Return the predicted class name as a string | |
| return f"Predicted class using {model_choice}: {class_name}" | |
| # Create a Gradio interface with a dropdown for model selection | |
| iface = gr.Interface( | |
| fn=classify_image, | |
| inputs=["image", gr.Dropdown(["ResNet", "EfficientNet","VGGNet","InceptionV3"], label="Select Model")], | |
| outputs="text", | |
| live=True | |
| ) | |
| # Launch the Gradio interface | |
| iface.launch(debug=True,share=True) | |