Try expanding the cropped image
Browse files- face_detection.py +32 -5
face_detection.py
CHANGED
|
@@ -14,16 +14,43 @@ def getCroppedImages(image: Image.Image, cap = -1):
|
|
| 14 |
face_locations_n = []
|
| 15 |
outputs = []
|
| 16 |
num = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
for f in face_locations:
|
| 18 |
-
|
| 19 |
-
# when there should only be one
|
| 20 |
-
if(cap != -1 and num >= cap):
|
| 21 |
break
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
outputs.append(image.crop(c))
|
| 24 |
face_locations_n.append(c)
|
| 25 |
num += 1
|
| 26 |
-
|
| 27 |
return face_locations_n, outputs
|
| 28 |
|
| 29 |
|
|
|
|
| 14 |
face_locations_n = []
|
| 15 |
outputs = []
|
| 16 |
num = 0
|
| 17 |
+
# for f in face_locations:
|
| 18 |
+
# # allow for capping the number of faces detected, helps prevent multiple faces being detected
|
| 19 |
+
# # when there should only be one
|
| 20 |
+
# if(cap != -1 and num >= cap):
|
| 21 |
+
# break
|
| 22 |
+
# c = (f[3], f[0], f[1], f[2])
|
| 23 |
+
# outputs.append(image.crop(c))
|
| 24 |
+
# face_locations_n.append(c)
|
| 25 |
+
# num += 1
|
| 26 |
+
|
| 27 |
+
# return face_locations_n, outputs
|
| 28 |
+
width, height = image.size # Get image dimensions
|
| 29 |
for f in face_locations:
|
| 30 |
+
if cap != -1 and num >= cap:
|
|
|
|
|
|
|
| 31 |
break
|
| 32 |
+
# Original coordinates: (top, right, bottom, left)
|
| 33 |
+
top, right, bottom, left = f
|
| 34 |
+
# Center of the face rectangle
|
| 35 |
+
center_x = (left + right) / 2
|
| 36 |
+
center_y = (top + bottom) / 2
|
| 37 |
+
# Expanded dimensions
|
| 38 |
+
box_width = (right - left) * 1.3
|
| 39 |
+
box_height = (bottom - top) * 1.3
|
| 40 |
+
new_left = int(center_x - box_width / 2)
|
| 41 |
+
new_right = int(center_x + box_width / 2)
|
| 42 |
+
new_top = int(center_y - box_height / 2)
|
| 43 |
+
new_bottom = int(center_y + box_height / 2)
|
| 44 |
+
# Clamp to image boundaries
|
| 45 |
+
new_left = max(0, new_left)
|
| 46 |
+
new_right = min(width, new_right)
|
| 47 |
+
new_top = max(0, new_top)
|
| 48 |
+
new_bottom = min(height, new_bottom)
|
| 49 |
+
# Crop the image with the new coordinates
|
| 50 |
+
c = (new_left, new_top, new_right, new_bottom)
|
| 51 |
outputs.append(image.crop(c))
|
| 52 |
face_locations_n.append(c)
|
| 53 |
num += 1
|
|
|
|
| 54 |
return face_locations_n, outputs
|
| 55 |
|
| 56 |
|