[英]Python - ValueError: could not broadcast input array from shape (5) into shape (2)
[英]ValueError: could not broadcast input array from shape (same shape)
我在從形狀廣播輸入數組時遇到錯誤。 通常,這似乎是由於將維度為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
以下是print語句的輸出,以便更好地理解上述代碼。
'overlay_img.shape: ' , (365, 365, 3),
'overlay_glasses.shape: ', ( 34, 99, 3)
'x: ', 623.26, 'y: ', 265.9
'h: ', 34, 'w: ', 99
較大的代碼段:
[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
您的x
超出了范圍,無法為您提供的overlay_img
創建子圖像。 圖像的尺寸為(365,365,3),但您提供的x
為623, x+w
為722。這將創建一個空的子圖像,該子圖像無法用overlay_glasses
的內容填充。 顯然, x
坐標有問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.