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.83 kB
-
https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/0d42a67f5da85d52025e126ab52847150a17fc7c/app.py
- Command line
-
hf download hf://spaces/trs/Image_Classification_with_pre-traind_Models@0d42a67f5da85d52025e126ab52847150a17fc7c/app.py
-
curl -L -o app.py https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/0d42a67f5da85d52025e126ab52847150a17fc7c/app.py
1.83 kB
| import tensorflow as tf | |
| import numpy as np | |
| import os | |
| import cv2 | |
| import gradio as gr | |
| # Load the models | |
| inception_model_path = 'path/to/inception_model.h5' | |
| vggnet_model_path = 'path/to/vggnet_model.h5' | |
| resnet_model_path = 'path/to/resnet_model.h5' | |
| efficientnet_model_path = 'path/to/efficientnet_model.h5' | |
| # Load the models | |
| 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) | |
| def classify_selected_model(image, model_name): | |
| img = cv2.imread(image.name) | |
| img = cv2.resize(img, (150, 150)) | |
| img = np.expand_dims(img, axis=0) | |
| img = img / 255.0 | |
| if model_name.lower() == 'inceptionv3': | |
| predictions = inception_model.predict(img) | |
| elif model_name.lower() == 'vggnet': | |
| predictions = vggnet_model.predict(img) | |
| elif model_name.lower() == 'resnet': | |
| predictions = resnet_model.predict(img) | |
| elif model_name.lower() == 'efficientnet': | |
| predictions = efficientnet_model.predict(img) | |
| else: | |
| return {"error": "Invalid model selection"} | |
| class_index = np.argmax(predictions) | |
| class_name = class_folders[class_index] # Define class_folders based on your classes | |
| probability = predictions[0, class_index] | |
| print(f"Class Index: {class_index}") | |
| print(f"Class Name: {class_name}") | |
| print(f"Probability: {probability}") | |
| return {class_name: probability} | |
| image_input = gr.Image() | |
| model_selection_input = gr.Dropdown(["InceptionV3", "VGGNet", "ResNet", "EfficientNet"], value="InceptionV3") | |
| gr.Interface( | |
| fn=classify_selected_model, | |
| inputs=[image_input, model_selection_input], | |
| outputs="label", | |
| live=True | |
| ).launch(debug=True) | |