likhithAIML24 commited on
Commit
938b9ab
1 Parent(s): a1f0fc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -2,21 +2,25 @@ import cv2
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()
 
2
  import gradio as gr
3
 
4
  def detect_faces(image):
5
+ # Convert the image to grayscale for better detection
6
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
7
+
8
+ # Load the pre-trained face detection model from OpenCV
9
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
10
+
11
+ # Detect faces in the image
12
  faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
13
+
14
+ # Draw rectangles around the faces
15
  for (x, y, w, h) in faces:
16
+ cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
17
+
18
+ # Return the modified image and the number of faces detected
19
+ return image, len(faces)
20
 
21
  iface = gr.Interface(fn=detect_faces,
22
+ inputs=gr.components.Image(source="webcam", type="pil"),
23
+ outputs=[gr.components.Image(type="pil"), gr.components.Label()],
24
+ title="Face Detection",
25
+ description="This app detects faces in real-time from your webcam.")
26
  iface.launch()