|
from fastapi import FastAPI ,Request ,Form, UploadFile, File |
|
from fastapi.responses import JSONResponse |
|
from fastapi.responses import HTMLResponse, FileResponse |
|
import os |
|
import io |
|
from PIL import ImageOps,Image ,ImageFilter |
|
|
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import ast |
|
from server import * |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.get('/') |
|
def main(): |
|
return "Hello World taha" |
|
|
|
|
|
|
|
|
|
@app.post('/imageStep1') |
|
async def image_step1(image_file: UploadFile = File(...),type_of_filters: str = Form(...), blur_radius: str = Form(...)): |
|
|
|
contents = await image_file.read() |
|
image = Image.open(io.BytesIO(contents)) |
|
|
|
produced_image=SegmenterBackground().Back_step1(image,type_of_filters,int(blur_radius))[0] |
|
|
|
|
|
output_file_path_tmp = "/tmp/tmp_processed_image.png" |
|
produced_image.save(output_file_path_tmp) |
|
|
|
|
|
return FileResponse(output_file_path_tmp, media_type='image/png', filename="/tmp/tmp_processed_image.png") |
|
|
|
|
|
@app.post('/imageStep2') |
|
async def image_step2(image_file: UploadFile = File(...),things_replace: str = Form(...), blur_radius: str = Form(...)): |
|
|
|
contents = await image_file.read() |
|
image = Image.open(io.BytesIO(contents)) |
|
|
|
things_replace=ast.literal_eval(things_replace) |
|
|
|
produced_image=SegmenterBackground().Back_step2(image,"cam",things_replace,int(blur_radius)) |
|
|
|
|
|
output_file_path_tmp = "/tmp/tmp_processed_image.png" |
|
produced_image.save(output_file_path_tmp) |
|
|
|
|
|
return FileResponse(output_file_path_tmp, media_type='image/png', filename="/tmp/tmp_processed_image.png") |
|
|
|
|
|
|
|
|
|
|
|
@app.post('/predict') |
|
async def predict(supported_types_str: str = Form(),age: str = Form() , file: UploadFile = File(...)): |
|
|
|
|
|
supported_types=ast.literal_eval(supported_types_str) |
|
|
|
contents = await file.read() |
|
image = Image.open(io.BytesIO(contents)) |
|
|
|
|
|
processed_image = image.convert("L") |
|
|
|
|
|
output_file_path = "/tmp/tmp_processed_image.png" |
|
processed_image.save(output_file_path) |
|
|
|
|
|
return FileResponse(output_file_path, media_type='image/png', filename="/tmp/tmp_processed_image.png") |
|
|
|
|