vargar commited on
Commit
b0ed203
·
verified ·
1 Parent(s): e4a6f06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -1,33 +1,33 @@
 
1
  import tensorflow as tf
2
  import numpy as np
3
- import gradio as gr
4
 
5
  # Load the model
6
  model = tf.keras.models.load_model("vgg19_binary_nonbinary.h5")
7
 
8
- # Define the preprocessing function
9
  def preprocess_image(image):
10
- image = image.resize((224, 224)) # Resize to match model input size
11
- image = np.array(image) / 255.0 # Normalize pixel values
12
- image = np.expand_dims(image, axis=0) # Add batch dimension
13
  return image
14
 
15
- # Define the prediction function
16
- def classify_image(image):
 
 
 
 
 
 
 
 
 
17
  processed_image = preprocess_image(image)
18
  predictions = model.predict(processed_image)
19
  class_names = ["binary", "non-binary"]
20
  confidence = {class_names[i]: float(predictions[0][i]) for i in range(2)}
21
- return confidence
22
-
23
- # Create a Gradio interface
24
- interface = gr.Interface(
25
- fn=classify_image,
26
- inputs=gr.Image(type="pil"), # Input is an image
27
- outputs=gr.Label(num_top_classes=2), # Output is a label with confidence scores
28
- title="Binary vs Non-Binary Image Classification",
29
- description="Upload an image to classify it as 'binary' or 'non-binary'.",
30
- )
31
 
32
- # Launch the app
33
- interface.launch()
 
1
+ import streamlit as st
2
  import tensorflow as tf
3
  import numpy as np
4
+ from PIL import Image
5
 
6
  # Load the model
7
  model = tf.keras.models.load_model("vgg19_binary_nonbinary.h5")
8
 
9
+ # Preprocess the image
10
  def preprocess_image(image):
11
+ image = image.resize((224, 224))
12
+ image = np.array(image) / 255.0
13
+ image = np.expand_dims(image, axis=0)
14
  return image
15
 
16
+ # Streamlit app
17
+ st.title("Binary vs Non-Binary Image Classification")
18
+ st.write("Upload an image to classify it as 'binary' or 'non-binary'.")
19
+
20
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
21
+ if uploaded_file is not None:
22
+ image = Image.open(uploaded_file)
23
+ st.image(image, caption="Uploaded Image", use_column_width=True)
24
+ st.write("Classifying...")
25
+
26
+ # Preprocess and predict
27
  processed_image = preprocess_image(image)
28
  predictions = model.predict(processed_image)
29
  class_names = ["binary", "non-binary"]
30
  confidence = {class_names[i]: float(predictions[0][i]) for i in range(2)}
 
 
 
 
 
 
 
 
 
 
31
 
32
+ st.write("Prediction:")
33
+ st.write(confidence)