如何动态显示图像?

时间:2017-01-22 05:08:24

标签: python flask keras

你好我正在制作一个小页面来显示我的结果我正在一个关于情绪分析的项目中我首先得到以下标签:

$ sed 's#^\(\\subimport{chapters/}{[[:alnum:]_]\+\)}$#\1-yyy}#' <<<'\subimport{chapters/}{foobar9}'
\subimport{chapters/}{foobar9-yyy}

$ sed 's#^\(\\subimport{chapters/}{[[:alnum:]_]\+\)}$#\1-yyy}#' <<<'\subimport{chapters/}{spamegg923}'
\subimport{chapters/}{spamegg923-yyy}

我根据我使用keras执行的预测函数的结果显示此标签,此时一切运行良好我希望根据上面的标签显示图像我尝试创建一个带有路径的数组图像如下,我不知道如何编写图像功能,

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():

由于我是烧瓶的初学者,所以感谢您的帮助,感谢您支持或建议克服这种情况,

在我尝试了非常有用的反馈之后:

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")

我没有收到任何错误,但图片没有显示我不太确定是什么问题,此时此刻我正在尝试使用位于我的文件同一级别的一个图像app.py,smile.jpg

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 个答案:

答案 0 :(得分: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")