在我的 python 代码中,我正在预处理图像并将其提供给模型进行预测。:
path = "/Users/iamreddy831/Desktop/ArchitecturalStyle_ML/FinalTests/testimage3.jpg"
styles = ["Baroque", "NeoClassical", "Gothic", "Modern", "Victorian"]
def read_image(file_path):
print("[INFO] loading and preprocessing image…")
image = load_img(file_path, target_size=(300, 300))
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image /= 255.
return image
def test_single_image(path):
styles = ["Baroque", "NeoClassical", "Gothic", "Modern", "Victorian"]
images = read_image(path)
time.sleep(.5)
bt_prediction = vgg19.predict(images)
tf.shape(bt_prediction)
preds = model.predict(bt_prediction)
for idx, styles, x in zip(range(0,7), styles, preds[0]):
print("ID: {}, Label: {} {}%".format(idx, styles, round(x*100,2) ))
print("Final Decision:")
time.sleep(.5)
for x in range(3):
print("."*(x+1))
time.sleep(.2)
class_predicted = np.argmax(model.predict(bt_prediction), axis=-1)
class_dictionary = generator_top.class_indices
inv_map = {v: k for k, v in class_dictionary.items()}
print("ID: {}, Label: {}".format(class_predicted[0], inv_map[class_predicted[0]]))
return load_img(path)
如何在网页上运行此 python 代码以预处理页面中的图像以供输入?我查看了 Tensorflow.js 并重新创建了工作流,但我认为因为它依赖于 applications.vgg19(存在于 Tensorflow 但不存在于 Tensorflow.js)我必须创建一个 python 环境来执行相同/类似的事情:
<script type="text/javascript">
async function run(){
const image = tf.browser.fromPixels(imgcanvas);
const batchedImage = languagePluginLoader.then(function () {
console.log(pyodide.runPython(`
import sys
sys.version
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import applications
tf.keras.applications.vgg19.preprocess_input(
image, data_format=None
`));
});
const MODEL_URL = 'web_model/model.json';
const model = await tf.loadGraphModel(MODEL_URL);
const result = model.predict(batchedImage);
result.print();
run();
</script>
在这种情况下,我是否正确使用了 Pyodide?尝试实时执行此操作时,我不断收到语法错误。或者有没有更简单的方法来解决这个问题?重塑相当复杂,一个依赖于卷积层的 [-1, 9, 9, 512] 数组。