FastApi send 图片(2)

服务端

@app.post("/SegCrop")
async def SegDD(imgID: str = Form(...),SegDD: str = Form(...),file: UploadFile = File(...)):

    # imgID = item.imgID
    print("imgID---->",imgID)
    print("SegDD---->",SegDD)

    contents = await file.read()
    # with open(file.filename, 'wb') as f:

    with open("filename.jpg", 'wb') as f:
        f.write(contents)

    await file.close()

客户端

img=cv2.imread("xx.jpg")
success, encoded_image = cv2.imencode(".jpg", img)
# 将数组转为bytes
img_bytes = encoded_image.tobytes()

# headers = {"Content-type": "application/json" "image/png"}
headers = {"Content-type":  "image/jpeg"}

url = 'http://18.0.44.100:9007/SegCrop'
file = {'file': img_bytes}
data={"imgID":1000,"SegDD":True}
resp = requests.post(url=url,files=file,data=data)
print(resp.json())

参考文章

  • https://blog.csdn.net/m0_37400316/article/details/125994185
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FastAPI WebSocket是FastAPI框架的一部分,它允许在应用程序中实现基于WebSocket的实时通信。它基于标准的Python异步框架,具有高性能和易用性的特点。 使用FastAPI WebSocket,您可以轻松地构建具有实时功能的应用程序,例如聊天应用程序、实时博客评论和在线协作工具等。FastAPI WebSocket使用asyncio库和websockets库来实现WebSocket协议。 要使用FastAPI WebSocket,您需要在FastAPI应用程序中添加一个WebSocket路由。您可以使用async def函数将WebSocket请求处理程序添加到路由中,该函数将处理来自客户端的WebSocket消息,并向客户端发送消息。FastAPI WebSocket还提供了一个WebSocket连接上下文,允许您跟踪每个连接的状态并处理连接事件。 以下是一个使用FastAPI WebSocket的示例代码: ```python from fastapi import FastAPI, WebSocket app = FastAPI() @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() while True: data = await websocket.receive_text() await websocket.send_text(f"You said: {data}") ``` 上面的代码将在应用程序中添加一个WebSocket路由,并在该路由中定义一个名为websocket_endpoint的async def函数来处理WebSocket请求。当客户端建立WebSocket连接时,该函数将使用accept()方法接受连接,并进入一个循环以处理来自客户端的消息。当客户端发送文本消息时,该函数将使用receive_text()方法接收消息,并使用send_text()方法向客户端发送回复。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值