pushpinder06 commited on
Commit
19175ed
·
verified ·
1 Parent(s): 21bacb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -18
app.py CHANGED
@@ -1,40 +1,34 @@
 
1
  import numpy as np
2
  import cv2
3
  import gradio as gr
 
4
 
5
  # Load Haar Cascade classifier
6
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
7
-
8
  # Face Detection Function
9
- def detect_faces(image_np, slider):
10
- # Convert image to numpy array
11
- img = np.array(image_np)
12
-
13
- # Convert to grayscale
14
  gray_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
15
 
16
  # Detect faces
17
  faces = face_cascade.detectMultiScale(gray_image, scaleFactor=slider, minNeighbors=5, minSize=(30, 30))
18
 
19
- # Draw rectangles on original image
20
  for (x, y, w, h) in faces:
21
- cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
22
 
23
- return img, f"Faces detected: {len(faces)}"
24
 
25
  # Create Gradio Interface
26
  iface = gr.Interface(
27
  fn=detect_faces,
28
- inputs=[
29
- gr.Image(type="numpy", label="Upload Image"),
30
- gr.Slider(minimum=1.1, maximum=2.0, step=0.1, label="Adjust the scale factor.")
31
- ],
32
- outputs=[
33
- gr.Image(label="Detected Faces"),
34
- gr.Label(label="Face Count")
35
- ],
36
  title="Face Detection",
37
  description="Upload an image, and the model will detect faces and draw bounding boxes around them."
38
  )
39
 
40
- iface.launch()
 
1
+ You said:
2
  import numpy as np
3
  import cv2
4
  import gradio as gr
5
+ from PIL import Image
6
 
7
  # Load Haar Cascade classifier
8
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
9
+ # slider=gr.Slider(minimum=1,maximum=2,step=.1,label="Adjust the scale factor.")
10
  # Face Detection Function
11
+ def detect_faces(image_np,slider):
12
+ img=np.array(gray_image)
13
+ # Convert image to grayscale
 
 
14
  gray_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
15
 
16
  # Detect faces
17
  faces = face_cascade.detectMultiScale(gray_image, scaleFactor=slider, minNeighbors=5, minSize=(30, 30))
18
 
19
+ # Draw rectangles around faces
20
  for (x, y, w, h) in faces:
21
+ cv2.rectangle(image_np, (x, y), (x + w, y + h), (0, 255, 0), 2)
22
 
23
+ return img, len(faces)
24
 
25
  # Create Gradio Interface
26
  iface = gr.Interface(
27
  fn=detect_faces,
28
+ inputs=["image",gr.Slider(minimum=1,maximum=2,step=.1,label="Adjust the scale factor.")],
29
+ outputs=["image",gr.Label( "Face counts")],
 
 
 
 
 
 
30
  title="Face Detection",
31
  description="Upload an image, and the model will detect faces and draw bounding boxes around them."
32
  )
33
 
34
+ iface.launch()