[Python] CV2 not able to identify the valid numpy arrays

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am running a Python code for the pose detection using HRNet, and the cv2 throws an error for an invalid numpy array, even though the type matched the validity criteria.

    This is giving error even though I have tested that the types of all the arrays are valid:

    center = [ 69.73415 178.39784] <class 'numpy.ndarray'>
    scale = [189.1834 252.24452] <class 'numpy.ndarray'>
    output_size = [288 384] <class 'numpy.ndarray'>
    src type = <class 'numpy.ndarray'> | src dtype = float32 | src shape = (3, 2)
    dst type = <class 'numpy.ndarray'> | dst dtype = float32 | dst shape = (3, 2)
    src shape = (3, 2) dtype = float32
    dst shape = (3, 2) dtype = float32
    ---------------------------------------------------------------------------
    error Traceback (most recent call last)
    /tmp/ipykernel_47/2195469303.py in <cell line: 0>()
    1 img = glob.glob(IMAGES_DIR + "/*.jpg")[0] # first image
    ----> 2 vis, (kps, heatmaps) = infer_image_with_model(img, teacher_w32, det_threshold=0.85)
    3 display(Image.fromarray(cv2.cvtColor(vis, cv2.COLOR_BGR2RGB)))
    4 print("People detected:", len(kps))

    /tmp/ipykernel_47/2004379265.py in infer_image_with_model(image_path, pose_model, det_threshold, kp_score_thr, save_vis, out_path)
    87
    88
    ---> 89 trans = get_affine_transform(center, scale, 0, output_size) # center & scale are np.float32 1D arrays
    90 inp_img = cv2.warpAffine(orig_rgb, trans, (model_w, model_h), flags=cv2.INTER_LINEAR)
    91 inp_tensor = hrnet_transform(inp_img).unsqueeze(0).to(device)

    /tmp/ipykernel_47/3123242988.py in get_affine_transform(center, scale, rot, output_size, shift, inv)
    57 return cv2.getAffineTransform(dst, src)
    58 else:
    ---> 59 return cv2.getAffineTransform(src, dst)

    error: OpenCV(4.12.0) :-1: error: (-5:Bad argument) in function 'getAffineTransform'
    > Overload resolution failed:
    > - src is not a numpy array, neither a scalar
    > - Expected Ptr<cv::UMat> for argument 'src'


    This was the code for the get_affine_transform:

    def get_affine_transform(center, scale, rot, output_size, shift=np.array([0, 0], dtype=np.float32), inv=0):
    if not isinstance(scale, np.ndarray) and not isinstance(scale, list):
    scale = np.array([scale, scale], dtype=np.float32)

    scale_tmp = scale * 200.0
    src_w = scale_tmp[0]
    dst_w, dst_h = output_size[0], output_size[1]

    rot_rad = np.pi * rot / 180
    src_dir = np.array([0, src_w * -0.5], np.float32)
    dst_dir = np.array([0, dst_w * -0.5], np.float32)

    sn, cs = np.sin(rot_rad), np.cos(rot_rad)
    src_dir = np.array([
    src_dir[0] * cs - src_dir[1] * sn,
    src_dir[0] * sn + src_dir[1] * cs
    ], np.float32)

    src = np.zeros((3, 2), dtype=np.float32)
    dst = np.zeros((3, 2), dtype=np.float32)

    src[0] = center + scale_tmp * shift
    src[1] = center + src_dir + scale_tmp * shift

    dx = src[1] - src[0]
    src[2] = np.array([-dx[1], dx[0]]) + src[0]

    dst[0] = [dst_w * 0.5, dst_h * 0.5]
    dst[1] = dst[0] + dst_dir
    d = dst[1] - dst[0]
    dst[2] = np.array([-d[1], d[0]]) + dst[0]

    # CRITICAL FIX
    src = np.asarray(src, dtype=np.float32)
    dst = np.asarray(dst, dtype=np.float32)

    if inv:
    return cv2.getAffineTransform(dst, src)
    else:
    return cv2.getAffineTransform(src, dst)

    Continue reading...

Compartilhe esta Página