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 1.99 kB
-
https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/7e9bbfe5f0cb4adb17afd990dfbde2cd2721041b/app.py
- Command line
-
hf download hf://spaces/trs/Image_Classification_with_pre-traind_Models@7e9bbfe5f0cb4adb17afd990dfbde2cd2721041b/app.py
-
curl -L -o app.py https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/7e9bbfe5f0cb4adb17afd990dfbde2cd2721041b/app.py
1.99 kB
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| class_folders = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] | |
| Inception_model = ort.InferenceSession('./Models_onnx/inceptionv3.onnx') | |
| VGGNet_model = ort.InferenceSession('./Models_onnx/vgg_16.onnx') | |
| ResNet_model = ort.InferenceSession('./Models_onnx/resnet50.onnx') | |
| Efficient_model = ort.InferenceSession('./Models_onnx/efficientnet.onnx') | |
| def classify_selected_model(image, model_name): | |
| try: | |
| img = cv2.imread(image) | |
| img = cv2.resize(img, (150, 150)) | |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| img = np.expand_dims(img, axis=0) | |
| if model_name == "ResNet": | |
| predictions = ResNet_model.predict(img) | |
| elif model_name == "EfficientNet": | |
| predictions = Efficient_model.predict(img) | |
| elif model_name == "VGGNet": | |
| predictions = VGGNet_model.predict(img) | |
| elif model_name == "InceptionV3": | |
| predictions = Inception_model.predict(img) | |
| class_indices = np.argsort(predictions[0])[::-1] # Sort indices in descending order of probability | |
| class_probabilities = predictions[0][class_indices] | |
| class_names = [class_folders[idx] for idx in class_indices] | |
| return class_names, class_probabilities.tolist() | |
| except Exception as e: | |
| return [str(e)], [None] # Return an error message and None for probability | |
| # Create Gradio interface | |
| image_input = gr.Image(type='filepath', label="Input Image") | |
| model_dropdown = gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model") | |
| iface = gr.Interface( | |
| fn=classify_selected_model, | |
| inputs=[image_input, model_dropdown], | |
| outputs=[ | |
| gr.Textbox(label="Predicted Classes"), | |
| gr.Number(label="Class Probabilities") | |
| ], | |
| tittle ="Image Classification Interface" , | |
| live=True | |
| ) | |
| iface.launch(debug=True, share=True) | |