首页 > 解决方案 > 在 python 中重塑图像的问题

问题描述

我有以下代码在服务器端检索元组(字符串,数组),但是当我运行代码时,我检索到错误。

客户端.py

image_name, image = footage_socket.recv_jpg()
image = cv2.imdecode( np.frombuffer( image, dtype='uint8' ), -1 )
image = img_to_array(image)
image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2]) )
preds = model.predict( preprocess_input( image ) )
print(preds.shape) #(1,1000)
dest_socket.send_image('test',preds)

服务器.py

def main():  
   while True:
        data=socket.recv_image()
        image=tuple(x for x in data if x != 'test' )
        #npimg = np.fromstring( image, dtype=np.uint8 )
        image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2]) )

我正在检索以下错误

image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2]) )
AttributeError: 'tuple' object has no attribute 'reshape'

我还使用下面的代码将元组更改为数组我仍在检索错误但另一种错误类型

 image_to_array=np.array(image)
 image = image.reshape( (1, image_to_array.shape[0], image_to_array.shape[1], 
 image_to_array.shape[2]) )

我从客户端检索的数据格式

(array([[1.47521848e-06, 2.06672325e-06, 1.44596870e-05, 1.64947978e-05,
2.81127559e-05, 3.47975970e-06, 1.05807794e-05, 4.30030159e-05,
5.65078590e-05, 2.27573415e-04, 7.15208662e-05, 2.86311624e-05)
   

谢谢,非常感谢您的帮助。

标签: pythonnumpyopencvimage-processingreshape

解决方案


根据您共享的数据重新创建第二个示例时,看起来具有形状 (1,12)。(数据片段似乎不完整,请看一下)。所以:

image_to_array.shape[0] = 1
image_to_array.shape[1] = 12
image_to_array.shape[2] does not exist and therefore gives you the error. 

您应该仔细确定从客户端检索的数据及其格式。在这种情况下,我看不到 3 个数组,这是图像数据所期望的。您在片段中发送了错误的数据,或者您正在检索错误的数据。


推荐阅读