Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,6 +34,66 @@ def main():
|
|
| 34 |
|
| 35 |
# Load Haar Cascade
|
| 36 |
face_cascade = load_cascade()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# File uploader
|
| 39 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
|
|
|
| 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 |
+
# Load Haar Cascade
|
| 44 |
+
@st.cache_resource
|
| 45 |
+
def load_cascade():
|
| 46 |
+
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
| 47 |
+
if face_cascade.empty():
|
| 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 |
+
face_cascade = load_cascade()
|
| 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
|
| 71 |
+
img_path = '/tmp/uploaded_image.jpg'
|
| 72 |
+
with open(img_path, "wb") as f:
|
| 73 |
+
f.write(uploaded_file.getbuffer())
|
| 74 |
+
|
| 75 |
+
# Read and process the image
|
| 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 display
|
| 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 |
+
# Show success or failure message based on face count
|
| 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"])
|