Download services/preprocess.py from MBG0903/AI_Floor_Plan_Studio: direct link, hf CLI and curl.
- Browser
- Download file 722 Bytes
-
https://huggingface.co/spaces/MBG0903/AI_Floor_Plan_Studio/resolve/main/services/preprocess.py
- Command line
-
hf download hf://spaces/MBG0903/AI_Floor_Plan_Studio/services/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/spaces/MBG0903/AI_Floor_Plan_Studio/resolve/main/services/preprocess.py
722 Bytes
| from PIL import Image, ImageOps, ImageFilter, ImageEnhance | |
| import numpy as np | |
| def preprocess_floorplan(image, max_side=1400): | |
| img=image.convert("RGB") | |
| w,h=img.size | |
| scale=min(1.0,max_side/max(w,h)) | |
| if scale<1.0: | |
| img=img.resize((int(w*scale),int(h*scale)),Image.LANCZOS) | |
| gray=ImageOps.grayscale(img) | |
| gray=ImageEnhance.Contrast(gray).enhance(1.8) | |
| gray=gray.filter(ImageFilter.MedianFilter(size=3)) | |
| arr=np.array(gray) | |
| low=np.percentile(arr,18); high=np.percentile(arr,92) | |
| if high<=low: high=low+1 | |
| norm=np.clip((arr-low)/(high-low)*255,0,255).astype(np.uint8) | |
| clean=ImageOps.autocontrast(Image.fromarray(norm,mode="L")) | |
| return Image.merge("RGB",(clean,clean,clean)) | |