Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,14 @@ import numpy as np
|
|
| 3 |
import cv2
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def preprocess_image(image):
|
| 7 |
# Convert to RGB if needed
|
| 8 |
if len(image.shape) == 2:
|
|
@@ -21,38 +29,32 @@ def preprocess_image(image):
|
|
| 21 |
return image
|
| 22 |
|
| 23 |
def predict_eye_state(image):
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
if image is None:
|
| 26 |
-
return {
|
| 27 |
-
"Error": 1.0,
|
| 28 |
-
"Message": "Please provide an image"
|
| 29 |
-
}
|
| 30 |
|
| 31 |
# Preprocess the image
|
| 32 |
processed_image = preprocess_image(image)
|
| 33 |
|
| 34 |
-
# Load model and make prediction
|
| 35 |
-
with tf.keras.utils.custom_object_scope({'GlorotUniform': tf.keras.initializers.glorot_uniform()}):
|
| 36 |
-
model = tf.keras.models.load_model('eyeStateModel.h5', compile=False)
|
| 37 |
-
|
| 38 |
# Make prediction
|
| 39 |
prediction = model.predict(processed_image)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
class_names[0]: float(prediction[0][0]),
|
| 45 |
-
class_names[1]: float(prediction[0][1])
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error in prediction: {str(e)}")
|
| 52 |
-
return {
|
| 53 |
-
"Error": 1.0,
|
| 54 |
-
"Message": f"Error during prediction: {str(e)}"
|
| 55 |
-
}
|
| 56 |
|
| 57 |
# Create Gradio interface
|
| 58 |
demo = gr.Interface(
|
|
@@ -60,7 +62,7 @@ demo = gr.Interface(
|
|
| 60 |
inputs=gr.Image(),
|
| 61 |
outputs=gr.Label(num_top_classes=2),
|
| 62 |
title="Eye State Detection",
|
| 63 |
-
description="Upload an image of eyes to detect if they are open or closed.
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import cv2
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Load model once at startup
|
| 7 |
+
try:
|
| 8 |
+
model = tf.keras.models.load_model('eyeStateModel.h5', compile=False)
|
| 9 |
+
print("Model loaded successfully!")
|
| 10 |
+
except Exception as e:
|
| 11 |
+
print(f"Error loading model: {str(e)}")
|
| 12 |
+
model = None
|
| 13 |
+
|
| 14 |
def preprocess_image(image):
|
| 15 |
# Convert to RGB if needed
|
| 16 |
if len(image.shape) == 2:
|
|
|
|
| 29 |
return image
|
| 30 |
|
| 31 |
def predict_eye_state(image):
|
| 32 |
+
if model is None:
|
| 33 |
+
return {"Error": 1.0}
|
| 34 |
+
|
| 35 |
try:
|
| 36 |
if image is None:
|
| 37 |
+
return {"Error": 1.0}
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Preprocess the image
|
| 40 |
processed_image = preprocess_image(image)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Make prediction
|
| 43 |
prediction = model.predict(processed_image)
|
| 44 |
|
| 45 |
+
# Get confidence scores
|
| 46 |
+
closed_conf = float(prediction[0][0])
|
| 47 |
+
open_conf = float(prediction[0][1])
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Return dictionary with numeric values
|
| 50 |
+
return {
|
| 51 |
+
"Eyes Closed": closed_conf,
|
| 52 |
+
"Eyes Open": open_conf
|
| 53 |
+
}
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error in prediction: {str(e)}")
|
| 57 |
+
return {"Error": 1.0}
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Create Gradio interface
|
| 60 |
demo = gr.Interface(
|
|
|
|
| 62 |
inputs=gr.Image(),
|
| 63 |
outputs=gr.Label(num_top_classes=2),
|
| 64 |
title="Eye State Detection",
|
| 65 |
+
description="Upload an image of eyes to detect if they are open or closed."
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|