首先我在app.py文件中定义了两个函数,如下所示:
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])]
这里我得到image_url的值,它是我图像的路径,如下所示:
@app.route("/", methods=['GET', 'POST'])
def index():
senti=["furious","angry","angry0","Indiferent","happy","enthusiastic","Euphoric"]
images=['static/angry.jpg','static/angry.jpg','static/angry.jpg','static/smile.jpg','static/smile.jpg','static/smile.jpg','static/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
print(image_path)
return render_template("result.html",
prediction=prediction,
text=q,
image_url=image_path)
return render_template("main.html")
然后根据我想要显示的结果和图像我只使用静态文件夹中包含的两个smile.jpg和angry.jpg, 之后,我将我的模板称为:" result.html"
此文件包含以下标记,以向我显示文本和预测,如下所示:
<h3>{{text}} </br> <button class="button-primary">{{prediction}}</button></h3>
一切正常,直到这一点我想动态显示图像:
<h3>{{image_url}}<td><img src={image_url} alt=""></td></h3>
但是没有显示任何内容,如果我放下以下标记:
<td><img src='static/angry.jpg' alt=""></td>
图像正在显示有任何问题我想欣赏支持修改上面的标签以显示图像dinamically我的意思是这一部分:
<td><img src='static/angry.jpg' alt=""></td>
我想改变这个字符串&#39; static / angry.jpg&#39;使用名为image_url的变量值,我是一个使用flask的begginer,所以我不知道如何使用这个值,谢谢你的支持。
答案 0 :(得分:1)
如果你确定你的image_url路径没问题,那么:
<td><img src="{{image_url}}" alt=""></td>