Update social_moderation/detectors/yolov8_face.py
Browse files
social_moderation/detectors/yolov8_face.py
CHANGED
|
@@ -1,79 +1,80 @@
|
|
| 1 |
-
# detectors/yolov8_face.py
|
| 2 |
-
|
| 3 |
-
from ultralytics import YOLO
|
| 4 |
-
import cv2
|
| 5 |
-
import os
|
| 6 |
-
from urllib.request import urlretrieve
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class YOLOv8Face:
|
| 10 |
-
def __init__(self, conf=0.5, model_name="yolov8n-face.pt"):
|
| 11 |
-
"""
|
| 12 |
-
Enhanced wrapper for YOLOv8 face detection with auto-download.
|
| 13 |
-
|
| 14 |
-
:param conf: default confidence threshold
|
| 15 |
-
:param model_name: name of YOLOv8 face model
|
| 16 |
-
"""
|
| 17 |
-
self.model_name = model_name
|
| 18 |
-
self.conf = conf
|
| 19 |
-
self.model = self._load_model()
|
| 20 |
-
|
| 21 |
-
def _load_model(self):
|
| 22 |
-
"""Load model, downloading if necessary"""
|
| 23 |
-
model_path = self.model_name
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
print("
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
:param
|
| 46 |
-
:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
confidence
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
float(
|
| 68 |
-
float(
|
| 69 |
-
float(
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
| 1 |
+
# detectors/yolov8_face.py
|
| 2 |
+
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
from urllib.request import urlretrieve
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class YOLOv8Face:
|
| 10 |
+
def __init__(self, conf=0.5, model_name="yolov8n-face.pt"):
|
| 11 |
+
"""
|
| 12 |
+
Enhanced wrapper for YOLOv8 face detection with auto-download.
|
| 13 |
+
|
| 14 |
+
:param conf: default confidence threshold
|
| 15 |
+
:param model_name: name of YOLOv8 face model
|
| 16 |
+
"""
|
| 17 |
+
self.model_name = model_name
|
| 18 |
+
self.conf = conf
|
| 19 |
+
self.model = self._load_model()
|
| 20 |
+
|
| 21 |
+
def _load_model(self):
|
| 22 |
+
"""Load model, downloading if necessary"""
|
| 23 |
+
model_path = f"/tmp/{self.model_name}"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# If model doesn't exist locally, download it
|
| 27 |
+
if not os.path.exists(model_path):
|
| 28 |
+
print(f"Downloading YOLOv8 face model to {model_path}...")
|
| 29 |
+
try:
|
| 30 |
+
# Download from a known source
|
| 31 |
+
model_url = "https://github.com/akanametov/yolov8-face/releases/download/v0.0.0/yolov8n-face.pt"
|
| 32 |
+
urlretrieve(model_url, model_path)
|
| 33 |
+
print("Download completed successfully.")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Download failed: {e}")
|
| 36 |
+
print("Please download the model manually from the link above.")
|
| 37 |
+
raise
|
| 38 |
+
|
| 39 |
+
return YOLO(model_path)
|
| 40 |
+
|
| 41 |
+
def detect_faces(self, image, conf_threshold=None):
|
| 42 |
+
"""
|
| 43 |
+
Detect faces in an image and return bounding boxes WITH confidence scores.
|
| 44 |
+
|
| 45 |
+
:param image: input image (numpy array, BGR format)
|
| 46 |
+
:param conf_threshold: confidence threshold (uses self.conf if None)
|
| 47 |
+
:return: list of tuples (x1, y1, x2, y2, confidence)
|
| 48 |
+
"""
|
| 49 |
+
if conf_threshold is None:
|
| 50 |
+
conf_threshold = self.conf
|
| 51 |
+
|
| 52 |
+
# Run prediction
|
| 53 |
+
results = self.model.predict(image, conf=conf_threshold, verbose=False)
|
| 54 |
+
|
| 55 |
+
detections = []
|
| 56 |
+
for result in results:
|
| 57 |
+
boxes = result.boxes
|
| 58 |
+
for box in boxes:
|
| 59 |
+
# Get bounding box coordinates
|
| 60 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 61 |
+
|
| 62 |
+
# Get confidence score
|
| 63 |
+
confidence = float(box.conf[0].cpu().numpy())
|
| 64 |
+
|
| 65 |
+
# Return as tuple with 5 values: (x1, y1, x2, y2, conf)
|
| 66 |
+
detections.append((
|
| 67 |
+
float(x1),
|
| 68 |
+
float(y1),
|
| 69 |
+
float(x2),
|
| 70 |
+
float(y2),
|
| 71 |
+
confidence
|
| 72 |
+
))
|
| 73 |
+
|
| 74 |
+
return detections
|
| 75 |
+
|
| 76 |
+
def __call__(self, image, conf_threshold=None):
|
| 77 |
+
"""
|
| 78 |
+
Make the class callable for convenience.
|
| 79 |
+
"""
|
| 80 |
+
return self.detect_faces(image, conf_threshold)
|