Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -24,31 +24,33 @@ def extract_face(image):
|
|
24 |
return None
|
25 |
x, y, w, h = faces[0]['box']
|
26 |
x, y = max(0, x), max(0, y)
|
27 |
-
|
28 |
-
return face
|
29 |
|
30 |
def predict(image):
|
31 |
face = extract_face(image)
|
32 |
if face is None:
|
33 |
return "No face detected", None
|
34 |
|
35 |
-
#
|
36 |
xcp_img = cv2.resize(face, (299, 299))
|
37 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
38 |
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
39 |
|
40 |
-
#
|
41 |
eff_img = cv2.resize(face, (224, 224))
|
42 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
43 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
44 |
|
45 |
-
# Ensemble
|
46 |
avg_pred = (xcp_pred + eff_pred) / 2
|
47 |
-
|
48 |
-
# ✅ Important fix: if label "real" = 1, fake = 0, prediction > 0.5 = real
|
49 |
label = "Real" if avg_pred > 0.5 else "Fake"
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
interface = gr.Interface(
|
54 |
fn=predict,
|
|
|
24 |
return None
|
25 |
x, y, w, h = faces[0]['box']
|
26 |
x, y = max(0, x), max(0, y)
|
27 |
+
return image[y:y+h, x:x+w]
|
|
|
28 |
|
29 |
def predict(image):
|
30 |
face = extract_face(image)
|
31 |
if face is None:
|
32 |
return "No face detected", None
|
33 |
|
34 |
+
# Xception
|
35 |
xcp_img = cv2.resize(face, (299, 299))
|
36 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
37 |
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
38 |
|
39 |
+
# EfficientNet
|
40 |
eff_img = cv2.resize(face, (224, 224))
|
41 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
42 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
43 |
|
44 |
+
# Ensemble
|
45 |
avg_pred = (xcp_pred + eff_pred) / 2
|
|
|
|
|
46 |
label = "Real" if avg_pred > 0.5 else "Fake"
|
47 |
|
48 |
+
# Log probabilities
|
49 |
+
print(f"Xception: {xcp_pred:.4f}, EfficientNetB4: {eff_pred:.4f}, Ensemble Avg: {avg_pred:.4f}")
|
50 |
+
|
51 |
+
# Return label with confidence
|
52 |
+
result = f"{label} (Avg: {avg_pred:.3f}, XCP: {xcp_pred:.3f}, EFF: {eff_pred:.3f})"
|
53 |
+
return result, face
|
54 |
|
55 |
interface = gr.Interface(
|
56 |
fn=predict,
|