File size: 1,643 Bytes
9346b00 1918f2b 9346b00 1dc9545 1918f2b 67c10a3 9346b00 1918f2b 9346b00 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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()
# Root route
@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(...)):
# Form(...) to accept input as web form ,may change when android /upload
'''
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))
# Process the image (example: convert to grayscale)
processed_image = image.convert("L")
# Save the processed image to a temporary file
output_file_path = "tmp_processed_image.png"
processed_image.save(output_file_path)
# Return the processed image for download
return FileResponse(output_file_path, media_type='image/png', filename="tmp_processed_image.png")
|