File size: 2,815 Bytes
b1f46e5 5d29343 b1f46e5 4722fbd 29dc177 ceb119d b1f46e5 ceb119d b1f46e5 4722fbd 7928acb 4722fbd 7928acb 4722fbd 7928acb 5e168b6 7928acb 4722fbd 5d29343 7ebdcd1 5d29343 4722fbd |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
from fastapi import FastAPI ,Request ,Form, UploadFile, File
from fastapi.responses import HTMLResponse, FileResponse,StreamingResponse,JSONResponse
import os
import io
from PIL import ImageOps,Image ,ImageFilter
#from transformers import pipeline
import matplotlib.pyplot as plt
import numpy as np
import ast
from server import *
import cv2
#http://localhost:8000
app = FastAPI()
# Root route
@app.get('/')
def main():
return "Hello World taha"
##### use space /tmp/ ...
@app.post('/imageStep1')
async def image_step1(image_file: UploadFile = File(...),type_of_filters: str = Form(...), blur_radius: str = Form(...)):#--->,background_image: UploadFile = File(...)):
contents = await image_file.read()
image = Image.open(io.BytesIO(contents))
produced_image=SegmenterBackground().Back_step1(image,type_of_filters,int(blur_radius))[0]#---->
# Save the processed image to a temporary file
output_file_path_tmp = "/tmp/tmp_processed_image.png"
produced_image.save(output_file_path_tmp)
# Return the processed image for download
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(...)):#--->,background_image: UploadFile = File(...)):
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))
# Save the processed image to a temporary file
output_file_path_tmp = "/tmp/tmp_processed_image.png"
produced_image.save(output_file_path_tmp)
# Return the processed image for download
return FileResponse(output_file_path_tmp, media_type='image/png', filename="/tmp/tmp_processed_image.png")
@app.post('/Video')
async def Video(video_file: UploadFile = File(...),kind_back: str = Form(...), blur_radius: str = Form(...)):#--->,background_image: UploadFile = File(...)):
#video_data = await video_file.read()
#nparr = np.frombuffer(video_data, np.uint8)
#video_path=cv2.imdecode(nparr, cv2.IMREAD_COLOR) #named this as just passed as it's path
video_path = f'tmp/tmptmp.mp4'#{video_file.filename}
with open(video_path, 'wb') as f:
f.write(await video_file.read())
produced_video=SegmenterBackground().Back_video(video_path, 'tmp/29_sep_2.avi','cam',['animal','person'])#video,background_image,what_remove,blur_radius=23)
return StreamingResponse(open('tmp/29_sep_2.avi', "rb"), media_type="video/mp4")
|