Abbas133 commited on
Commit
ab3d215
·
verified ·
1 Parent(s): 6421398

Create app.py

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