srivatsavdamaraju commited on
Commit
3bd947f
·
verified ·
1 Parent(s): 2b9c40c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -20
app.py CHANGED
@@ -2,15 +2,13 @@ import cv2
2
  import streamlit as st
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
  # Load the pre-trained Haar Cascade face detector
7
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
8
 
 
9
  def detect_faces(frame):
10
- """
11
- Detect faces in the frame.
12
- Returns the frame with bounding boxes drawn around detected faces.
13
- """
14
  # Convert the frame to grayscale (Haar Cascade works on grayscale images)
15
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
16
 
@@ -26,23 +24,34 @@ def detect_faces(frame):
26
  # Streamlit UI for the app
27
  st.title("Real-Time Face Detection")
28
 
29
- # Capture the video from the webcam
30
- camera = st.camera_input("Capture a photo")
31
 
32
- # Process the webcam image if available
33
- if camera:
34
- # Convert the camera image into a numpy array
35
- img = Image.open(camera)
36
- img_array = np.array(img)
37
-
38
- # Convert the image to a format OpenCV can process (BGR)
39
- img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
40
 
41
- # Detect faces in the image
42
- result_frame = detect_faces(img_bgr)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- # Convert result frame back to RGB (for displaying in Streamlit)
45
- result_frame_rgb = cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB)
46
 
47
- # Display the result in Streamlit
48
- st.image(result_frame_rgb, caption="Detected Faces", use_column_width=True)
 
2
  import streamlit as st
3
  import numpy as np
4
  from PIL import Image
5
+ import time
6
 
7
  # Load the pre-trained Haar Cascade face detector
8
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
9
 
10
+ # Define a function to detect faces in a frame
11
  def detect_faces(frame):
 
 
 
 
12
  # Convert the frame to grayscale (Haar Cascade works on grayscale images)
13
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
14
 
 
24
  # Streamlit UI for the app
25
  st.title("Real-Time Face Detection")
26
 
27
+ # Create a container for the webcam video feed
28
+ video_placeholder = st.empty()
29
 
30
+ # Start the webcam feed
31
+ cap = cv2.VideoCapture(0) # 0 is the default webcam device
 
 
 
 
 
 
32
 
33
+ if not cap.isOpened():
34
+ st.error("Error: Could not access the webcam.")
35
+ else:
36
+ # Start capturing video frames
37
+ while True:
38
+ ret, frame = cap.read()
39
+
40
+ if not ret:
41
+ st.error("Failed to grab frame.")
42
+ break
43
+
44
+ # Detect faces in the current frame
45
+ result_frame = detect_faces(frame)
46
+
47
+ # Convert BGR (OpenCV format) to RGB (for Streamlit display)
48
+ result_frame_rgb = cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB)
49
+
50
+ # Display the frame in Streamlit (dynamically updating the image)
51
+ video_placeholder.image(result_frame_rgb, channels="RGB", use_column_width=True)
52
 
53
+ # Add a small delay to control the frame rate (optional)
54
+ time.sleep(0.03) # This gives roughly 30 FPS
55
 
56
+ # Release the webcam when the stream ends
57
+ cap.release()