首页 > 解决方案 > 如何使用 OpenCV 获取图像的 frame_width?

问题描述

我尝试使用以下代码获取图像的 frame_width:

    """Predict the gender of the faces showing in the image"""
    # Read Input Image
    img = cv2.imread(imag)
    # resize the image, uncomment if you want to resize the image
    img = cv2.resize(img, (frame_width, frame_height))
    # Take a copy of the initial image and resize it
    frame = img.copy()
    print(frame.shape[1])
    if frame.shape[1] > frame_width:
        frame = image_resize(frame, width=frame_width)
    # predict the faces
    faces = get_faces(frame)

按照我的主要方法,我用图像调用该方法:

if __name__ == '__main__':
    predict_gender("/Users/$$$/Downloads/test.jpg")

我已经尝试导入 tkinter,因为我认为框架需要 tkinter,但它也无法正常工作。这是我当前的错误:

<ipython-input-10-2d047afa91e4> in predict_gender(imag)
      4     img = cv2.imread(imag)
      5     # resize the image, uncomment if you want to resize the image
----> 6     img = cv2.resize(img, (frame_width, frame_height))
      7     # Take a copy of the initial image and resize it
      8     frame = img.copy()

NameError: name 'frame_width' is not defined

标签: pythonopencv

解决方案


您需要定义frame_widthframe_height

img = cv2.imread(imag)
frame_height, frame_width, _ = img.shape
# resize the image, uncomment if you want to resize the image
img = cv2.resize(img, (frame_width, frame_height))

推荐阅读