1adrianb / 1adrianb/face-alignment

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

未关闭 适合新手
#378 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
7.5k
派生
1.4k
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

这个仓库没有索引到贡献指南

调研方向

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.

由索引模型根据 Issue 内容生成。

评估

技术栈
opencv, python, pytorch
领域
computer-vision
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
冷清
描述清晰度
描述清楚
新手友好度
75/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。