Hammad712 commited on
Commit
2db11bd
·
verified ·
1 Parent(s): 4df8c44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -5,7 +5,8 @@ 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 # Import PIL Image
 
9
 
10
  # Authenticate and download model from Hugging Face
11
  repo_id = "Hammad712/closed_eye_detection"
@@ -65,16 +66,27 @@ st.set_page_config(layout="wide")
65
  st.markdown(f"<style>{combined_css}</style>", unsafe_allow_html=True)
66
 
67
  st.markdown('<div class="title"><span class="colorful-text">Eye</span> <span class="black-white-text">Detection Model</span></div>', unsafe_allow_html=True)
68
- st.markdown('<div class="custom-text">Upload an image to predict whether the eyes are open or closed.</div>', unsafe_allow_html=True)
69
 
70
  # Input for image URL or path
71
  with st.expander("Input Options", expanded=True):
72
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
73
-
74
- if uploaded_file is not None:
75
- # Read the uploaded image
76
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
77
- image = cv2.imdecode(file_bytes, 1)
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # Resize and preprocess the image
80
  resized_image = cv2.resize(image, (img_height, img_width))
@@ -90,7 +102,7 @@ if uploaded_file is not None:
90
  label = get_label(prediction)
91
 
92
  # Display the image and prediction
93
- st.image(image, channels="BGR", caption='Uploaded Image')
94
  st.markdown(f"### Prediction: {prediction:.2f}, Label: {label}")
95
 
96
  # Provide a download button for the uploaded image (optional)
@@ -102,6 +114,6 @@ if uploaded_file is not None:
102
  st.download_button(
103
  label="Download Image",
104
  data=img_byte_arr,
105
- file_name="uploaded_image.jpg",
106
  mime="image/jpeg"
107
  )
 
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"
 
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))
 
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)
 
114
  st.download_button(
115
  label="Download Image",
116
  data=img_byte_arr,
117
+ file_name="processed_image.jpg",
118
  mime="image/jpeg"
119
  )