File size: 1,060 Bytes
39ba66a
 
 
dd35a77
0f2b7d6
 
 
fa33891
 
856944d
dd35a77
 
 
39ba66a
 
 
 
 
 
 
a4ef1ef
39ba66a
 
 
 
 
 
b1e7a24
dd35a77
0f2b7d6
 
 
 
 
 
 
 
2a9c2a8
0f2b7d6
 
 
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
from PIL import Image
from fastapi import FastAPI
from process import Segment
from pydantic import BaseModel
import numpy as np
import io
import base64
import os

os.environ['TORCH_HOME'] = "/app/cache"

class SegmentationBody(BaseModel):
    baseImage: str

app = FastAPI()

def process_image(input_image):
    if not isinstance(input_image, Image.Image):
        input_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
    
    output_image = Segment(input_image)
    return output_image

@app.get("/")
def root_path():
    return { "log": "Lol" }
    
@app.post("/api/segment")
def api_segment(data: SegmentationBody):
    # Convert base64 string to PIL Image
    input_image = Image.open(io.BytesIO(base64.b64decode(data.baseImage)))
    
    # Process the image
    output_image = process_image(input_image)
    
    # Convert PIL Image to base64 string
    byte_arr = io.BytesIO()
    output_image.save(byte_arr, format='PNG')
    encoded_image = base64.b64encode(byte_arr.getvalue()).decode('ascii')
    
    return { "image": encoded_image }