首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图片从瓶发送到HTML不成功

将图片从瓶发送到HTML不成功
EN

Stack Overflow用户
提问于 2022-09-21 08:53:57
回答 1查看 52关注 0票数 -2

当我点击我的Flask代码中的预测按钮时,我想用html应用程序发送图像。代码应该得到提示,运行ML模型,并根据输入提示符生成图像。我可以生成图片,但不知怎么我不能将它发送到html,或者html不显示图像。任何人都可以检查我的flaskhtml代码,问题出在哪里?我的flask图像发送方法是错误的还是html部件是错误的?如何在html中显示图像?

代码语言:javascript
复制
import os
import io
import base64
from PIL import Image
import torch
from torch import autocast
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
import transformers
from flask import Flask, render_template, request

app = Flask(__name__)

lms = LMSDiscreteScheduler(
    beta_start=0.00085,
    beta_end=0.012,
    beta_schedule="scaled_linear"
    )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

pipe = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    scheduler=lms,
    use_auth_token=True,
    cache_dir=os.getenv("cache_dir", "./models")
    ).to("cuda")

@app.route('/', methods=['GET'])

def page_template():
    return render_template('index.html')

@app.route('/', methods=['POST'])

def predict():
    prompt_text = request.form['word']
    with autocast("cuda"):
        image = pipe(prompt_text)["sample"][0]
        data = io.BytesIO()
        image.save(data, "JPEG")
        encoded_img_data = base64.b64encode(data.getvalue())
    return render_template("index.html", img_data=encoded_img_data.decode('utf-8'))


if __name__ == '__main__':
    app.run()


<!DOCTYPE html>
<html>
    <head>
        <title>Tutorial</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    </head>

    <body>
        <h1 class="text-center">Stable Diffusion</h1>

        <form class="p-3 text-center" action='/', method="post" enctype="multipart/form-data" >
            <label for="promptfield" class="form-label">StableDiffusionPrompt</label>
            <input type="text" class="form-control" id="promptfield" name="word" placeholder="Please enter your prompt" />


            <input class="form-control" type="file" name="imagefile" >
            <input class="btn btn-primary mt-3" type="submit" value="Predict Image" >
        </form>
        
        {% if prediction %}
            <img id="picture" src="data:image/jpeg;base64,{{ img_data }}">
            <p> img id="picture" src="data:image/jpeg;base64,{{ img_data }}"</p>
        {% endif %}
        
    </body>


</html>
EN

回答 1

Stack Overflow用户

发布于 2022-09-23 09:48:05

这是encoded_img_data.decode('utf-8')的输出。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73797968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档