Spaces:
Sleeping
Sleeping
Eric P. Nusbaum
commited on
Commit
·
ecce323
1
Parent(s):
34f5d81
Fix Dependencies
Browse files- app.py +26 -4
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,19 +1,41 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
# Load labels
|
| 8 |
with open('tensorflow/labels.txt', 'r') as f:
|
| 9 |
labels = f.read().splitlines()
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def preprocess_image(image):
|
| 15 |
# Resize image to the size expected by the model
|
| 16 |
-
target_size = (
|
| 17 |
image = image.resize(target_size)
|
| 18 |
image = np.array(image)
|
| 19 |
image = image / 255.0 # Normalize if required
|
|
@@ -22,7 +44,7 @@ def preprocess_image(image):
|
|
| 22 |
def predict(image):
|
| 23 |
image = preprocess_image(image)
|
| 24 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 25 |
-
predictions =
|
| 26 |
predicted_index = np.argmax(predictions, axis=1)[0]
|
| 27 |
predicted_label = labels[predicted_index]
|
| 28 |
confidence = predictions[0][predicted_index] * 100
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import tensorflow as tf
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Suppress TensorFlow logging for cleaner logs
|
| 10 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
| 11 |
+
|
| 12 |
# Load labels
|
| 13 |
with open('tensorflow/labels.txt', 'r') as f:
|
| 14 |
labels = f.read().splitlines()
|
| 15 |
|
| 16 |
+
# Function to load the frozen TensorFlow graph
|
| 17 |
+
def load_frozen_graph(pb_file_path):
|
| 18 |
+
with tf.io.gfile.GFile(pb_file_path, 'rb') as f:
|
| 19 |
+
graph_def = tf.compat.v1.GraphDef()
|
| 20 |
+
graph_def.ParseFromString(f.read())
|
| 21 |
+
|
| 22 |
+
with tf.Graph().as_default() as graph:
|
| 23 |
+
tf.import_graph_def(graph_def, name='')
|
| 24 |
+
return graph
|
| 25 |
+
|
| 26 |
+
# Load the TensorFlow model
|
| 27 |
+
MODEL_DIR = 'tensorflow/'
|
| 28 |
+
graph = load_frozen_graph(os.path.join(MODEL_DIR, 'model.pb'))
|
| 29 |
+
sess = tf.compat.v1.Session(graph=graph)
|
| 30 |
+
|
| 31 |
+
# Identify input and output tensor names
|
| 32 |
+
# You may need to adjust these based on your model's actual tensor names
|
| 33 |
+
input_tensor = graph.get_tensor_by_name('input:0') # Replace 'input:0' with your actual input tensor name
|
| 34 |
+
output_tensor = graph.get_tensor_by_name('output:0') # Replace 'output:0' with your actual output tensor name
|
| 35 |
|
| 36 |
def preprocess_image(image):
|
| 37 |
# Resize image to the size expected by the model
|
| 38 |
+
target_size = (320, 320) # Replace with your model's expected input size
|
| 39 |
image = image.resize(target_size)
|
| 40 |
image = np.array(image)
|
| 41 |
image = image / 255.0 # Normalize if required
|
|
|
|
| 44 |
def predict(image):
|
| 45 |
image = preprocess_image(image)
|
| 46 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 47 |
+
predictions = sess.run(output_tensor, feed_dict={input_tensor: image})
|
| 48 |
predicted_index = np.argmax(predictions, axis=1)[0]
|
| 49 |
predicted_label = labels[predicted_index]
|
| 50 |
confidence = predictions[0][predicted_index] * 100
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
-
tensorflow==2.11.0 #
|
| 3 |
Pillow
|
| 4 |
-
numpy
|
|
|
|
| 1 |
gradio
|
| 2 |
+
tensorflow==2.11.0 # Ensure this matches the TensorFlow version used during model training
|
| 3 |
Pillow
|
| 4 |
+
numpy<2.0.0
|