In my python code, I'm pre-processing an image and feeding it to a model for predictions.:
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)
How can I run this python code on a webpage to preprocess images from the page for input? I looked into Tensorflow.js and recreating the workflow, but I think because it relies on applications.vgg19 (which exists in Tensorflow but not Tensorflow.js) I have to create a python environment to do the same/similar thing:
<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>
Am I using Pyodide correctly in this case? I keep getting syntax errors when trying to execute this live. Or is there an easier way to approach this problem? The reshaping is rather complex, a [-1, 9, 9, 512] array that is dependent on convolutional layers.