Simrandhiman commited on
Commit
31beb3f
·
verified ·
1 Parent(s): 19fd2a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+ # Load the Haar Cascade model
6
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
7
+
8
+ def detect_faces(image):
9
+ # Convert image (PIL -> NumPy)
10
+ img = np.array(image)
11
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
12
+
13
+ # Detect faces
14
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
15
+
16
+ # Draw rectangles around detected faces
17
+ for (x, y, w, h) in faces:
18
+ cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3)
19
+
20
+ return img, f"✅ Faces Detected: {len(faces)}" if len(faces) > 0 else "😕 No Faces Detected"
21
+
22
+ # Build Gradio Interface
23
+ title = "🧠 Face Detection App"
24
+ description = """
25
+ Upload an image to detect faces automatically using OpenCV Haar Cascade.
26
+ Works with multiple faces and outputs an annotated image!
27
+ """
28
+
29
+ iface = gr.Interface(
30
+ fn=detect_faces,
31
+ inputs=gr.Image(type="pil", label="Upload Your Image"),
32
+ outputs=[gr.Image(label="Detected Faces"), gr.Textbox(label="Result")],
33
+ title=title,
34
+ description=description,
35
+ theme="soft", # You can try "gradio/soft", "gradio/dark", "gradio/base", etc.
36
+ examples=[
37
+ ["https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cat.png"],
38
+ ]
39
+ )
40
+
41
+ iface.launch()