(self, image_rgb, crop_factor, sort = True)
| 223 | return pred[0].boxes.xyxy.cpu().numpy() |
| 224 | |
| 225 | def detect_face(self, image_rgb, crop_factor, sort = True): |
| 226 | bboxes = self.get_face_bboxes(image_rgb) |
| 227 | w, h = get_rgb_size(image_rgb) |
| 228 | |
| 229 | print(f"w, h:{w, h}") |
| 230 | |
| 231 | cx = w / 2 |
| 232 | min_diff = w |
| 233 | best_box = None |
| 234 | for x1, y1, x2, y2 in bboxes: |
| 235 | bbox_w = x2 - x1 |
| 236 | if bbox_w < 30: continue |
| 237 | diff = abs(cx - (x1 + bbox_w / 2)) |
| 238 | if diff < min_diff: |
| 239 | best_box = [x1, y1, x2, y2] |
| 240 | print(f"diff, min_diff, best_box:{diff, min_diff, best_box}") |
| 241 | min_diff = diff |
| 242 | |
| 243 | if best_box == None: |
| 244 | print("Failed to detect face!!") |
| 245 | return [0, 0, w, h] |
| 246 | |
| 247 | x1, y1, x2, y2 = best_box |
| 248 | |
| 249 | #for x1, y1, x2, y2 in bboxes: |
| 250 | bbox_w = x2 - x1 |
| 251 | bbox_h = y2 - y1 |
| 252 | |
| 253 | crop_w = bbox_w * crop_factor |
| 254 | crop_h = bbox_h * crop_factor |
| 255 | |
| 256 | crop_w = max(crop_h, crop_w) |
| 257 | crop_h = crop_w |
| 258 | |
| 259 | kernel_x = int(x1 + bbox_w / 2) |
| 260 | kernel_y = int(y1 + bbox_h / 2) |
| 261 | |
| 262 | new_x1 = int(kernel_x - crop_w / 2) |
| 263 | new_x2 = int(kernel_x + crop_w / 2) |
| 264 | new_y1 = int(kernel_y - crop_h / 2) |
| 265 | new_y2 = int(kernel_y + crop_h / 2) |
| 266 | |
| 267 | if not sort: |
| 268 | return [int(new_x1), int(new_y1), int(new_x2), int(new_y2)] |
| 269 | |
| 270 | if new_x1 < 0: |
| 271 | new_x2 -= new_x1 |
| 272 | new_x1 = 0 |
| 273 | elif w < new_x2: |
| 274 | new_x1 -= (new_x2 - w) |
| 275 | new_x2 = w |
| 276 | if new_x1 < 0: |
| 277 | new_x2 -= new_x1 |
| 278 | new_x1 = 0 |
| 279 | |
| 280 | if new_y1 < 0: |
| 281 | new_y2 -= new_y1 |
| 282 | new_y1 = 0 |
no test coverage detected