Python ValueError:无法从形状(相同形状)广播输入数组
我从shape广播输入数组时出错。通常,这似乎是由于将某个维度数组Python ValueError:无法从形状(相同形状)广播输入数组,python,Python,我从shape广播输入数组时出错。通常,这似乎是由于将某个维度数组p转换为某个维度数组:p+1或p-1。然而,我的输入和输出的形状似乎是相同的:3维。所以我的问题是,我做错了什么?我该如何着手解决这个问题 ValueErrorTraceback (most recent call last) <ipython-input-21-e821b7e8e0de> in <module>() 108 "overlay_glasses.sh
p
转换为某个维度数组:p+1
或p-1
。然而,我的输入和输出的形状似乎是相同的:3维。所以我的问题是,我做错了什么?我该如何着手解决这个问题
ValueErrorTraceback (most recent call last)
<ipython-input-21-e821b7e8e0de> in <module>()
108 "overlay_glasses.shape: ", overlay_glasses.shape)
109
--> 110 overlay_img[int(y):int(y+h),int(x):int(x+w)] = overlay_glasses
111
较大的代码段:
[omitted code here]
if len(centers)>0:
# change the given value of 2.15 according to the size of the detected face
glasses_width = 2.16*abs(centers[1][0]-centers[0][0])
overlay_img = np.ones(shape = roi_color.shape,
dtype = np.uint8)*255
h,w = glass_img.shape[:2]
scaling_factor = glasses_width/w
overlay_glasses = cv2.resize(src = glass_img,
dsize = None,
fx = scaling_factor, # scale factor along x-axis; when it equals 0, it is computed as \texttt{(double)dsize.width/src.cols}
fy = scaling_factor, # scale factor along y-axis
interpolation = cv2.INTER_AREA) # INTER_AREA: resampling using pixel area relation. It may be a preferred method for image decimation,
x = centers[0][0] if centers[0][0] < centers[1][0] else centers[1][0]
# The x and y variables below depend upon the size of the detected face.
x -= 0.26*overlay_glasses.shape[1]
y += 0.85*overlay_glasses.shape[0]
print("x: ", x,
"y: ", y)
#slice the height, width of the overlay image.
h, w = overlay_glasses.shape[:2]
print("h: ", h,
"w: ", w)
print("overlay_img.shape: ", overlay_img.shape,
"overlay_glasses.shape: ", overlay_glasses.shape)
overlay_img[int(y):int(y+h),int(x):int(x+w)] = overlay_glasses # this is the offending line of code
[此处省略代码]
如果len(中心)>0:
#根据检测到的面部大小更改给定值2.15
玻璃宽度=2.16*abs(中心[1][0]-中心[0][0])
叠加图像=np.ones(形状=roi\U颜色.shape,
数据类型=np.uint8)*255
h、 w=玻璃形状[:2]
比例系数=玻璃宽度/w
覆盖玻璃=cv2。调整大小(src=玻璃),
dsize=无,
fx=缩放因子,#沿x轴的缩放因子;当它等于0时,计算为\texttt{(double)dsize.width/src.cols}
fy=缩放因子,#沿y轴的缩放因子
插值=cv2。INTER_区域)#INTER_区域:使用像素区域关系重新采样。这可能是图像抽取的首选方法,
x=中心[0][0]如果中心[0][0]<中心[1][0]其他中心[1][0]
#下面的x和y变量取决于检测到的面的大小。
x-=0.26*overlay_眼镜。形状[1]
y+=0.85*叠加玻璃。形状[0]
打印(“x:”,x,
y:,y)
#切片覆盖图像的高度、宽度。
h、 w=覆盖玻璃。形状[:2]
打印(“h:”,h,
“w:,w)
打印(“叠加图像形状:”,叠加图像形状,
“叠加眼镜。形状:”,叠加眼镜。形状)
覆盖层img[int(y):int(y+h),int(x):int(x+w)]=覆盖层#眼镜#这是有问题的代码行
您的x
超出范围,无法为您提供的overlay\u img
创建子图像。图像的尺寸为(365,365,3),但您提供的x
为623,而x+w
为722。这将创建一个空的子图像,不能用覆盖眼镜的内容填充该子图像。显然,x
坐标有问题。您的x
超出了为您提供的叠加图像创建子图像的范围。图像的尺寸为(365,365,3),但您提供的x
为623,而x+w
为722。这将创建一个空的子图像,不能用覆盖眼镜的内容填充该子图像。显然,x
坐标有问题。试着打印overlay\u img[int(y):int(y+h),int(x):int(x+w)]
。你得到的确切错误是什么?你不能用(34,0,3)
填充(34,99,3)
。试着打印overlay\u img[int(y):int(y+h),int(x):int(x+w)]
。您得到的确切错误是什么?您不能用(34,0,3)
填充(34,99,3)
。
[omitted code here]
if len(centers)>0:
# change the given value of 2.15 according to the size of the detected face
glasses_width = 2.16*abs(centers[1][0]-centers[0][0])
overlay_img = np.ones(shape = roi_color.shape,
dtype = np.uint8)*255
h,w = glass_img.shape[:2]
scaling_factor = glasses_width/w
overlay_glasses = cv2.resize(src = glass_img,
dsize = None,
fx = scaling_factor, # scale factor along x-axis; when it equals 0, it is computed as \texttt{(double)dsize.width/src.cols}
fy = scaling_factor, # scale factor along y-axis
interpolation = cv2.INTER_AREA) # INTER_AREA: resampling using pixel area relation. It may be a preferred method for image decimation,
x = centers[0][0] if centers[0][0] < centers[1][0] else centers[1][0]
# The x and y variables below depend upon the size of the detected face.
x -= 0.26*overlay_glasses.shape[1]
y += 0.85*overlay_glasses.shape[0]
print("x: ", x,
"y: ", y)
#slice the height, width of the overlay image.
h, w = overlay_glasses.shape[:2]
print("h: ", h,
"w: ", w)
print("overlay_img.shape: ", overlay_img.shape,
"overlay_glasses.shape: ", overlay_glasses.shape)
overlay_img[int(y):int(y+h),int(x):int(x+w)] = overlay_glasses # this is the offending line of code