Jus1012 commited on
Commit
ad12b0a
·
verified ·
1 Parent(s): 8e48551

Add face-detection filter to catch selfies/portraits scoring falsely confident on Fire/Flood by color/lighting alone

Browse files
Files changed (1) hide show
  1. api.py +34 -1
api.py CHANGED
@@ -13,6 +13,7 @@ import json
13
  import os
14
  import re
15
  import sys
 
16
  import torch
17
  import httpx
18
  import numpy as np
@@ -103,6 +104,27 @@ else:
103
  phase2_model = None
104
  print(f"⚠️ Phase2 model not found at {PHASE2_MODEL_PATH} — classification will skip.")
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  PHASE2_TO_REPORT_CATEGORY = {
107
  "EARTHQUAKE": "Earthquake",
108
  "FIRE": "Fire",
@@ -317,6 +339,14 @@ def run_image_verification(image_bytes: bytes) -> dict:
317
  # detector in this project.
318
  is_valid = disaster_confidence >= DISASTER_CONFIDENCE_THRESHOLD
319
 
 
 
 
 
 
 
 
 
320
  return {
321
  "is_valid_report_photo": is_valid,
322
  "valid_probability": disaster_confidence,
@@ -325,6 +355,7 @@ def run_image_verification(image_bytes: bytes) -> dict:
325
  # useful to a human reviewer than hiding it outright.
326
  "detected_disaster_category": PHASE2_TO_REPORT_CATEGORY[disaster_label],
327
  "disaster_confidence": disaster_confidence,
 
328
  "earthquake_probability": round(float(disaster_probs[0]) * 100, 2),
329
  "fire_probability": round(float(disaster_probs[1]) * 100, 2),
330
  "flood_probability": round(float(disaster_probs[2]) * 100, 2),
@@ -518,7 +549,9 @@ async def verify_image(
518
  photo_category = verification["detected_disaster_category"]
519
  matches = image_matches_report(photo_category, reported_category)
520
 
521
- if not verification["is_valid_report_photo"]:
 
 
522
  verdict = "⚠️ Photo doesn't look like a disaster report photo. Needs manual review."
523
  elif photo_category is None:
524
  verdict = "Photo looks valid, but the classification model isn't loaded yet — nothing to compare."
 
13
  import os
14
  import re
15
  import sys
16
+ import cv2
17
  import torch
18
  import httpx
19
  import numpy as np
 
104
  phase2_model = None
105
  print(f"⚠️ Phase2 model not found at {PHASE2_MODEL_PATH} — classification will skip.")
106
 
107
+ # Pretrained face detector (ships with opencv, no training/download needed).
108
+ # Used as a sanity filter: disaster evidence photos essentially never have a
109
+ # close-up human face as the dominant subject, so a selfie or portrait can
110
+ # be caught this way even when the 4-class model's color/texture match
111
+ # happens to score confidently (e.g. warm lighting resembling "Fire").
112
+ FACE_CASCADE = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
113
+ # A detected face covering at least this fraction of the frame counts as
114
+ # "dominant" — small background faces (e.g. bystanders in a real disaster
115
+ # photo) shouldn't trigger this.
116
+ DOMINANT_FACE_AREA_RATIO = 0.15
117
+
118
+ def has_dominant_face(image: Image.Image) -> bool:
119
+ cv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
120
+ gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
121
+ faces = FACE_CASCADE.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(60, 60))
122
+ if len(faces) == 0:
123
+ return False
124
+ image_area = image.width * image.height
125
+ largest_face_area = max(w * h for (_, _, w, h) in faces)
126
+ return (largest_face_area / image_area) >= DOMINANT_FACE_AREA_RATIO
127
+
128
  PHASE2_TO_REPORT_CATEGORY = {
129
  "EARTHQUAKE": "Earthquake",
130
  "FIRE": "Fire",
 
339
  # detector in this project.
340
  is_valid = disaster_confidence >= DISASTER_CONFIDENCE_THRESHOLD
341
 
342
+ # Sanity override: a selfie/portrait can still score confidently for
343
+ # Fire/Flood on color and lighting alone (e.g. warm indoor lighting).
344
+ # A dominant face means this almost certainly isn't disaster evidence,
345
+ # regardless of what the 4-class model thinks.
346
+ dominant_face = has_dominant_face(image)
347
+ if dominant_face:
348
+ is_valid = False
349
+
350
  return {
351
  "is_valid_report_photo": is_valid,
352
  "valid_probability": disaster_confidence,
 
355
  # useful to a human reviewer than hiding it outright.
356
  "detected_disaster_category": PHASE2_TO_REPORT_CATEGORY[disaster_label],
357
  "disaster_confidence": disaster_confidence,
358
+ "dominant_face_detected": dominant_face,
359
  "earthquake_probability": round(float(disaster_probs[0]) * 100, 2),
360
  "fire_probability": round(float(disaster_probs[1]) * 100, 2),
361
  "flood_probability": round(float(disaster_probs[2]) * 100, 2),
 
549
  photo_category = verification["detected_disaster_category"]
550
  matches = image_matches_report(photo_category, reported_category)
551
 
552
+ if verification.get("dominant_face_detected"):
553
+ verdict = "⚠️ Photo looks like a personal/selfie photo rather than disaster evidence. Needs manual review."
554
+ elif not verification["is_valid_report_photo"]:
555
  verdict = "⚠️ Photo doesn't look like a disaster report photo. Needs manual review."
556
  elif photo_category is None:
557
  verdict = "Photo looks valid, but the classification model isn't loaded yet — nothing to compare."