Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,54 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
-
try:
|
| 4 |
-
import cv2
|
| 5 |
-
st.write("OpenCV successfully imported!")
|
| 6 |
-
except ImportError as e:
|
| 7 |
-
st.error(f"Error importing OpenCV: {e}")
|
| 8 |
-
|
| 9 |
-
import streamlit as st
|
| 10 |
-
import cv2
|
| 11 |
-
import numpy as np
|
| 12 |
-
from PIL import Image
|
| 13 |
-
|
| 14 |
-
# Load Haar Cascade
|
| 15 |
-
@st.cache_resource
|
| 16 |
-
def load_cascade():
|
| 17 |
-
cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
| 18 |
-
if cascade.empty():
|
| 19 |
-
raise Exception("Haar Cascade file not loaded!")
|
| 20 |
-
return cascade
|
| 21 |
-
|
| 22 |
-
# Detect faces
|
| 23 |
-
def detect_faces(image, face_cascade):
|
| 24 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 25 |
-
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
|
| 26 |
-
for (x, y, w, h) in faces:
|
| 27 |
-
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
| 28 |
-
return image, len(faces)
|
| 29 |
-
|
| 30 |
-
# Streamlit app
|
| 31 |
-
def main():
|
| 32 |
-
st.title("Real-Time Face Detection App")
|
| 33 |
-
st.write("Upload an image, and the app will detect faces!")
|
| 34 |
-
|
| 35 |
-
# Load Haar Cascade
|
| 36 |
-
face_cascade = load_cascade()
|
| 37 |
-
import streamlit as st
|
| 38 |
import cv2
|
| 39 |
import numpy as np
|
| 40 |
from PIL import Image
|
| 41 |
import os
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
@st.cache_resource
|
| 45 |
def load_cascade():
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
raise Exception("Haar Cascade file not loaded!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return face_cascade
|
| 50 |
|
| 51 |
-
# Detect faces
|
| 52 |
def detect_faces(image, face_cascade):
|
|
|
|
| 53 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
| 54 |
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
|
|
|
|
|
|
|
| 55 |
for (x, y, w, h) in faces:
|
| 56 |
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
|
|
|
| 57 |
return image, len(faces)
|
| 58 |
|
| 59 |
-
# Streamlit app
|
| 60 |
def main():
|
| 61 |
st.title("Face Detection App")
|
| 62 |
st.write("Upload an image, and the app will detect faces!")
|
| 63 |
|
| 64 |
-
# Load Haar Cascade
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
# File uploader
|
| 68 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 69 |
if uploaded_file is not None:
|
| 70 |
# Save the uploaded image temporarily
|
|
@@ -72,44 +56,24 @@ def main():
|
|
| 72 |
with open(img_path, "wb") as f:
|
| 73 |
f.write(uploaded_file.getbuffer())
|
| 74 |
|
| 75 |
-
#
|
| 76 |
image = np.array(Image.open(uploaded_file))
|
| 77 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 78 |
|
| 79 |
-
# Detect faces
|
| 80 |
result_image, face_count = detect_faces(image, face_cascade)
|
| 81 |
|
| 82 |
-
# Convert BGR to RGB for
|
| 83 |
result_image = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
|
| 84 |
|
| 85 |
-
# Display the image with face detection
|
| 86 |
st.image(result_image, caption=f"Detected {face_count} face(s).", use_column_width=True)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
if face_count > 0:
|
| 90 |
st.success("Face Detection Successful!")
|
| 91 |
else:
|
| 92 |
st.error("No faces detected. Please try again with a clearer image.")
|
| 93 |
|
| 94 |
-
if __name__ == "__main__":
|
| 95 |
-
main()
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
# File uploader
|
| 99 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 100 |
-
if uploaded_file is not None:
|
| 101 |
-
# Read the image
|
| 102 |
-
image = np.array(Image.open(uploaded_file))
|
| 103 |
-
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 104 |
-
|
| 105 |
-
# Detect faces
|
| 106 |
-
result_image, face_count = detect_faces(image, face_cascade)
|
| 107 |
-
|
| 108 |
-
# Convert BGR to RGB for display
|
| 109 |
-
result_image = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
|
| 110 |
-
|
| 111 |
-
# Display the image
|
| 112 |
-
st.image(result_image, caption=f"Detected {face_count} face(s).", use_column_width=True)
|
| 113 |
-
|
| 114 |
if __name__ == "__main__":
|
| 115 |
main()
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Ensure Haar Cascade file is loaded correctly
|
|
|
|
| 8 |
def load_cascade():
|
| 9 |
+
# Path to the Haar Cascade file
|
| 10 |
+
cascade_path = 'haarcascade_frontalface_default.xml'
|
| 11 |
+
|
| 12 |
+
# Check if the Haar Cascade file exists
|
| 13 |
+
if not os.path.exists(cascade_path):
|
| 14 |
+
st.error("Haar Cascade file not found! Please upload the file.")
|
| 15 |
raise Exception("Haar Cascade file not loaded!")
|
| 16 |
+
|
| 17 |
+
# Load the Haar Cascade file
|
| 18 |
+
face_cascade = cv2.CascadeClassifier(cascade_path)
|
| 19 |
+
if face_cascade.empty():
|
| 20 |
+
st.error("Haar Cascade file failed to load.")
|
| 21 |
+
raise Exception("Haar Cascade file not loaded properly!")
|
| 22 |
+
|
| 23 |
return face_cascade
|
| 24 |
|
| 25 |
+
# Detect faces in the uploaded image
|
| 26 |
def detect_faces(image, face_cascade):
|
| 27 |
+
# Convert the image to grayscale
|
| 28 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 29 |
+
|
| 30 |
+
# Perform face detection
|
| 31 |
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
|
| 32 |
+
|
| 33 |
+
# Draw rectangles around detected faces
|
| 34 |
for (x, y, w, h) in faces:
|
| 35 |
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
| 36 |
+
|
| 37 |
return image, len(faces)
|
| 38 |
|
| 39 |
+
# Main Streamlit app
|
| 40 |
def main():
|
| 41 |
st.title("Face Detection App")
|
| 42 |
st.write("Upload an image, and the app will detect faces!")
|
| 43 |
|
| 44 |
+
# Load the Haar Cascade
|
| 45 |
+
try:
|
| 46 |
+
face_cascade = load_cascade()
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error(f"Error: {e}")
|
| 49 |
+
return
|
| 50 |
|
| 51 |
+
# File uploader for image
|
| 52 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 53 |
if uploaded_file is not None:
|
| 54 |
# Save the uploaded image temporarily
|
|
|
|
| 56 |
with open(img_path, "wb") as f:
|
| 57 |
f.write(uploaded_file.getbuffer())
|
| 58 |
|
| 59 |
+
# Open the uploaded image and convert it to a NumPy array
|
| 60 |
image = np.array(Image.open(uploaded_file))
|
| 61 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 62 |
|
| 63 |
+
# Detect faces in the image
|
| 64 |
result_image, face_count = detect_faces(image, face_cascade)
|
| 65 |
|
| 66 |
+
# Convert BGR to RGB for displaying in Streamlit
|
| 67 |
result_image = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
|
| 68 |
|
| 69 |
+
# Display the image with face detection results
|
| 70 |
st.image(result_image, caption=f"Detected {face_count} face(s).", use_column_width=True)
|
| 71 |
|
| 72 |
+
# Display success or failure message
|
| 73 |
if face_count > 0:
|
| 74 |
st.success("Face Detection Successful!")
|
| 75 |
else:
|
| 76 |
st.error("No faces detected. Please try again with a clearer image.")
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
if __name__ == "__main__":
|
| 79 |
main()
|