从烧瓶中得到结果

I have trained a deep learning model, save it with and now I want to hit a web service in order to get pack a category. This is the first time I try to do that so I am a little confused. I can not figure out how to take categories back. Also when I write http://localhost:8000/predict I get back Method Not Allowed The method is not allowed for the requested URL. The code so far is below

from flask import Flask,request, jsonify#--jsonify will return the data
import os
from keras.models import load_model

app = Flask(__name__)

os.chdir(r'C:\Users\aggelos\Desktop\flask')
model = load_model(os.path.join(os.getcwd(), "lstm-final-five-Copy1.h5"))

@app.route('/')
def getmodel():
    return str (model)
@app.route('/predict', methods= ["POST"])
def predict():
    a1= pd.read_csv("sitting 1.csv")
    a1.insert(0, 'activity', "sitting")
    b1= pd.read_csv("standing 1.csv")
    b1.insert(0, 'activity', "standing")
    c1= pd.read_csv("walking 1.csv")
    c1.insert(0, 'activity', "walking")
    d1= pd.read_csv("jogging 1.csv")
    d1.insert(0, 'activity', "jogging")
    e1= pd.read_csv("upstairs 1.csv")
    e1.insert(0, 'activity', "upstairs")
    f1= pd.read_csv("downstairs 1.csv")
    f1.insert(0, 'activity', "downstairs")

    df1 = pd.concat([a1, b1, c1 , d1, e1 ,f1])


    a2= pd.read_csv("sitting 2.csv")
    a2.insert(0, 'activity', "sitting")
    b2= pd.read_csv("standing 2.csv")
    b2.insert(0, 'activity', "standing")
    c2= pd.read_csv("walking 2.csv")
    c2.insert(0, 'activity', "walking")
    d2= pd.read_csv("jogging 2.csv")
    d2.insert(0, 'activity', "jogging")
    e2= pd.read_csv("upstairs 2.csv")
    e2.insert(0, 'activity', "upstairs")
    f2= pd.read_csv("downstairs 2.csv")
    f2.insert(0, 'activity', "downstairs")

    df2 = pd.concat([a2, b2, c2 , d2, e2 ,f2])

    a3= pd.read_csv("sitting 3.csv")
    a3.insert(0, 'activity', "sitting")
    b3= pd.read_csv("standing 3.csv")
    b3.insert(0, 'activity', "standing")
    c3= pd.read_csv("walking 3.csv")
    c3.insert(0, 'activity', "walking")
    d3= pd.read_csv("jogging 3.csv")
    d3.insert(0, 'activity', "jogging")
    e3= pd.read_csv("upstairs 3.csv")
    e3.insert(0, 'activity', "upstairs")
    f3= pd.read_csv("downstairs 3.csv")
    f3.insert(0, 'activity', "downstairs")

    df3 = pd.concat([a3, b3, c3 , d3, e3 ,f3])

    a4= pd.read_csv("sitting 4.csv")
    a4.insert(0, 'activity', "sitting")
    b4= pd.read_csv("standing 4.csv")
    b4.insert(0, 'activity', "standing")
    c4= pd.read_csv("walking 4.csv")
    c4.insert(0, 'activity', "walking")
    d4= pd.read_csv("jogging 4.csv")
    d4.insert(0, 'activity', "jogging")
    e4= pd.read_csv("upstairs 4.csv")
    e4.insert(0, 'activity', "upstairs")
    f4= pd.read_csv("downstairs 4.csv")
    f4.insert(0, 'activity', "downstairs")

    df4 = pd.concat([a4, b4, c4 , d4, e4 ,f4])

    a5= pd.read_csv("sitting 5.csv")
    a5.insert(0, 'activity', "sitting")
    b5= pd.read_csv("standing 5.csv")
    b5.insert(0, 'activity', "standing")
    c5= pd.read_csv("walking 5.csv")
    c5.insert(0, 'activity', "walking")
    d5= pd.read_csv("jogging 5.csv")
    d5.insert(0, 'activity', "jogging")
    e5= pd.read_csv("upstairs 5.csv")
    e5.insert(0, 'activity', "upstairs")
    f5= pd.read_csv("downstairs 5.csv")
    f5.insert(0, 'activity', "downstairs")

    df5 = pd.concat([a5, b5, c5 , d5, e5 ,f5])

    f_final = pd.concat([df1, df2, df3, df4, df5], axis=0)

    df_final.columns = ["activity", "time 1", "accx", "accy", "accz", "time 2",  "gyrx", "gyry", 
    "gyrz",  "hr", "longitude", "latitude"]
    df_final= df_final.drop(['time 1', 'time 2',   "hr", "longitude", "latitude" ], axis = 1).copy()

    df_final.dropna(how='any', inplace=True)

    activities = df_final['activity'].value_counts().index

    label = LabelEncoder()
    df_final['label'] = label.fit_transform(df_final['activity'])

    X = df_final[['accx', 'accy', 'accz', 'gyrx', 'gyry', 'gyrz']] 
    y = df_final['label'] 

    scaler = StandardScaler()
    X = scaler.fit_transform(X)

    df_final = pd.DataFrame(X, columns = ['accx', 'accy', 'accz', 'gyrx', 'gyry', 'gyrz'])
    df_final['label'] = y.values

    Fs = 50
    frame_size = Fs*2 # 200 samples
    hop_size = frame_size # 40 samples
    def get_frames(df_final, frame_size, hop_size):


       N_FEATURES = 6 #x,y,z (acc,gut)

      frames = []
      labels = []
      for i in range(0, len(df_final) - frame_size, hop_size):
          accx = df_final['accx'].values[i: i + frame_size]
          accy = df_final['accy'].values[i: i + frame_size]
          accz = df_final['accz'].values[i: i + frame_size]
          gyrx = df_final['gyrx'].values[i: i + frame_size]
          gyry = df_final['gyry'].values[i: i + frame_size]
          gyrz = df_final['gyrz'].values[i: i + frame_size]

          # Retrieve the most often used label in this segment
          label = stats.mode(df_final['label'][i: i + frame_size])[0][0]
          frames.append([accx, accy, accz, gyrx, gyry, gyrz])
          labels.append(label)

      # Bring the segments into a better shape
      frames = np.asarray(frames).reshape(-1, frame_size, N_FEATURES)
      labels = np.asarray(labels)

      return frames, labels

      X, y = get_frames(df_final, frame_size, hop_size)

     pred = model.predict_classes(X)

if __name__ == '__main__':
app.run(host="localhost", port=8000, debug=False)
评论