【问题标题】:Issue in reshaping image in python在 python 中重塑图像的问题
【发布时间】:2020-12-01 10:16:22
【问题描述】:

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

client.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)

server.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)
   

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

【问题讨论】:

    标签: python numpy opencv image-processing reshape


    【解决方案1】:

    根据您共享的数据重新创建第二个示例时,形状看起来是 (1,12)。 (数据sn-p好像不全,请看这个)。 因此:

    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 个数组,这是图像数据所期望的。要么您在 sn-p 中发送了错误的数据,要么您正在检索错误的数据。

    【讨论】:

    • 您好,非常感谢您的帮助,非常感谢@TimVisser。我已经更新了帖子,数组的大小是(1,1000),因此我没有发布完整的数据。
    猜你喜欢
    • 2020-10-05
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2023-04-06
    • 2023-02-14
    相关资源
    最近更新 更多