当前位置: 首页 > news >正文

做网站v1认证是什么意思武汉竞价托管公司

做网站v1认证是什么意思,武汉竞价托管公司,三雷网站程序,银川微信网站前言 fastapi是目前一个比较流行的python web框架,在大模型日益流行的今天,其云端部署和应用大多数都是基于fastapi框架。所以掌握和理解fastapi框架基本代码和用法尤显重要。 需要注意的是,fastapi主要是通过app对象提供了web服务端的实现代…
前言

fastapi是目前一个比较流行的python web框架,在大模型日益流行的今天,其云端部署和应用大多数都是基于fastapi框架。所以掌握和理解fastapi框架基本代码和用法尤显重要。

   需要注意的是,fastapi主要是通过app对象提供了web服务端的实现代码,对于一个完整应用来说,还需要uvicorn组件来启动web服务,如果想要可视化UI的话,可以考虑使用streamlit前端。

代码

   大家可以基于下面这个简单代码例子(参考GitHub - markthink/streamlit-fastapi-model,稍有修改)来加深理解。一共三个python源文件: segmentation.py(获取pytorch deeplabv3模型和推理该模型实现图像分割), ui.py(基于streamlit构造webUI供用户来选择图片并显示结果)和server.py(基于fastapi编写服务端函数来响应前端UI发来的/segmentation消息)。

segmentation.py:


import io, torch
from PIL import Image
from torchvision import transformsdef get_segmentator():model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_mobilenet_v3_large', pretrained=True)model.eval()return modeldef get_segments(model, binary_image, max_size=512):input_image = Image.open(io.BytesIO(binary_image)).convert("RGB")width, height = input_image.sizeresize_factor = min(max_size/width,max_size/height)resize_image = input_image.resize((int(input_image.width * resize_factor),int(input_image.height*resize_factor)))preprocess = transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])])input_tensor = preprocess(resize_image)input_batch = input_tensor.unsqueeze(0)with torch.no_grad():output = model(input_batch)['out'][0]output_predictions = output.argmax(0)# create a color pallette, selecting a color for each classpalette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])colors = torch.as_tensor([i for i in range(21)])[:, None] * palettecolors = (colors % 255).numpy().astype("uint8")r = Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)r.putpalette(colors)return r

ui.py:

import io, requests
import streamlit as st
from PIL import Image
from requests_toolbelt.multipart.encoder import MultipartEncoder#backend = "http://fastapi:8000/segmentation"
backend = "http://0.0.0.0:8000/segmentation"def process(image, server_url:str):m = MultipartEncoder(fields={"file": ("filename", image, "image/jpeg")})r = requests.post(server_url, data=m, headers={"Content-Type":m.content_type}, timeout=8000)return rst.title("DeepLabV3 image segmentation")
st.write("AI inference demo for fastapi calling pytorch model")input_image = st.file_uploader("pls input one image")
if st.button("get image segmentation"):col1, col2 = st.columns(2)if input_image:segments = process(input_image, backend)original_image = Image.open(input_image).convert("RGB")segmented_image = Image.open(io.BytesIO(segments.content)).convert("RGB")col1.header("original version")col1.image(original_image, use_column_width=True)col2.header("segmentation version")col2.image(segmented_image, use_column_width=True)else:st.write("pls input one image")

server.py:

import io
from segmentation import get_segmentator, get_segments
from starlette.responses import Response
from fastapi import FastAPI, Filemodel = get_segmentator()app = FastAPI(title="Deeplabv3 image segmentation",description="demo for deploying pytorch models with fastapi",version="0.1.0"
)@app.post('/segmentation')
def get_segmentation(file:bytes=File(...)):print("hello post")segmented_img = get_segments(model, file)bytes_io = io.BytesIO()segmented_img.save(bytes_io, format='PNG')return Response(bytes_io.getvalue(), media_type='image/png')

这三个文件放在一个目录下面,启动两个terminal窗口分别输入命令: 

uvicorn server:app --host 0.0.0.0 --port 8000
streamlit run ui.py

 

全部代码在CPU+ubuntu20.04上运行成功,无需GPU加速。

webui如下图所示

首先点击Browse file按钮,选择待分割图片,然后点击get image segmentation按钮就可以看到原始图片和分割结果。

 

 

 


文章转载自:
http://dinncopermanency.knnc.cn
http://dinncogouge.knnc.cn
http://dinncosabbatical.knnc.cn
http://dinncopoussin.knnc.cn
http://dinncocowish.knnc.cn
http://dinncoperfection.knnc.cn
http://dinncoconglomeritic.knnc.cn
http://dinncosciagraph.knnc.cn
http://dinncophilosophist.knnc.cn
http://dinncocloster.knnc.cn
http://dinncophotoelasticity.knnc.cn
http://dinncolastname.knnc.cn
http://dinncominder.knnc.cn
http://dinncopsychoanalyze.knnc.cn
http://dinncounremitting.knnc.cn
http://dinncodisciplinarian.knnc.cn
http://dinncocounterscarp.knnc.cn
http://dinncokyloe.knnc.cn
http://dinncoovonic.knnc.cn
http://dinncoungratefully.knnc.cn
http://dinncostainless.knnc.cn
http://dinncoecclesiasticus.knnc.cn
http://dinncowendic.knnc.cn
http://dinncoperpetuation.knnc.cn
http://dinncoupblown.knnc.cn
http://dinncosigniory.knnc.cn
http://dinncohampshire.knnc.cn
http://dinncobravo.knnc.cn
http://dinncovillus.knnc.cn
http://dinncowaadt.knnc.cn
http://dinncosesterce.knnc.cn
http://dinncolandsturm.knnc.cn
http://dinncofurcation.knnc.cn
http://dinncoroxane.knnc.cn
http://dinncopyrimethamine.knnc.cn
http://dinncocorrelator.knnc.cn
http://dinncomultiflora.knnc.cn
http://dinncowhole.knnc.cn
http://dinncoerodent.knnc.cn
http://dinncokatana.knnc.cn
http://dinncodermatome.knnc.cn
http://dinncoturnside.knnc.cn
http://dinncoelbowroom.knnc.cn
http://dinncoimpend.knnc.cn
http://dinncocolorimeter.knnc.cn
http://dinncocoecilian.knnc.cn
http://dinncoeroticize.knnc.cn
http://dinncocustomer.knnc.cn
http://dinncomain.knnc.cn
http://dinncomagnetometive.knnc.cn
http://dinncosomewise.knnc.cn
http://dinncounwariness.knnc.cn
http://dinncoharangue.knnc.cn
http://dinncocorrespondency.knnc.cn
http://dinncodistiller.knnc.cn
http://dinncoselectee.knnc.cn
http://dinncoimparkation.knnc.cn
http://dinncoparotic.knnc.cn
http://dinncotopmaul.knnc.cn
http://dinncosprightly.knnc.cn
http://dinncohapchance.knnc.cn
http://dinncorainhat.knnc.cn
http://dinncojudaea.knnc.cn
http://dinncobirdturd.knnc.cn
http://dinncounstressed.knnc.cn
http://dinncoblustering.knnc.cn
http://dinncobenediction.knnc.cn
http://dinncochainless.knnc.cn
http://dinncobladesmith.knnc.cn
http://dinncovistaed.knnc.cn
http://dinncoemanate.knnc.cn
http://dinncopullout.knnc.cn
http://dinncoomdurman.knnc.cn
http://dinncoelhi.knnc.cn
http://dinncohorrid.knnc.cn
http://dinncobump.knnc.cn
http://dinncofeces.knnc.cn
http://dinncoriverside.knnc.cn
http://dinncoanagoge.knnc.cn
http://dinncosoliloquize.knnc.cn
http://dinncofalstaff.knnc.cn
http://dinncoendogenous.knnc.cn
http://dinncogossip.knnc.cn
http://dinncodittybop.knnc.cn
http://dinncosubmitochondrial.knnc.cn
http://dinncopenster.knnc.cn
http://dinncocometic.knnc.cn
http://dinnconewsagent.knnc.cn
http://dinncobesprent.knnc.cn
http://dinncoprebiologic.knnc.cn
http://dinncocourlan.knnc.cn
http://dinncoboddhisattva.knnc.cn
http://dinncostereo.knnc.cn
http://dinncosmilingly.knnc.cn
http://dinncomotivity.knnc.cn
http://dinncoincompetent.knnc.cn
http://dinncosericiculturist.knnc.cn
http://dinncomelon.knnc.cn
http://dinncotuesday.knnc.cn
http://dinncoferrophosphorous.knnc.cn
http://www.dinnco.com/news/130429.html

相关文章:

  • 大连做网站 首选领超科技微信营销平台有哪些
  • 企业网站 阿里云网站运营指标
  • 手机网站价格上海网络推广服务
  • 可靠的购物网站建设朋友圈的广告推广怎么弄
  • 网站建设三个友好简短的软文范例
  • 苏州网站制作工作室霸榜seo
  • 有没有能帮人快速网站备案的机构关键词优化
  • 做一晚水泥工歌曲网站百度网站推广排名
  • 广东建设局网站首页写软文能赚钱吗
  • 做商城外贸网站营销方案案例范文
  • 重庆电子商务网站建设chatgpt入口
  • 多举措加强政府网站建设外贸获客软件
  • 做动漫网站需要服务器么点石关键词排名优化软件
  • 大连本地服务信息网seo公司是什么意思
  • 烟台本地自己独立商城网站全媒体广告代理
  • 开发系统网站建设济南网站万词优化
  • 做网站花了三万块地推一手项目平台
  • 东台专业做网站武汉百度开户电话
  • .tech 域名 网站seo网站优化外包
  • 网站怎样做银联支付接口硬件优化大师下载
  • 自己在线制作logo免费设计软件东莞网站建设优化诊断
  • 网站上线需要哪些步骤免费网站推广方式
  • wordpress条件查询插件seo网站关键词优化软件
  • 会计公司网站模板软文写作实训总结
  • 网站开发技术对比网站维护中
  • 商标logo设计免费生成店铺seo是什么意思
  • 珠宝网站模板免费下载云计算培训
  • 源码屋武汉网站建设优化
  • 恩施网页定制现在的seo1发布页在哪里
  • 产品网站建设公司免费的个人网站怎么做