Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -31,48 +31,48 @@ def expand_box(x, y, w, h, scale=1.5, img_shape=None):
|
|
31 |
def predict(image):
|
32 |
faces = detector.detect_faces(image)
|
33 |
if not faces:
|
34 |
-
return "No
|
35 |
|
|
|
36 |
results = []
|
37 |
-
annotated = image.copy()
|
38 |
|
39 |
-
for
|
40 |
x, y, w, h = face['box']
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
)
|
74 |
-
|
75 |
-
return "\n".join(results),
|
76 |
|
77 |
# Gradio Interface
|
78 |
interface = gr.Interface(
|
|
|
31 |
def predict(image):
|
32 |
faces = detector.detect_faces(image)
|
33 |
if not faces:
|
34 |
+
return "No face detected", image
|
35 |
|
36 |
+
output_image = image.copy()
|
37 |
results = []
|
|
|
38 |
|
39 |
+
for idx, face in enumerate(faces):
|
40 |
x, y, w, h = face['box']
|
41 |
+
|
42 |
+
# Add 20% margin while staying inside bounds
|
43 |
+
margin = 0.2
|
44 |
+
img_h, img_w = image.shape[:2]
|
45 |
+
x = max(0, int(x - w * margin))
|
46 |
+
y = max(0, int(y - h * margin))
|
47 |
+
w = int(w * (1 + 2 * margin))
|
48 |
+
h = int(h * (1 + 2 * margin))
|
49 |
+
x2 = min(img_w, x + w)
|
50 |
+
y2 = min(img_h, y + h)
|
51 |
+
|
52 |
+
face_img = image[y:y2, x:x2]
|
53 |
+
|
54 |
+
# Resize + preprocess
|
55 |
+
face_xcp = cv2.resize(face_img, (299, 299))
|
56 |
+
face_eff = cv2.resize(face_img, (224, 224))
|
57 |
+
xcp_tensor = xcp_pre(face_xcp.astype(np.float32))[np.newaxis, ...]
|
58 |
+
eff_tensor = eff_pre(face_eff.astype(np.float32))[np.newaxis, ...]
|
59 |
+
|
60 |
+
# Predictions
|
61 |
+
pred_xcp = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
62 |
+
pred_eff = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
63 |
+
avg = (pred_xcp + pred_eff) / 2
|
64 |
+
|
65 |
+
label = "Real" if avg > 0.5 else "Fake"
|
66 |
+
color = (0, 255, 0) if label == "Real" else (0, 0, 255)
|
67 |
+
|
68 |
+
# Annotate image
|
69 |
+
cv2.rectangle(output_image, (x, y), (x2, y2), color, 2)
|
70 |
+
cv2.putText(output_image, f"{label} ({avg:.2f})", (x, y - 10),
|
71 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
72 |
+
|
73 |
+
results.append(f"Face {idx+1}: {label} (Avg: {avg:.3f}, XCP: {pred_xcp:.3f}, EFF: {pred_eff:.3f})")
|
74 |
+
|
75 |
+
return "\n".join(results), output_image
|
76 |
|
77 |
# Gradio Interface
|
78 |
interface = gr.Interface(
|