Zeyadd-Mostaffa commited on
Commit
7d8ff14
·
verified ·
1 Parent(s): 626beb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -38
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 faces detected", image
35
 
 
36
  results = []
37
- annotated = image.copy()
38
 
39
- for i, face in enumerate(faces):
40
  x, y, w, h = face['box']
41
- x, y, w, h = max(0, x), max(0, y), w, h
42
- x1, y1, x2, y2 = expand_box(x, y, w, h, scale=1.6, img_shape=image.shape)
43
-
44
- face_crop = image[y1:y2, x1:x2]
45
-
46
- # Preprocess for each model
47
- xcp_img = cv2.resize(face_crop, (299, 299))
48
- eff_img = cv2.resize(face_crop, (224, 224))
49
-
50
- xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
51
- eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
52
-
53
- xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
54
- eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
55
- avg_pred = (xcp_pred + eff_pred) / 2
56
- label = "Real" if avg_pred > 0.5 else "Fake"
57
-
58
- results.append(
59
- f"Face {i+1}: {label} (Avg: {avg_pred:.3f}, XCP: {xcp_pred:.3f}, EFF: {eff_pred:.3f})"
60
- )
61
-
62
- # Draw
63
- color = (0, 255, 0) if label == "Real" else (255, 0, 0)
64
- cv2.rectangle(annotated, (x1, y1), (x2, y2), color, 2)
65
- cv2.putText(
66
- annotated,
67
- f"{label} ({avg_pred:.2f})",
68
- (x1, y1 - 10),
69
- cv2.FONT_HERSHEY_SIMPLEX,
70
- 0.6,
71
- color,
72
- 2,
73
- )
74
-
75
- return "\n".join(results), annotated
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(