Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,45 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
# Load the Haar Cascade face detector
|
| 7 |
cascade_path = "haarcascade_frontalface_default.xml"
|
| 8 |
detector = cv2.CascadeClassifier(cascade_path)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
st.
|
| 15 |
-
st.sidebar.write("Upload an image to detect faces.")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
if
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
st.write(f"Detected {len(rects)} face(s).")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
# Display the image with detected faces
|
| 47 |
-
st.image(image_rgb, caption="Detected Faces", use_column_width=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
# Load the Haar Cascade face detector
|
| 6 |
cascade_path = "haarcascade_frontalface_default.xml"
|
| 7 |
detector = cv2.CascadeClassifier(cascade_path)
|
| 8 |
|
| 9 |
+
# Check if the cascade file is loaded
|
| 10 |
+
if detector.empty():
|
| 11 |
+
st.error("Error: Could not load Haar Cascade. Ensure the XML file is in the correct location.")
|
| 12 |
+
else:
|
| 13 |
+
# Streamlit app title
|
| 14 |
+
st.title("Face Detection App")
|
| 15 |
|
| 16 |
+
# File uploader
|
| 17 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
|
| 18 |
|
| 19 |
+
if uploaded_file is not None:
|
| 20 |
+
# Convert uploaded file to OpenCV format
|
| 21 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 22 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 23 |
|
| 24 |
+
if image is None:
|
| 25 |
+
st.error("Error: Could not process the uploaded image.")
|
| 26 |
+
else:
|
| 27 |
+
# Convert image to grayscale
|
| 28 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 29 |
|
| 30 |
+
# Perform face detection
|
| 31 |
+
rects = detector.detectMultiScale(
|
| 32 |
+
gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)
|
| 33 |
+
)
|
| 34 |
+
st.write(f"Detected {len(rects)} face(s).")
|
|
|
|
| 35 |
|
| 36 |
+
# Draw bounding boxes around detected faces
|
| 37 |
+
for (x, y, w, h) in rects:
|
| 38 |
+
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Convert image to RGB for display
|
| 41 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
|
| 42 |
|
| 43 |
+
# Display the image
|
| 44 |
+
st.image(image_rgb, caption="Detected Faces", use_column_width=True)
|
| 45 |
|
|
|
|
|
|