Spaces:
Runtime error
Runtime error
Download app.py from VoxlDev/ClothesSegmentation: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/VoxlDev/ClothesSegmentation/resolve/main/app.py
- Command line
-
hf download hf://spaces/VoxlDev/ClothesSegmentation/app.py
-
curl -L -o app.py https://huggingface.co/spaces/VoxlDev/ClothesSegmentation/resolve/main/app.py
1.06 kB
| 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 | |
| def root_path(): | |
| return { "log": "Lol" } | |
| 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 } | |