-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
41 lines (31 loc) · 1.28 KB
/
app.py
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
from fastapi import FastAPI, File, UploadFile, HTTPException, status, Response
from PIL import Image
import io
import logging
from main import segment_everything
app = FastAPI()
@app.post("/segment-image")
async def segment_image(file: UploadFile = File(...)):
# Ensure the file is an image
if not file.content_type.startswith('image/'):
raise HTTPException(status_code=400, detail="File is not an image.")
try:
# Read the contents of the uploaded file
contents = await file.read()
# Convert the contents to a byte stream
image_stream = io.BytesIO(contents)
# Open the byte stream as an image object
image = Image.open(image_stream).convert("RGB")
# Process the image with the MobileSam model
segmented_image = segment_everything(image)
# Convert the segmented image to a byte array response
img_byte_arr = io.BytesIO()
segmented_image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
return Response(content=img_byte_arr, media_type="image/png")
except Exception as e:
# Handle exceptions
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
def read_root():
return {"Hello: Welcome to MobileSam segmentation service"}