1adrianb / 1adrianb/face-alignment

When image width == height == 49, image resizing in blazeface detection failed to calculate the proper new dimension.

Ouverte Adaptée aux débutants
#378 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Python
Étoiles
7.5k
Forks
1.4k
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

```bash
Traceback (most recent call last):
File "/workspace/face-feature/calc_mean_faces.py", line 443, in
success, std = calc_mean_face(path, use_median=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/face-feature/calc_mean_faces.py", line 318, in calc_mean_face
boxes = fa.face_detector.detect_from_image(frame_roi)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/face-feature/.venv/lib/python3.12/site-packages/face_alignment/detection/blazeface/blazeface_detector.py", line 51, in detect_from_image
bboxlist = detect(self.face_detector, image, target_size=image_size, device=self.device)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/face-feature/.venv/lib/python3.12/site-packages/face_alignment/detection/blazeface/detect.py", line 13, in detect
preds = net.predict_on_image(img)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/face-feature/.venv/lib/python3.12/site-packages/face_alignment/detection/blazeface/net_blazeface.py", line 252, in predict_on_image
return self.predict_on_batch(img.unsqueeze(0))[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/face-feature/.venv/lib/python3.12/site-packages/face_alignment/detection/blazeface/net_blazeface.py", line 280, in predict_on_batch
assert x.shape[3] == 128
^^^^^^^^^^^^^^^^^
AssertionError
```

I had checked that the resized image dimensions were miscalculated because the longer size was set to 127 when width == height == 49.
Therefore, it is better to make another if/elif branch in image_resize and resize_and_crop_image functions in the face_alignment/detection/blaceface/utils.py file.

```python
def resize_and_crop_image(image, dim):
if image.shape[0] > image.shape[1]:
img = image_resize(image, width=dim)
yshift, xshift = (image.shape[0] - image.shape[1]) // 2, 0
y_start = (img.shape[0] - img.shape[1]) // 2
y_end = y_start + dim
return img[y_start:y_end, :, :], (xshift, yshift)
elif image.shape[0] == image.shape[1]:
img = image_resize(image, width=dim, height=dim)
yshift, xshift = 0, 0
return img, (xshift, yshift)
else:
img = image_resize(image, height=dim)
yshift, xshift = 0, (image.shape[1] - image.shape[0]) // 2
x_start = (img.shape[1] - img.shape[0]) // 2
x_end = x_start + dim
return img[:, x_start:x_end, :], (xshift, yshift)

def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]

# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image

if width is not None and height is not None:
dim = (width, height)
elif width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)

# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))

# resize the image
resized = cv2.resize(image, dim, interpolation=inter)

# return the resized image
return resized
```

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

The issue is in face_alignment/detection/blazeface/utils.py, specifically in the resize_and_crop_image and image_resize functions. The bug occurs when an image has width == height == 49, leading to a miscalculated resized dimension. Start by examining the logic for handling square images and the dimension calculations. Run the failing test case with a 49x49 image to reproduce the AssertionError, then adjust the condition to ensure the longer side is set to 128 as expected.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
opencv, python, pytorch
Domaine
computer-vision
Type d'issue
Bug
Difficulté
2/5
Temps estimé
1-3 heures
Activité
Calme
Clarté
Clairement spécifiée
Accessibilité débutants
75/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.