zainabbbbbbbbbb commited on
Commit
9d900a9
·
verified ·
1 Parent(s): 9602857

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
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
- # Streamlit app title
11
- st.title("Face Detection App")
 
 
 
 
12
 
13
- # Sidebar instructions
14
- st.sidebar.title("Upload an Image")
15
- st.sidebar.write("Upload an image to detect faces.")
16
 
17
- # Upload file option
18
- uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
 
19
 
20
- if uploaded_file is not None:
21
- # Convert uploaded file to OpenCV format
22
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
23
- image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
 
24
 
25
- # Check if the image was loaded successfully
26
- if image is None:
27
- st.error("Error: Could not load the image. Please try again.")
28
- else:
29
- # Convert image to grayscale
30
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
31
 
32
- # Perform face detection
33
- rects = detector.detectMultiScale(
34
- gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)
35
- )
36
-
37
- st.write(f"Detected {len(rects)} face(s).")
38
 
39
- # Draw bounding boxes around detected faces
40
- for (x, y, w, h) in rects:
41
- cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
42
 
43
- # Convert the image back to RGB for display in Streamlit
44
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
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