Hammad712 commited on
Commit
7008c9f
·
verified ·
1 Parent(s): 1042775

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import cv2
5
+ from huggingface_hub import hf_hub_download
6
+ from tensorflow.keras.models import load_model
7
+ from io import BytesIO
8
+
9
+ # Authenticate and download model from Hugging Face
10
+ repo_id = "Hammad712/closed_eye_detection"
11
+ filename = "Closed_Eye_Detection_98.h5"
12
+ model_path = hf_hub_download(repo_id=repo_id, filename=filename)
13
+
14
+ # Load the downloaded model
15
+ model = load_model(model_path)
16
+
17
+ # Set image dimensions
18
+ img_height, img_width = 150, 150
19
+
20
+ # Custom CSS
21
+ def set_css(style):
22
+ st.markdown(f"<style>{style}</style>", unsafe_allow_html=True)
23
+
24
+ combined_css = """
25
+ .main, .sidebar .sidebar-content { background-color: #1c1c1c; color: #f0f2f6; }
26
+ .block-container { padding: 1rem 2rem; background-color: #333; border-radius: 10px; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.5); }
27
+ .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; }
28
+ .stSpinner { color: #4CAF50; }
29
+ .title {
30
+ font-size: 3rem;
31
+ font-weight: bold;
32
+ display: flex;
33
+ align-items: center;
34
+ justify-content: center;
35
+ }
36
+ .colorful-text {
37
+ background: -webkit-linear-gradient(135deg, #ff7e5f, #feb47b);
38
+ -webkit-background-clip: text;
39
+ -webkit-text-fill-color: transparent;
40
+ }
41
+ .black-white-text {
42
+ color: black;
43
+ }
44
+ .small-input .stTextInput>div>input {
45
+ height: 2rem;
46
+ font-size: 0.9rem;
47
+ }
48
+ .small-file-uploader .stFileUploader>div>div {
49
+ height: 2rem;
50
+ font-size: 0.9rem;
51
+ }
52
+ .custom-text {
53
+ font-size: 1.2rem;
54
+ color: #feb47b;
55
+ text-align: center;
56
+ margin-top: -20px;
57
+ margin-bottom: 20px;
58
+ }
59
+ """
60
+
61
+ # Streamlit application
62
+ st.set_page_config(layout="wide")
63
+
64
+ st.markdown(f"<style>{combined_css}</style>", unsafe_allow_html=True)
65
+
66
+ st.markdown('<div class="title"><span class="colorful-text">Eye</span> <span class="black-white-text">Detection Model</span></div>', unsafe_allow_html=True)
67
+ st.markdown('<div class="custom-text">Upload an image to predict whether the eyes are open or closed.</div>', unsafe_allow_html=True)
68
+
69
+ # Input for image URL or path
70
+ with st.expander("Input Options", expanded=True):
71
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
72
+
73
+ if uploaded_file is not None:
74
+ # Read the uploaded image
75
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
76
+ image = cv2.imdecode(file_bytes, 1)
77
+
78
+ # Resize and preprocess the image
79
+ resized_image = cv2.resize(image, (img_height, img_width))
80
+ input_image = resized_image.reshape((1, img_height, img_width, 3)) / 255.0
81
+
82
+ # Perform inference
83
+ predictions = model.predict(input_image)
84
+ prediction = predictions[0][0]
85
+
86
+ def get_label(prediction):
87
+ return "Open Eye" if prediction >= 0.5 else "Closed Eye"
88
+
89
+ label = get_label(prediction)
90
+
91
+ # Display the image and prediction
92
+ st.image(image, channels="BGR", caption='Uploaded Image')
93
+ st.markdown(f"### Prediction: {prediction:.2f}, Label: {label}")
94
+
95
+ # Provide a download button for the uploaded image (optional)
96
+ img_byte_arr = BytesIO()
97
+ img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
98
+ img.save(img_byte_arr, format='JPEG')
99
+ img_byte_arr = img_byte_arr.getvalue()
100
+
101
+ st.download_button(
102
+ label="Download Image",
103
+ data=img_byte_arr,
104
+ file_name="uploaded_image.jpg",
105
+ mime="image/jpeg"
106
+ )