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.72 kB
-
https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/bf55a699e23e3fef002768d3dfe9a58fd97c20ca/app.py
- Command line
-
hf download hf://spaces/trs/Image_Classification_with_pre-traind_Models@bf55a699e23e3fef002768d3dfe9a58fd97c20ca/app.py
-
curl -L -o app.py https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/bf55a699e23e3fef002768d3dfe9a58fd97c20ca/app.py
2.72 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_path='./Models/inceptionv3_-saved-model-59-val_accuracy-0.94.hdf5' | |
| VGGNet_path='./Models/vgg_16_-saved-model-89-acc-0.93.hdf5' | |
| ResNet_path= './Models/resnet50-saved-model-79-val_acc-1.00.hdf5' | |
| EfficientNet_path= './Models/efficientnet-saved-model-06-val_acc-0.98.hdf5' | |
| Inception_model = load_model(Inception_path) | |
| VGGNet_model = load_model(VGGNet_path) | |
| ResNet_model = load_model(ResNet_path) | |
| Efficient_model = load_model(EfficientNet_path) | |
| 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_index = np.argmax(predictions[0]) | |
| class_name = class_folders[class_index] | |
| probability = round(max(predictions[0]) * 100, 2) | |
| return class_name, probability | |
| except Exception as e: | |
| return "Error", None | |
| def export_results(): | |
| # In this example, you can use the values from the current state of widgets | |
| class_name, probability = classify_selected_model(image_input.value, model_dropdown.value) | |
| model_name = model_dropdown.value | |
| result_str = f"Predicted Class: {class_name}\nProbability: {probability}%\nModel: {model_name}" | |
| result_label.value = result_str | |
| return result_str | |
| image_input = gr.Image(type='filepath', label="Input Image") | |
| model_dropdown = gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model") | |
| result_label = gr.Label(value="Exported Result will be displayed here") | |
| iface = gr.Interface( | |
| fn=classify_selected_model, | |
| inputs=[image_input, model_dropdown], | |
| outputs=[ | |
| gr.Label(value="Archaeological Site"), | |
| gr.Label(value="Probability"), | |
| result_label], | |
| examples=[ | |
| ["Sites/Ajloun.png"], | |
| ["Sites/Jeresh.png"], | |
| ["Sites/Umm Qais.png"], | |
| ["Sites/Petra.png"], | |
| ["Sites/Roman Amphitheater.png"], | |
| ["Sites/Wadi Rum.png"] ] , | |
| live=True, | |
| title="Image Classification for Archaeological Sites Interface", | |
| ) | |
| iface.launch(debug=True, share=True ) | |