Gagan0141 commited on
Commit
1d20984
·
verified ·
1 Parent(s): 06413fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
+ import gradio as gr
5
+
6
+ def detect_faces(image):
7
+ image_np = np.array(image)
8
+ gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
9
+ face_cascade = cv2.CascadeClassifier(
10
+ cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
11
+ )
12
+
13
+ scale_factors_found = [] # ✅ store scale factors that detect 14 faces
14
+
15
+ # Test scale factors from 1.01 to 1.20
16
+ for temp in np.arange(1.01, 1.21, 0.01):
17
+ faces = face_cascade.detectMultiScale(
18
+ gray_image,
19
+ scaleFactor=temp,
20
+ minNeighbors=5,
21
+ minSize=(20, 20)
22
+ )
23
+ if len(faces) == 14:
24
+ scale_factors_found.append(round(temp, 2))
25
+
26
+ # Draw faces using the last successful scale factor (if any)
27
+ if scale_factors_found:
28
+ best_scale = scale_factors_found[-1]
29
+ faces = face_cascade.detectMultiScale(
30
+ gray_image,
31
+ scaleFactor=best_scale,
32
+ minNeighbors=5,
33
+ minSize=(20, 20)
34
+ )
35
+ for (x, y, w, h) in faces:
36
+ cv2.rectangle(image_np, (x, y), (x + w, y + h), (0, 255, 0), 2)
37
+ msg = (
38
+ f"✅ Found {len(scale_factors_found)} scale factor(s) that detect 14 faces:\n"
39
+ f"{scale_factors_found}\n\n"
40
+ f"Image displayed uses the last one: {best_scale}"
41
+ )
42
+ else:
43
+ msg = "❌ No scale factor between 1.01 and 1.20 detected exactly 14 faces."
44
+
45
+ return image_np, msg
46
+
47
+ # --- Gradio Interface ---
48
+ iface = gr.Interface(
49
+ fn=detect_faces,
50
+ inputs=gr.Image(type="pil"),
51
+ outputs=["image", "text"],
52
+ title="Dynamic Face Detection App",
53
+ description="Tests multiple scale factors (1.01–1.20) and shows all that detect exactly 14 faces."
54
+ )
55
+
56
+ iface.launch(share=True)