2017-01-22 20 views
-1

こんにちは、私は私が最初に評判分析に関するプロジェクト、私は次のラベルを持っているで働いています私の結果を表示するために、小さなページを作っています:動的に表示する方法と画像を表示する方法は?

senti=["furious","angry","angry0","Indiferent","happy","enthusiastic","Euphoric"] 

私は私の予測関数の結果に依存し、このラベルを表示ケラスを使用して実行され、この時点ですべてがうまくいきます。上のラベルに応じて画像を表示したいと思います。画像のパスを使って配列を作成しようとしましたが、画像関数を書き込む方法がわかりません。

images=['home/image0.jpg','home/image1.jpg','home/image2.jpg','home/image3.jpg','home/image4.jpg','home/image5.jpg','home/image6.jpg'] 
def image(): 

これは予測を実行する関数です。この時点ではshowi上記のラベルをngの、私はまた、個別の画像を表示したいと思いますので、私は次の関数を変更する必要があります。

def predict(text): 
    seqs = tok.texts_to_sequences([text]) 
    print(text) 
    word_index = tok.word_index 
    print('Found %s unique tokens.' % len(word_index)) 
    sequence_pred = sequence.pad_sequences(seqs, maxlen=MAX_SEQUENCE_LENGTH) 
    print(sequence_pred) 
    prediction = model.predict(sequence_pred) 
    print(prediction) 
    return senti[np.argmax(prediction[0])] 

    @app.route("/", methods=['GET', 'POST']) 
    def index(): 
     print(request.method) 
     if request.method == 'POST': 
      q=request.form['querytext'] 
      prediction=predict(q) 
      return render_template("result.html",prediction=prediction,text=q) 
     return render_template("main.html") 

私はフラスコで初心者ですので、私はこれを克服するためのサポートや提案に感謝したいと思います状況助けてくれてありがとう、

は、私が試した非常に有益なフィードバック後:

senti=["furious","angry","angry0","Indiferent","happy","enthusiastic","Euphoric"] 


def predict(text): 
    seqs = tok.texts_to_sequences([text]) 
    print(text) 
    word_index = tok.word_index 
    print('Found %s unique tokens.' % len(word_index)) 
    sequence_pred = sequence.pad_sequences(seqs, maxlen=MAX_SEQUENCE_LENGTH) 
    print(sequence_pred) 
    prediction = model.predict(sequence_pred) 
    print(prediction) 
    return senti[np.argmax(prediction[0])] 

@app.route("/", methods=['GET', 'POST']) 
def index(): 
    senti=["furious","angry","angry0","Indiferent","happy","enthusiastic","Euphoric"] 
    images=['smile.jpg','smile.jpg','smile.jpg','smile.jpg','smile.jpg','smile.jpg','smile.jpg'] 
    lookup_keys = dict(zip(senti, images)) 
    print(request.method) 
    if request.method == 'POST': 
     q=request.form['querytext'] 
     prediction=predict(q) 
     image_path = lookup_keys[prediction] # get the path 
     return render_template("result.html", 
           prediction=prediction, 
           text=q, 
           image_url=image_path) 
    return render_template("main.html") 

私はエラーを取得していないのですが、画像は、私はこの瞬間、私は何が間違っているので、確認していない表示されません。 1つの画像を同じレベルに配置しようとしています私のファイルapp.py、smile.jpg

$ ls 
app.py smile.jpg 

答えて

1

あなたのキーとイメージ値の辞書を作成してください。そして、特定の感情のために画像を返すためにそれを使用:

>>> senti=["furious","angry","angry0","Indiferent","happy","enthusiastic","Euphoric"] 
>>> images=['home/image0.jpg','home/image1.jpg','home/image2.jpg','home/image3.jpg','home/image4.jpg','home/image5.jpg','home/image6.jpg'] 
>>> dict(zip(senti, images)) 
{'enthusiastic': 'home/image5.jpg', 'Indiferent': 'home/image3.jpg', 'furious': 'home/image0.jpg', 'Euphoric': 'home/image6.jpg', 'angry': 'home/image1.jpg', 'happy': 'home/image4.jpg', 'angry0': 'home/image2.jpg'} 
>>> lookup_values = dict(zip(senti, images)) 
>>> lookup_values['angry'] 
'home/image1.jpg' 

あなたは右の画像のパスを取得するには、ビューの方法でこれを使用して、テンプレートにそれを送ることができます。

@app.route("/", methods=['GET', 'POST']) 
def index(): 
    senti=["furious","angry","angry0","Indiferent","happy","enthusiastic","Euphoric"] 
    images=['home/image0.jpg','home/image1.jpg','home/image2.jpg','home/image3.jpg','home/image4.jpg','home/image5.jpg','home/image6.jpg'] 
    lookup_keys = dict(zip(senti, images)) 
    print(request.method) 
    if request.method == 'POST': 
     q=request.form['querytext'] 
     prediction=predict(q) 
     image_path = lookup_keys[prediction] # get the path 
     return render_template("result.html", 
           prediction=prediction, 
           text=q, 
           image_url=image_path) 
    return render_template("main.html") 
+0

感謝にそれを私はこのメソッドを試してみましたが、画像は表示されていません。何が間違っているのかわからないので、私は質問を更新します。 – neo33

+0

[静的ファイルのフラスコのドキュメント]を読んでください。 ://flask.pocoo.org/docs/0.12/quickstart/#static-files) –

+0

はい私はそれを読むでしょう、イメージを表示するためにresult.htmlという名前のファイルを変更する必要があることを知らなかった ご支援ありがとうございます – neo33

関連する問題