python - 在 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)
谢谢,非常感谢您的帮助。
解决方案
根据您共享的数据重新创建第二个示例时,看起来具有形状 (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 个数组,这是图像数据所期望的。您在片段中发送了错误的数据,或者您正在检索错误的数据。
推荐阅读
- ruby - Ruby 块方法中变量的使用
- docusignapi - 在复合模板上组合签名者任务
- javascript - 在 CSS 中添加篮子图标数量的篮子中的产品
- javascript - 在 JavaScript 中的 SQL 查询中获取当前日期
- sql - Flink Sql match_recognize 返回不同的时间(不是作为输入事件时间)
- git - 从配置中的特定主机进行 git-submodule 更新
- python-3.x - 重定向 Django 不工作且不重定向
- javascript - 无法通过 react jsx 传递状态值。出现错误 - 意外令牌:'this'
- python - 为什么在使用 readlines() 读取文件时出现 lis 索引超出范围错误?
- angular - Angular 9 ng build --prod 中的“超出最大调用堆栈大小”用于库