首页 > 解决方案 > 如何在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

标签: pythonmatlabkerasprediction

解决方案


问题通过添加'python. 所以代码变成了

system('python test_model.py');

只是它。


推荐阅读