|
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 |
|
from transformers import pipeline |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.get('/') |
|
def hello_world(): |
|
return "Hello World taha" |
|
|
|
def get_segment_image(raw_image): |
|
pipe = pipeline("image-segmentation", model="Intel/dpt-large-ade") |
|
output = pipe(raw_image, points_per_batch=32) |
|
return output |
|
|
|
def get_supported_segmentation(output): |
|
return [obj for obj in output if obj['label']=='person'] |
|
|
|
|
|
@app.post('/predict') |
|
async def predict(name: str = Form(),age: str = Form() , file: UploadFile = File(...)): |
|
|
|
''' |
|
contents = await file.read() |
|
|
|
image = Image.open(io.BytesIO(contents)) |
|
|
|
|
|
return { |
|
"message": f"Your name is {name}, age is {age}", |
|
"filename": file.filename, |
|
"image:": str(np.array(image)) # Returns the original image size |
|
} |
|
''' |
|
contents = await file.read() |
|
image = Image.open(io.BytesIO(contents)) |
|
|
|
|
|
processed_image = image.convert("L") |
|
|
|
|
|
output_file_path = "tmp_processed_image.png" |
|
processed_image.save(output_file_path) |
|
|
|
|
|
return FileResponse(output_file_path, media_type='image/png', filename="tmp_processed_image.png") |
|
|