zainabbbbbbbbbb commited on
Commit
9dde88d
·
verified ·
1 Parent(s): 9604727

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -74
app.py CHANGED
@@ -1,91 +1,47 @@
1
- import cv2
2
  import streamlit as st
3
- import imutils
4
  import numpy as np
5
- import matplotlib.pyplot as plt
6
  from PIL import Image
7
 
8
- # Streamlit page configuration
9
- st.set_page_config(page_title="Real-time Face Detection", layout="wide")
 
10
 
11
- # Title for the Streamlit app
12
- st.title("Real-time Face Detection App")
13
 
14
- # Function to display images using matplotlib
15
- def plt_imshow(title, image):
16
- # Convert the image frame from BGR to RGB color space
17
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
18
- plt.imshow(image)
19
- plt.title(title)
20
- plt.axis('off')
21
- st.pyplot(plt)
22
 
23
- # Upload the image
24
- uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
25
 
26
  if uploaded_file is not None:
27
- # Read the uploaded image
28
- image = Image.open(uploaded_file)
29
- image = np.array(image)
30
-
31
- # Load the Haar Cascade face detector
32
- cascade_path = "haarcascade_frontalface_default.xml"
33
- detector = cv2.CascadeClassifier(cascade_path)
34
-
35
- # Convert the image to grayscale
36
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
37
-
38
- # Perform face detection
39
- rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
40
-
41
- # Draw bounding boxes around detected faces
42
- for (x, y, w, h) in rects:
43
- cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
44
-
45
- # Display the result in Streamlit
46
- st.image(image, caption="Face Detected", channels="BGR", use_column_width=True)
47
 
48
- # Upload video file
49
- uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi"])
50
-
51
- if uploaded_video is not None:
52
- # Open the uploaded video
53
- video_file = uploaded_video.read()
54
-
55
- # Use OpenCV to read the video
56
- video_capture = cv2.VideoCapture(video_file)
57
-
58
- # Load the Haar Cascade face detector
59
- cascade_path = "haarcascade_frontalface_default.xml"
60
- detector = cv2.CascadeClassifier(cascade_path)
61
-
62
- # Create a video writer to save the output video
63
- fourcc = cv2.VideoWriter_fourcc(*"MJPG")
64
- output_file = "output_video.avi"
65
- writer = cv2.VideoWriter(output_file, fourcc, 20, (640, 480))
66
-
67
- while video_capture.isOpened():
68
- ret, frame = video_capture.read()
69
- if not ret:
70
- break
71
-
72
- # Resize and convert the frame to grayscale
73
- frame = imutils.resize(frame, width=500)
74
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
75
 
76
  # Perform face detection
77
- rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
 
 
 
 
78
 
79
  # Draw bounding boxes around detected faces
80
  for (x, y, w, h) in rects:
81
- cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
82
-
83
- # Write the processed frame to the output video
84
- writer.write(frame)
85
 
86
- # Release the video capture and writer objects
87
- video_capture.release()
88
- writer.release()
89
 
90
- # Show the output video in Streamlit
91
- st.video(output_file)
 
 
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)