簡體   English   中英

如何在服務器上托管 function,我可以從我的 python 腳本調用它?

[英]How do I host a function on a server, which I can call from my python script?

我最近做了一個程序,它在圖像上運行 ML model,並預測它是什么。 問題是,包括 model 和 tensorflow 等依賴項,導致它為500mb 所以我想知道我是否可以在服務器中托管 function,我可以在其中傳遞我的圖像,它會返回 output。 這是我希望主持的 function -

from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np
labels= ["Banana", "Fan", "Clock","Coin","Leaf","Paper_airplane","Pen","Phone","Spoon","Tomato"]
model = load_model('keras_model.h5')
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

#Here I wish to pass the image, from my python program, and recieve the prediction
def RunPrediction(img):
    image = img
    size = (224, 224)
    image = ImageOps.fit(image, size, Image.ANTIALIAS)

    image_array = np.asarray(image)
    normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
    data[0] = normalized_image_array

    #And I want to recieve this output in my code
    prediction = model.predict(data)

    return prediction

我如何使用 python 腳本調用這個 function? 謝謝!

我如何實現這一目標? 謝謝!

制作一個 RestAPI 並在您的程序中調用它。

這是關於 RestAPI 的幫助鏈接: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxiii-application-programming-interfaces-apis

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM