likhithAIML24 commited on
Commit
a1f0fc9
1 Parent(s): 299f55c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -2,16 +2,21 @@ import cv2
2
  import gradio as gr
3
 
4
  def detect_faces(image):
 
5
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
6
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
 
 
7
  faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
 
8
  for (x, y, w, h) in faces:
9
- cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
 
10
  return image, f"Number of faces detected: {len(faces)}"
11
 
12
  iface = gr.Interface(fn=detect_faces,
13
- inputs=gr.components.Image(source="webcam", tool="editor", type="pil"),
14
  outputs=[gr.components.Image(type="pil"), gr.components.Textbox()],
15
  title="Face Detection App",
16
- description="This app detects faces in real-time from your webcam feed.")
17
  iface.launch()
 
2
  import gradio as gr
3
 
4
  def detect_faces(image):
5
+ # Convert to grayscale for detection
6
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
7
+ # Load the Haar cascade for face detection
8
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
9
+ # Detect faces
10
  faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
11
+ # Draw rectangles around each face
12
  for (x, y, w, h) in faces:
13
+ cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 3)
14
+ # Return image and the count of faces
15
  return image, f"Number of faces detected: {len(faces)}"
16
 
17
  iface = gr.Interface(fn=detect_faces,
18
+ inputs=gr.components.Image(tool="editor", type="pil"),
19
  outputs=[gr.components.Image(type="pil"), gr.components.Textbox()],
20
  title="Face Detection App",
21
+ description="This app detects faces in real-time. Upload or capture an image using your webcam.")
22
  iface.launch()