Spaces:
Build error
Build error
App
Browse files- .gitattributes +1 -0
- app.py +79 -0
- clf_Model.joblib +3 -0
- model.keras +3 -0
- save/14_class_features.npy +3 -0
- save/SVC-14classes.joblib +3 -0
- save/classes.joblib +3 -0
- save/labels.npy +3 -0
- save/pca.joblib +3 -0
- save/scaler.joblib +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
model.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# !pip install gradio ipywidgets
|
| 2 |
+
import joblib
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.decomposition import PCA
|
| 7 |
+
from sklearn.svm import SVC
|
| 8 |
+
import joblib
|
| 9 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 10 |
+
from tensorflow.keras.applications.vgg16 import preprocess_input # Import preprocess_input from appropriate Keras module
|
| 11 |
+
from PIL import Image
|
| 12 |
+
|
| 13 |
+
model = tf.keras.models.load_model("model.keras")
|
| 14 |
+
classes = joblib.load("save/classes.joblib")
|
| 15 |
+
|
| 16 |
+
clf = joblib.load('save/SVC-14classes.joblib')
|
| 17 |
+
scaler = joblib.load('save/scaler.joblib')
|
| 18 |
+
pca = joblib.load('save/pca.joblib')
|
| 19 |
+
|
| 20 |
+
def preprocess_image(image):
|
| 21 |
+
if isinstance(image, str): # Check if the input is a path
|
| 22 |
+
img = load_img(image, target_size=(224, 224)) # Load the image from the path and resize
|
| 23 |
+
else:
|
| 24 |
+
img = image.copy() # If the input is not a path, assume it's an image and make a copy
|
| 25 |
+
img = img.resize((224, 224)) # Resize the image to the desired target size
|
| 26 |
+
img = img.convert('RGB') # Ensure image is in RGB format
|
| 27 |
+
img_array = img_to_array(img) # Convert image to array
|
| 28 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 29 |
+
img_array = preprocess_input(img_array) # Preprocess the image for VGG16 model
|
| 30 |
+
img_array /= 255.0 # Rescale pixel values to the range [0, 1]
|
| 31 |
+
return img_array
|
| 32 |
+
|
| 33 |
+
def predict(image_path):
|
| 34 |
+
img = preprocess_image(image_path)
|
| 35 |
+
feature = model.predict(img)[0] # Extract features using the VGG16 model
|
| 36 |
+
feature = scaler.transform(feature.reshape(1, -1))
|
| 37 |
+
# Select only relevant features
|
| 38 |
+
feature = pca.transform(feature)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
pred = clf.predict_proba(feature.reshape(1, -1))[0]
|
| 43 |
+
|
| 44 |
+
top_classes = pred.argsort()[-3:][::-1] # Get indices of top 3 classes
|
| 45 |
+
top_classes_with_probs = []
|
| 46 |
+
for class_index in top_classes:
|
| 47 |
+
class_label = classes[class_index]
|
| 48 |
+
class_prob = pred[class_index]
|
| 49 |
+
top_classes_with_probs.append((class_label, class_prob))
|
| 50 |
+
|
| 51 |
+
# Format probabilities to 2 decimal places
|
| 52 |
+
formatted_probs = [(label, "{:.2f}".format(prob)) for label, prob in top_classes_with_probs]
|
| 53 |
+
|
| 54 |
+
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]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# https://www.gradio.app/guides
|
| 60 |
+
with gr.Blocks() as blocks:
|
| 61 |
+
path = gr.Image(label="Image", type="filepath")
|
| 62 |
+
label_1 = gr.Textbox(label="1st Label")
|
| 63 |
+
prob_1 = gr.Textbox(label="1st Probability")
|
| 64 |
+
label_2 = gr.Textbox(label="2nd Label")
|
| 65 |
+
prob_2 = gr.Textbox(label="2nd Probability")
|
| 66 |
+
label_3 = gr.Textbox(label="3rd Label")
|
| 67 |
+
prob_3 = gr.Textbox(label="3rd Probability")
|
| 68 |
+
|
| 69 |
+
inputs = [path]
|
| 70 |
+
outputs = [label_1, prob_1, label_2, prob_2, label_3, prob_3]
|
| 71 |
+
|
| 72 |
+
predict_btn = gr.Button("Predict")
|
| 73 |
+
predict_btn.click(predict, inputs=inputs, outputs=outputs)
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
blocks.launch() # Local machine only
|
| 77 |
+
#blocks.launch(server_name="0.0.0.0") # LAN access to local machine
|
| 78 |
+
#blocks.launch(share=True) # Public access to local machine
|
| 79 |
+
|
clf_Model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:194a09b275dffe68f49ae2458f831399dbf740e936045a1ed48414cba3bc64e3
|
| 3 |
+
size 124563497
|
model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f5d9d0795db596a3123167ab28315fe52bbd9e936579e8677a73faa8db553f0e
|
| 3 |
+
size 58925420
|
save/14_class_features.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ce971ee799c2004b7e6e667d9e47c332faccc557a8872867696501cd379c13bb
|
| 3 |
+
size 1369002112
|
save/SVC-14classes.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4f43cb5af81b0bb8e68c041b6d816ce9dffa999a02b9d681744e30c6a4e47070
|
| 3 |
+
size 124563449
|
save/classes.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1d7318f049ecf53dd2814db341a5fb9387e14995cb846f93b0489daed3c81812
|
| 3 |
+
size 187
|
save/labels.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5791572922dc489e141a38c17b899195252259d4ef7cc328fa3f7815b42f7ce8
|
| 3 |
+
size 54696
|
save/pca.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:33e95752086d8915145380f380c1bc6f0cb42c7a3251b58c320e7425c6a074e4
|
| 3 |
+
size 191896937
|
save/scaler.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d557d3aca632691c4fa6d658a8e1206ae206729ffb4cd17ba59495b71d5e42e0
|
| 3 |
+
size 602733
|