|
import streamlit as st |
|
import tensorflow as tf |
|
import numpy as np |
|
import cv2 |
|
from huggingface_hub import hf_hub_download |
|
from tensorflow.keras.models import load_model |
|
from io import BytesIO |
|
from PIL import Image |
|
|
|
|
|
repo_id = "Hammad712/closed_eye_detection" |
|
filename = "Closed_Eye_Detection_98.h5" |
|
model_path = hf_hub_download(repo_id=repo_id, filename=filename) |
|
|
|
|
|
model = load_model(model_path) |
|
|
|
|
|
img_height, img_width = 150, 150 |
|
|
|
|
|
def set_css(style): |
|
st.markdown(f"<style>{style}</style>", unsafe_allow_html=True) |
|
|
|
combined_css = """ |
|
.main, .sidebar .sidebar-content { background-color: #1c1c1c; color: #f0f2f6; } |
|
.block-container { padding: 1rem 2rem; background-color: #333; border-radius: 10px; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.5); } |
|
.stButton>button, .stDownloadButton>button { background: linear-gradient(135deg, #ff7e5f, #feb47b); color: white; border: none; padding: 10px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 5px; } |
|
.stSpinner { color: #4CAF50; } |
|
.title { |
|
font-size: 3rem; |
|
font-weight: bold; |
|
display: flex; |
|
align-items: center; |
|
justify-content: center; |
|
} |
|
.colorful-text { |
|
background: -webkit-linear-gradient(135deg, #ff7e5f, #feb47b); |
|
-webkit-background-clip: text; |
|
-webkit-text-fill-color: transparent; |
|
} |
|
.black-white-text { |
|
color: black; |
|
} |
|
.small-input .stTextInput>div>input { |
|
height: 2rem; |
|
font-size: 0.9rem; |
|
} |
|
.small-file-uploader .stFileUploader>div>div { |
|
height: 2rem; |
|
font-size: 0.9rem; |
|
} |
|
.custom-text { |
|
font-size: 1.2rem; |
|
color: #feb47b; |
|
text-align: center; |
|
margin-top: -20px; |
|
margin-bottom: 20px; |
|
} |
|
""" |
|
|
|
|
|
st.set_page_config(layout="wide") |
|
|
|
st.markdown(f"<style>{combined_css}</style>", unsafe_allow_html=True) |
|
|
|
st.markdown('<div class="title"><span class="colorful-text">Eye</span> <span class="black-white-text">Detection Model</span></div>', unsafe_allow_html=True) |
|
st.markdown('<div class="custom-text">Upload an image to predict whether the eyes are open or closed.</div>', unsafe_allow_html=True) |
|
|
|
|
|
with st.expander("Input Options", expanded=True): |
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8) |
|
image = cv2.imdecode(file_bytes, 1) |
|
|
|
|
|
resized_image = cv2.resize(image, (img_height, img_width)) |
|
input_image = resized_image.reshape((1, img_height, img_width, 3)) / 255.0 |
|
|
|
|
|
predictions = model.predict(input_image) |
|
prediction = predictions[0][0] |
|
|
|
def get_label(prediction): |
|
return "Open Eye" if prediction >= 0.5 else "Closed Eye" |
|
|
|
label = get_label(prediction) |
|
|
|
|
|
st.image(image, channels="BGR", caption='Uploaded Image') |
|
st.markdown(f"### Prediction: {prediction:.2f}, Label: {label}") |
|
|
|
|
|
img_byte_arr = BytesIO() |
|
img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) |
|
img.save(img_byte_arr, format='JPEG') |
|
img_byte_arr = img_byte_arr.getvalue() |
|
|
|
st.download_button( |
|
label="Download Image", |
|
data=img_byte_arr, |
|
file_name="uploaded_image.jpg", |
|
mime="image/jpeg" |
|
) |
|
|