Skip to content

Commit 2fa4d96

Browse files
JeansBoussierladaapp
authored andcommitted
detection dataset: Adjust scale parameter of head and face detections
1 parent af28862 commit 2fa4d96

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

lada/lib/box_utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
from lada.lib import Box
1+
import random
2+
3+
from lada.lib import Box, Image
24

35
def box_overlap(box1: Box, box2: Box):
46
y1min, x1min, y1max, x1max = box1
57
y2min, x2min, y2max, x2max = box2
6-
return x1min < x2max and x2min < x1max and y1min < y2max and y2min < y1max
8+
return x1min < x2max and x2min < x1max and y1min < y2max and y2min < y1max
9+
10+
def scale_box(img: Image, box: Box, mask_scale=1.0) -> Box:
11+
img_h, img_w = img.shape[:2]
12+
s = mask_scale - 1.0
13+
t, l, b, r = box
14+
w, h = r - l + 1, b - t + 1
15+
t -= h * s
16+
b += h * s
17+
l -= w * s
18+
r += w * s
19+
t = max(0, t)
20+
b = min(img_h - 1, b)
21+
l = max(0, l)
22+
r = min(img_w - 1, r)
23+
return int(t), int(l), int(b), int(r)
24+
25+
def random_scale_box(img: Image, box: Box, scale_range=(1.0, 1.5)) -> Box:
26+
scale = random.uniform(scale_range[0], scale_range[1])
27+
return scale_box(img, box, scale)

lada/lib/face_detector.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@
44
import cv2
55
import numpy as np
66
from lada.lib import Mask, Box, Image, Detection, Detections, DETECTION_CLASSES
7-
from lada.lib import mask_utils
7+
from lada.lib import box_utils
88
from lada.centerface.centerface import CenterFace
99

10-
def scale_box(box, mask_scale=1.0) -> Box:
11-
s = mask_scale - 1.0
12-
t, l, b, r = box
13-
w, h = r - l + 1, b - t + 1
14-
t -= h * s
15-
b += h * s
16-
l -= w * s
17-
r += w * s
18-
return int(t), int(l), int(b), int(r)
19-
2010
def convert_to_boxes(dets) -> list[Box]:
2111
boxes = []
2212
for i, det in enumerate(dets):
@@ -50,17 +40,15 @@ def create_mask(frame: Image, box: Box) -> Mask:
5040

5141
return mask
5242

53-
def get_nsfw_frame(dets: list[Box], frame, random_extend_masks: bool, mask_scale: float) -> Detections | None:
43+
def get_nsfw_frame(dets: list[Box], frame: Image, random_extend_masks: bool) -> Detections | None:
5444
if len(dets) == 0:
5545
return None
5646
detections = []
5747
for box in dets:
58-
box = scale_box(box, mask_scale)
59-
mask = create_mask(frame, box)
6048

6149
if random_extend_masks:
62-
mask = mask_utils.apply_random_mask_extensions(mask)
63-
box = mask_utils.get_box(mask)
50+
box = box_utils.random_scale_box(frame, box, scale_range=(1.2, 1.5))
51+
mask = create_mask(frame, box)
6452

6553
t, l, b, r = box
6654
width, height = r - l + 1, b - t + 1
@@ -72,14 +60,13 @@ def get_nsfw_frame(dets: list[Box], frame, random_extend_masks: bool, mask_scale
7260
return Detections(frame, detections)
7361

7462
class FaceDetector:
75-
def __init__(self, model: CenterFace, random_extend_masks=False, conf=0.2, mask_scale=1.3):
63+
def __init__(self, model: CenterFace, random_extend_masks=False, conf=0.2):
7664
self.model = model
7765
self.random_extend_masks = random_extend_masks
7866
self.conf = conf
79-
self.mask_scale = mask_scale
8067

8168
def detect(self, file_path: str) -> Detections | None:
8269
image = cv2.imread(file_path, cv2.IMREAD_COLOR_RGB)
8370
dets, _ = self.model(image, threshold=self.conf)
8471
dets_boxes = convert_to_boxes(dets)
85-
return get_nsfw_frame(dets_boxes, cv2.cvtColor(image, cv2.COLOR_RGB2BGR), random_extend_masks=self.random_extend_masks, mask_scale=self.mask_scale)
72+
return get_nsfw_frame(dets_boxes, cv2.cvtColor(image, cv2.COLOR_RGB2BGR), random_extend_masks=self.random_extend_masks)

lada/lib/head_detector.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import cv2
55
import numpy as np
66
from lada.lib import Mask, Box, Image, Detections, Detection, DETECTION_CLASSES
7-
from lada.lib import mask_utils
7+
from lada.lib import box_utils
88
from lada.bpjdet.inference import inference
99

1010
def _create_mask(frame: Image, box: Box) -> Mask:
@@ -37,11 +37,9 @@ def _get_detection(dets: list[Box], frame, random_extend_masks: bool) -> Detecti
3737
return None
3838
detections = []
3939
for box in dets:
40-
mask = _create_mask(frame, box)
41-
4240
if random_extend_masks:
43-
mask = mask_utils.apply_random_mask_extensions(mask)
44-
box = mask_utils.get_box(mask)
41+
box = box_utils.random_scale_box(frame, box, scale_range=(0.9, 1.2))
42+
mask = _create_mask(frame, box)
4543

4644
t, l, b, r = box
4745
width, height = r - l + 1, b - t + 1
@@ -52,7 +50,7 @@ def _get_detection(dets: list[Box], frame, random_extend_masks: bool) -> Detecti
5250
return Detections(frame, detections)
5351

5452
class HeadDetector:
55-
def __init__(self, model, data, conf_thres, iou_thres, imgz=1024, random_extend_masks=False):
53+
def __init__(self, model, data, conf_thres, iou_thres, imgz=1536, random_extend_masks=False):
5654
self.model = model
5755
self.data = data
5856
self.random_extend_masks = random_extend_masks

0 commit comments

Comments
 (0)