async def predict_api(request: Request, email: str = Form(...), usr: str = Form(...), age: int = Form(...), file: UploadFile = File(...)): # 帶入參數 email = email usr = usr age = age filename = file.filename # 許可圖片格式 extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png") # 圖片格式判定, 中斷返回下列訊息 if not extension: predictionMessage = "Image must be jpg or png format!" else: # #with open("/tmp/destination.png", "wb") as buffer: # shutil.copyfileobj(file.file, buffer) # 圖片讀取, 啟動等待作業 image = read_imagefile(await file.read()) image.save("static/" + filename) # 圖片預測函式 predictionMessage = predict(image) return templates.TemplateResponse('form2.html', context={ 'request': request, 'email': email, 'usr': usr, 'age': age, 'filename': filename, 'prediction': predictionMessage })
async def predict_api(file: UploadFile = File(...)): extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png") if not extension: return "Image must be jpg or png format!" image = read_imagefile(await file.read()) prediction = predict(image) return prediction
async def predict_api(file: UploadFile = File(...)): # 許可圖片格式 extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png") # 圖片格式判定, 中斷返回下列訊息 if not extension: return "Image must be jpg or png format!" else: # 圖片讀取, 啟動等待作業 image = read_imagefile(await file.read()) # 圖片預測函式 prediction = predict(image) # 傳回預測結果 return prediction