1adrianb / 1adrianb/face-alignment

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

Open Beginner friendly
#378 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
7.5k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

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
```

Contributor guide

No contributing guide indexed for this repository

Research direction

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.

Written by the indexing model from the issue text.

Assessment

Tech stack
opencv, python, pytorch
Domain
computer-vision
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.