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")