Uploaded deployment file
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
model_path = 'C:/Users/kamel/Documents/Image Classification/model_checkpoint_manual_effnet.h5'
|
9 |
+
model = load_model(model_path)
|
10 |
+
|
11 |
+
# Define a function to preprocess the input image
|
12 |
+
def preprocess_image(img):
|
13 |
+
# Check if img is a file path or an image object
|
14 |
+
if isinstance(img, str):
|
15 |
+
# Load and preprocess the image
|
16 |
+
img = cv2.imread(img)
|
17 |
+
img = cv2.resize(img, (224, 224))
|
18 |
+
img = img / 255.0 # Normalize pixel values
|
19 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
20 |
+
elif isinstance(img, np.ndarray):
|
21 |
+
# If img is already an image array, resize it
|
22 |
+
img = cv2.resize(img, (224, 224))
|
23 |
+
img = img / 255.0 # Normalize pixel values
|
24 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
25 |
+
else:
|
26 |
+
raise ValueError("Unsupported input type. Please provide a file path or a NumPy array.")
|
27 |
+
|
28 |
+
return img
|
29 |
+
|
30 |
+
# Define the classification function
|
31 |
+
def classify_image(img):
|
32 |
+
# Preprocess the image
|
33 |
+
img = preprocess_image(img)
|
34 |
+
|
35 |
+
# Make predictions
|
36 |
+
predictions = model.predict(img)
|
37 |
+
|
38 |
+
# Get the predicted class label
|
39 |
+
predicted_class = np.argmax(predictions)
|
40 |
+
|
41 |
+
return f"Predicted Class: {predicted_class}"
|
42 |
+
|
43 |
+
# Create a Gradio interface
|
44 |
+
iface = gr.Interface(fn=classify_image,
|
45 |
+
inputs="image",
|
46 |
+
outputs="text",
|
47 |
+
live=True)
|
48 |
+
|
49 |
+
# Launch the Gradio app
|
50 |
+
iface.launch()
|
51 |
+
|
52 |
+
|
53 |
+
# In[ ]:
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|