zainabbbbbbbbbb commited on
Commit
8436d4e
·
verified ·
1 Parent(s): 19be11c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -54
app.py CHANGED
@@ -1,81 +1,54 @@
1
- cascade_path = 'path/to/haarcascade_frontalface_default.xml'
2
-
3
- import streamlit as st
4
  import cv2
5
- import numpy as np
6
  from PIL import Image
7
- import os
8
 
9
- # Ensure Haar Cascade file is loaded correctly
10
  def load_cascade():
11
- # Path to the Haar Cascade file
12
  cascade_path = 'haarcascade_frontalface_default.xml'
13
 
14
  # Check if the Haar Cascade file exists
15
  if not os.path.exists(cascade_path):
16
- st.error("Haar Cascade file not found! Please upload the file.")
17
- raise Exception("Haar Cascade file not loaded!")
18
 
19
- # Load the Haar Cascade file
20
  face_cascade = cv2.CascadeClassifier(cascade_path)
 
 
21
  if face_cascade.empty():
22
- st.error("Haar Cascade file failed to load.")
23
- raise Exception("Haar Cascade file not loaded properly!")
24
 
25
  return face_cascade
26
 
27
- # Detect faces in the uploaded image
28
- def detect_faces(image, face_cascade):
29
- # Convert the image to grayscale
30
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
31
-
32
- # Perform face detection
33
- faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
34
-
35
- # Draw rectangles around detected faces
36
- for (x, y, w, h) in faces:
37
- cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
38
-
39
- return image, len(faces)
40
-
41
- # Main Streamlit app
42
  def main():
43
  st.title("Face Detection App")
44
- st.write("Upload an image, and the app will detect faces!")
45
 
46
- # Load the Haar Cascade
47
- try:
48
- face_cascade = load_cascade()
49
- except Exception as e:
50
- st.error(f"Error: {e}")
51
- return
52
 
53
- # File uploader for image
54
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
55
- if uploaded_file is not None:
56
- # Save the uploaded image temporarily
57
- img_path = '/tmp/uploaded_image.jpg'
58
- with open(img_path, "wb") as f:
59
- f.write(uploaded_file.getbuffer())
60
-
61
- # Open the uploaded image and convert it to a NumPy array
62
- image = np.array(Image.open(uploaded_file))
63
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
64
 
 
 
 
 
 
 
65
  # Detect faces in the image
66
- result_image, face_count = detect_faces(image, face_cascade)
67
 
68
- # Convert BGR to RGB for displaying in Streamlit
69
- result_image = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
 
70
 
71
- # Display the image with face detection results
72
- st.image(result_image, caption=f"Detected {face_count} face(s).", use_column_width=True)
 
73
 
74
- # Display success or failure message
75
- if face_count > 0:
76
- st.success("Face Detection Successful!")
77
  else:
78
- st.error("No faces detected. Please try again with a clearer image.")
79
 
80
  if __name__ == "__main__":
81
  main()
 
1
+ import os
 
 
2
  import cv2
3
+ import streamlit as st
4
  from PIL import Image
 
5
 
 
6
  def load_cascade():
7
+ # Specify the path to the Haar Cascade XML file
8
  cascade_path = 'haarcascade_frontalface_default.xml'
9
 
10
  # Check if the Haar Cascade file exists
11
  if not os.path.exists(cascade_path):
12
+ raise Exception(f"Haar Cascade file not found at {cascade_path}. Please upload the file.")
 
13
 
14
+ # Load the Haar Cascade classifier
15
  face_cascade = cv2.CascadeClassifier(cascade_path)
16
+
17
+ # Check if the file was successfully loaded
18
  if face_cascade.empty():
19
+ raise Exception(f"Failed to load Haar Cascade file from {cascade_path}.")
 
20
 
21
  return face_cascade
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def main():
24
  st.title("Face Detection App")
 
25
 
26
+ # Load Haar Cascade for face detection
27
+ face_cascade = load_cascade()
 
 
 
 
28
 
29
+ uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
 
 
 
 
 
 
 
 
 
 
30
 
31
+ if uploaded_file is not None:
32
+ # Read the image file
33
+ image = Image.open(uploaded_file)
34
+ image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
35
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
36
+
37
  # Detect faces in the image
38
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
39
 
40
+ # Draw rectangles around faces
41
+ for (x, y, w, h) in faces:
42
+ cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
43
 
44
+ # Convert image back to RGB and display it
45
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
46
+ st.image(image, caption="Processed Image", use_column_width=True)
47
 
48
+ if len(faces) > 0:
49
+ st.success("Face detection successful!")
 
50
  else:
51
+ st.warning("No faces detected.")
52
 
53
  if __name__ == "__main__":
54
  main()