python - 如何使用 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
解决方案
您需要定义frame_width
和frame_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))
推荐阅读
- javascript - 解构具有附加属性的项目
- php - JWT Laravel 修改密码机制
- node.js - User.findOne 返回 null
- python - ValueError:从数据帧中提取 X 和 Y 时,代码需要是类似数组的整数
- assembly - 我可以将 CPU 中的寄存器用作堆栈机器的“堆栈顶部”吗?
- node.js - FindOneAndUpdate MongoDB 与 Mongoose 更新用户的个人资料不使用电子邮件?
- pandas - 我正在尝试合并两个具有几个相同值但它给出一个空集的 DataFrame?
- javascript - 为什么删除按钮停止作用于通过函数创建的元素?| Javascript & HML
- php - 在没有数据库的情况下使用 Symfony 实体和原则映射
- python - tkinter 图像的操作:为什么在将图片放置在画布上之前对其进行操作时,它的运行速度会快 10 倍以上?