python - 如何在matlab中使用python进行模型预测
问题描述
这是 Python 代码,它在Python中运行良好:
import keras
from keras.models import load_model
from keras.preprocessing import image
import json
import numpy as np
filepath = 'model2.h5'
test_img = 'falling14145853.png'
model = keras.models.load_model(filepath)
img = image.load_img(test_img, target_size=(150, 150))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
images = np.vstack([x])
classes = model.predict(images, batch_size = 10)
_mapping = ["falling", "sitting", "standing"]
a = np.concatenate(classes, axis=None).astype(int).tolist()
idx = np.argmax(a[0])
prediction = _mapping[idx]
response_json = {"Prediction" : prediction}
data_in_json = json.dumps(response_json)
with open("prediction.json", "w") as out:
out.write(data_in_json)
这是在Matlab中:
system('test_model.py'); %doing prediction
这是 Matlab 输出,我尝试运行 python 以使用 CV 进行捕获,并且在 Matlab 中运行良好,但是当我尝试运行包含 keras 的 python 代码时,它显示此错误:
回溯(最后一次调用):文件“E:\angie\test_model.py”,第 1 行,在 import keras ImportError: No module named keras
解决方案
问题通过添加'python. 所以代码变成了
system('python test_model.py');
只是它。
推荐阅读
- javascript - 动态加载“materializecss”时波纹效果不起作用
- microsoft-teams - 作为电源自动化解决方案的一部分,为新团队创建频道
- python - 访问 Python/Kivy 动态屏幕类
- java - 需要使用java将所有文件从一个文件夹复制到另一个文件夹
- css - 在长包装词之前防止换行
- google-cloud-platform - 谷歌云函数永久链接
- keycloak - 有没有办法为每个领域定义一个自定义注册 URL?
- apache-spark - 如何摆脱这个错误不匹配的输入'line'期待
spark-sql 中的(第 1 行,第 17 行) - swift - 如何在 Amplify DataStore 中将可选值保存为 1:1 关系
- java - geeksforgeeks 问题上的螺旋矩阵遍历