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

Author: keeyoungkim-emologyCreated May 26, 2026Updated May 26, 2026
bash
Traceback (most recent call last):                                                                                                                                                                                                                                                                                                                                                                                                  
  File "/workspace/face-feature/calc_mean_faces.py", line 443, in <module>                                                                                                                                                                                                                                                                                                                                                          
    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