MarcosRodrigo commited on
Commit
86f1f8f
·
verified ·
1 Parent(s): 9e1ebc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -1,16 +1,33 @@
1
  import gradio as gr
 
2
 
3
- # A simple function to just display the live video feed
4
- def show_live_webcam(image):
5
- return image
6
 
7
- # Create a Gradio Interface for the webcam feed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  webcam_interface = gr.Interface(
9
- fn=show_live_webcam,
10
- inputs=gr.Image(source="webcam", streaming=True),
11
  outputs="image",
12
- title="Live Webcam Feed",
13
- description="Displays the live feed from your webcam."
14
  )
15
 
16
  # Launch the Gradio app
 
1
  import gradio as gr
2
+ import cv2
3
 
4
+ # Load the pre-trained Haar Cascade classifier for face detection
5
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
 
6
 
7
+ def detect_faces(image):
8
+ # Convert RGB image to OpenCV BGR format
9
+ img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
10
+
11
+ # Convert to grayscale for face detection
12
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13
+
14
+ # Perform face detection
15
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
16
+
17
+ # Draw rectangles around detected faces
18
+ for (x, y, w, h) in faces:
19
+ cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
20
+
21
+ # Convert back to RGB for display
22
+ return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
23
+
24
+ # Use gr.Video for webcam feed instead of gr.Image
25
  webcam_interface = gr.Interface(
26
+ fn=detect_faces,
27
+ inputs=gr.Video(source="webcam", streaming=True),
28
  outputs="image",
29
+ title="Live Webcam Face Detection",
30
+ description="Displays the live feed from your webcam and detects faces in real-time."
31
  )
32
 
33
  # Launch the Gradio app