Akash810419 commited on
Commit
8d798f7
·
verified ·
1 Parent(s): 59180bd

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
- # If model doesn't exist locally, download it
26
- if not os.path.exists(model_path):
27
- print(f"Downloading YOLOv8 face model to {model_path}...")
28
- try:
29
- # Download from a known source
30
- model_url = "https://github.com/akanametov/yolov8-face/releases/download/v0.0.0/yolov8n-face.pt"
31
- urlretrieve(model_url, model_path)
32
- print("Download completed successfully.")
33
- except Exception as e:
34
- print(f"Download failed: {e}")
35
- print("Please download the model manually from the link above.")
36
- raise
37
-
38
- return YOLO(model_path)
39
-
40
- def detect_faces(self, image, conf_threshold=None):
41
- """
42
- Detect faces in an image and return bounding boxes WITH confidence scores.
43
-
44
- :param image: input image (numpy array, BGR format)
45
- :param conf_threshold: confidence threshold (uses self.conf if None)
46
- :return: list of tuples (x1, y1, x2, y2, confidence)
47
- """
48
- if conf_threshold is None:
49
- conf_threshold = self.conf
50
-
51
- # Run prediction
52
- results = self.model.predict(image, conf=conf_threshold, verbose=False)
53
-
54
- detections = []
55
- for result in results:
56
- boxes = result.boxes
57
- for box in boxes:
58
- # Get bounding box coordinates
59
- x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
60
-
61
- # Get confidence score
62
- confidence = float(box.conf[0].cpu().numpy())
63
-
64
- # Return as tuple with 5 values: (x1, y1, x2, y2, conf)
65
- detections.append((
66
- float(x1),
67
- float(y1),
68
- float(x2),
69
- float(y2),
70
- confidence
71
- ))
72
-
73
- return detections
74
-
75
- def __call__(self, image, conf_threshold=None):
76
- """
77
- Make the class callable for convenience.
78
- """
79
- return self.detect_faces(image, conf_threshold)
 
 
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)