赞
踩
#api接口,主函数 import base64 import io import cv2 import os import time from PIL import Image import numpy as np from fastapi import FastAPI,HTTPException import uvicorn app = FastAPI() ############################################# # @app.post("/uploadfile/") # async def create_upload_file(file: UploadFile = File(...)): # contents = await file.read() # return {"filename": file.filename, "contents": contents} @app.post("/upload_image") async def upload_image(file: UploadFile): if not file: raise HTTPException(status_code=400, detail="没有收到图片文件") image = await file.read() try: # 将图片转换为base64编码 image_base64 = base64.b64encode(image).decode('utf-8') # 将base64编码的图片解码为原始图像 image_array = np.frombuffer(base64.b64decode(image_base64), dtype=np.uint8) image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) # 保存图像到临时文件 cv2.imwrite("temp.jpg", image) img_path = "temp.jpg" except Exception as e: raise HTTPException(status_code=500, detail=str(e)) return {"image_path": img_path} @app.get("/") async def read_root(): return {"Hello": "World"} if __name__ == '__main__': uvicorn.run(app, host="192.168.0.1", port=int("8000"))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。