Spaces:
Sleeping
Sleeping
Download app.py from Pomp1/flowerver2: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/spaces/Pomp1/flowerver2/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pomp1/flowerver2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pomp1/flowerver2/resolve/main/app.py
3.12 kB
| # !pip install gradio ipywidgets | |
| import joblib | |
| import tensorflow as tf | |
| import gradio as gr | |
| import numpy as np | |
| from sklearn.decomposition import PCA | |
| from sklearn.svm import SVC | |
| import joblib | |
| from tensorflow.keras.preprocessing.image import load_img, img_to_array | |
| from tensorflow.keras.applications.vgg16 import preprocess_input # Import preprocess_input from appropriate Keras module | |
| from PIL import Image | |
| model = tf.keras.models.load_model("model.keras") | |
| classes = joblib.load("save/classes.joblib") | |
| clf = joblib.load('save/SVC-14classes.joblib') | |
| scaler = joblib.load('save/scaler.joblib') | |
| pca = joblib.load('save/pca.joblib') | |
| def preprocess_image(image): | |
| if isinstance(image, str): # Check if the input is a path | |
| img = load_img(image, target_size=(224, 224)) # Load the image from the path and resize | |
| else: | |
| img = image.copy() # If the input is not a path, assume it's an image and make a copy | |
| img = img.resize((224, 224)) # Resize the image to the desired target size | |
| img = img.convert('RGB') # Ensure image is in RGB format | |
| img_array = img_to_array(img) # Convert image to array | |
| img_array = np.expand_dims(img_array, axis=0) # Add batch dimension | |
| img_array = preprocess_input(img_array) # Preprocess the image for VGG16 model | |
| img_array /= 255.0 # Rescale pixel values to the range [0, 1] | |
| return img_array | |
| def predict(image_path): | |
| img = preprocess_image(image_path) | |
| feature = model.predict(img)[0] # Extract features using the VGG16 model | |
| feature = scaler.fit_transform(feature.reshape(1, -1)) | |
| # Select only relevant features | |
| feature = pca.transform(feature) | |
| pred = clf.predict_proba(feature.reshape(1, -1))[0] | |
| top_classes = pred.argsort()[-3:][::-1] # Get indices of top 3 classes | |
| top_classes_with_probs = [] | |
| for class_index in top_classes: | |
| class_label = classes[class_index] | |
| class_prob = pred[class_index] | |
| top_classes_with_probs.append((class_label, class_prob)) | |
| # Format probabilities to 2 decimal places | |
| formatted_probs = [(label, "{:.2f}".format(prob)) for label, prob in top_classes_with_probs] | |
| return formatted_probs[0][0], formatted_probs[0][1], formatted_probs[1][0], formatted_probs[1][1], formatted_probs[2][0], formatted_probs[2][1] | |
| # https://www.gradio.app/guides | |
| with gr.Blocks() as blocks: | |
| path = gr.Image(label="Image", type="filepath") | |
| label_1 = gr.Textbox(label="1st Label") | |
| prob_1 = gr.Textbox(label="1st Probability") | |
| label_2 = gr.Textbox(label="2nd Label") | |
| prob_2 = gr.Textbox(label="2nd Probability") | |
| label_3 = gr.Textbox(label="3rd Label") | |
| prob_3 = gr.Textbox(label="3rd Probability") | |
| inputs = [path] | |
| outputs = [label_1, prob_1, label_2, prob_2, label_3, prob_3] | |
| predict_btn = gr.Button("Predict") | |
| predict_btn.click(predict, inputs=inputs, outputs=outputs) | |
| if __name__ == "__main__": | |
| blocks.launch() # Local machine only | |
| #blocks.launch(server_name="0.0.0.0") # LAN access to local machine | |
| #blocks.launch(share=True) # Public access to local machine | |