Spaces:
Sleeping
Sleeping
Commit ·
49c15d7
1
Parent(s): 6666885
part 2
Browse files- app.py +17 -12
- requirements.txt +2 -3
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
import cv2
|
|
|
|
| 6 |
|
| 7 |
# Load the model
|
| 8 |
model = tf.keras.models.load_model('eyeStateModel.h5')
|
|
@@ -12,7 +11,7 @@ def preprocess_image(image):
|
|
| 12 |
if len(image.shape) == 2:
|
| 13 |
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 14 |
|
| 15 |
-
# Resize image to
|
| 16 |
image = cv2.resize(image, (224, 224))
|
| 17 |
|
| 18 |
# Normalize pixel values
|
|
@@ -34,23 +33,29 @@ def predict_eye_state(image):
|
|
| 34 |
confidence = float(prediction[0][predicted_class])
|
| 35 |
|
| 36 |
# Map prediction to class name
|
| 37 |
-
class_names = ['
|
| 38 |
result = class_names[predicted_class]
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Create Gradio interface
|
| 44 |
-
|
| 45 |
fn=predict_eye_state,
|
| 46 |
-
inputs=gr.Image(
|
| 47 |
outputs=gr.Label(num_top_classes=2),
|
| 48 |
examples=[
|
| 49 |
-
["
|
| 50 |
-
["
|
| 51 |
],
|
| 52 |
title="Eye State Detection",
|
| 53 |
-
description="Upload an image to detect if
|
| 54 |
)
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import cv2
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
model = tf.keras.models.load_model('eyeStateModel.h5')
|
|
|
|
| 11 |
if len(image.shape) == 2:
|
| 12 |
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 13 |
|
| 14 |
+
# Resize image to 224x224 (matching the model's expected input)
|
| 15 |
image = cv2.resize(image, (224, 224))
|
| 16 |
|
| 17 |
# Normalize pixel values
|
|
|
|
| 33 |
confidence = float(prediction[0][predicted_class])
|
| 34 |
|
| 35 |
# Map prediction to class name
|
| 36 |
+
class_names = ['Eyes Closed', 'Eyes Open']
|
| 37 |
result = class_names[predicted_class]
|
| 38 |
|
| 39 |
+
# Create confidence dict
|
| 40 |
+
confidences = {
|
| 41 |
+
class_names[0]: float(prediction[0][0]),
|
| 42 |
+
class_names[1]: float(prediction[0][1])
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
return confidences
|
| 46 |
|
| 47 |
# Create Gradio interface
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
fn=predict_eye_state,
|
| 50 |
+
inputs=gr.Image(),
|
| 51 |
outputs=gr.Label(num_top_classes=2),
|
| 52 |
examples=[
|
| 53 |
+
["sample1.jpg"],
|
| 54 |
+
["sample2.jpg"]
|
| 55 |
],
|
| 56 |
title="Eye State Detection",
|
| 57 |
+
description="Upload an image of eyes to detect if they are open or closed.",
|
| 58 |
)
|
| 59 |
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
tensorflow==2.13.0
|
| 2 |
-
|
| 3 |
-
Pillow==10.0.0
|
| 4 |
numpy==1.23.5
|
| 5 |
-
opencv-python-headless==4.8.
|
|
|
|
| 1 |
tensorflow==2.13.0
|
| 2 |
+
Pillow==9.3.0
|
|
|
|
| 3 |
numpy==1.23.5
|
| 4 |
+
opencv-python-headless==4.8.0.74
|