github-actions[bot] commited on
Commit ·
44808d4
1
Parent(s): a7c4f3e
Automated Deploy: 53bcc8519daa3559b71db352e56ee4484da1ab32
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import hashlib
|
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
-
import shutil
|
| 8 |
import time
|
| 9 |
import urllib.error
|
| 10 |
import urllib.parse
|
|
@@ -37,7 +36,7 @@ from fastapi import FastAPI, File, Form, HTTPException, Response, UploadFile, Ba
|
|
| 37 |
from fastapi.middleware.cors import CORSMiddleware
|
| 38 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 39 |
from fastapi.staticfiles import StaticFiles
|
| 40 |
-
from PIL import Image
|
| 41 |
from transformers import (
|
| 42 |
AutoImageProcessor,
|
| 43 |
AutoModelForDepthEstimation,
|
|
@@ -233,6 +232,18 @@ BUNDLE_IMAGE_QUALITY = int(config_value(
|
|
| 233 |
"bundle_image_quality",
|
| 234 |
"92",
|
| 235 |
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
DEBUG_ARTIFACTS_ENABLED = str(config_value(
|
| 237 |
"DEBUG_ARTIFACTS_ENABLED",
|
| 238 |
"runtime",
|
|
@@ -3112,6 +3123,50 @@ def load_uploaded_rgb_image(contents: bytes) -> tuple[Image.Image, np.ndarray]:
|
|
| 3112 |
except Exception as exc:
|
| 3113 |
raise HTTPException(status_code=400, detail="Upload must be a readable image.") from exc
|
| 3114 |
return image, np.array(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3115 |
|
| 3116 |
|
| 3117 |
def contour_to_polygon(contour: np.ndarray, simplify_epsilon: float) -> np.ndarray:
|
|
@@ -4169,19 +4224,21 @@ async def prepare_material_maps(
|
|
| 4169 |
|
| 4170 |
|
| 4171 |
@app.post("/viz2d/convert")
|
| 4172 |
-
async def convert_to_viz2d(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
|
| 4173 |
-
if file.content_type and not file.content_type.startswith("image/"):
|
| 4174 |
-
raise HTTPException(status_code=400, detail="Upload must be a JPG or PNG image.")
|
| 4175 |
-
|
| 4176 |
-
|
| 4177 |
-
|
| 4178 |
-
|
| 4179 |
-
|
| 4180 |
-
|
| 4181 |
-
|
| 4182 |
-
|
| 4183 |
-
|
| 4184 |
-
|
|
|
|
|
|
|
| 4185 |
|
| 4186 |
job = {
|
| 4187 |
"id": job_id,
|
|
|
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import os
|
|
|
|
| 7 |
import time
|
| 8 |
import urllib.error
|
| 9 |
import urllib.parse
|
|
|
|
| 36 |
from fastapi.middleware.cors import CORSMiddleware
|
| 37 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 38 |
from fastapi.staticfiles import StaticFiles
|
| 39 |
+
from PIL import Image, ImageOps
|
| 40 |
from transformers import (
|
| 41 |
AutoImageProcessor,
|
| 42 |
AutoModelForDepthEstimation,
|
|
|
|
| 232 |
"bundle_image_quality",
|
| 233 |
"92",
|
| 234 |
))
|
| 235 |
+
ROOM_UPLOAD_MAX_DIMENSION = max(720, int(config_value(
|
| 236 |
+
"ROOM_UPLOAD_MAX_DIMENSION",
|
| 237 |
+
"runtime",
|
| 238 |
+
"room_upload_max_dimension",
|
| 239 |
+
"1920",
|
| 240 |
+
)))
|
| 241 |
+
ROOM_UPLOAD_JPEG_QUALITY = min(95, max(50, int(config_value(
|
| 242 |
+
"ROOM_UPLOAD_JPEG_QUALITY",
|
| 243 |
+
"runtime",
|
| 244 |
+
"room_upload_jpeg_quality",
|
| 245 |
+
"85",
|
| 246 |
+
))))
|
| 247 |
DEBUG_ARTIFACTS_ENABLED = str(config_value(
|
| 248 |
"DEBUG_ARTIFACTS_ENABLED",
|
| 249 |
"runtime",
|
|
|
|
| 3123 |
except Exception as exc:
|
| 3124 |
raise HTTPException(status_code=400, detail="Upload must be a readable image.") from exc
|
| 3125 |
return image, np.array(image)
|
| 3126 |
+
|
| 3127 |
+
|
| 3128 |
+
def normalize_room_upload(contents: bytes) -> bytes:
|
| 3129 |
+
try:
|
| 3130 |
+
image = Image.open(io.BytesIO(contents))
|
| 3131 |
+
image.load()
|
| 3132 |
+
except Exception as exc:
|
| 3133 |
+
raise HTTPException(status_code=400, detail="Upload must be a readable image.") from exc
|
| 3134 |
+
|
| 3135 |
+
source_format = (image.format or "").upper()
|
| 3136 |
+
has_alpha = image.mode in {"RGBA", "LA"} or (image.mode == "P" and "transparency" in image.info)
|
| 3137 |
+
width, height = image.size
|
| 3138 |
+
largest = max(width, height)
|
| 3139 |
+
needs_resize = largest > ROOM_UPLOAD_MAX_DIMENSION
|
| 3140 |
+
try:
|
| 3141 |
+
orientation = image.getexif().get(274)
|
| 3142 |
+
except Exception:
|
| 3143 |
+
orientation = None
|
| 3144 |
+
needs_orientation = orientation not in {None, 1}
|
| 3145 |
+
if source_format == "JPEG" and not has_alpha and not needs_resize and not needs_orientation:
|
| 3146 |
+
return contents
|
| 3147 |
+
|
| 3148 |
+
image = ImageOps.exif_transpose(image)
|
| 3149 |
+
if has_alpha:
|
| 3150 |
+
alpha = image.convert("RGBA")
|
| 3151 |
+
background = Image.new("RGB", alpha.size, (255, 255, 255))
|
| 3152 |
+
background.paste(alpha, mask=alpha.getchannel("A"))
|
| 3153 |
+
image = background
|
| 3154 |
+
else:
|
| 3155 |
+
image = image.convert("RGB")
|
| 3156 |
+
|
| 3157 |
+
width, height = image.size
|
| 3158 |
+
largest = max(width, height)
|
| 3159 |
+
if largest > ROOM_UPLOAD_MAX_DIMENSION:
|
| 3160 |
+
scale = ROOM_UPLOAD_MAX_DIMENSION / float(largest)
|
| 3161 |
+
target_size = (
|
| 3162 |
+
max(1, int(round(width * scale))),
|
| 3163 |
+
max(1, int(round(height * scale))),
|
| 3164 |
+
)
|
| 3165 |
+
image = image.resize(target_size, Image.Resampling.LANCZOS)
|
| 3166 |
+
|
| 3167 |
+
buffer = io.BytesIO()
|
| 3168 |
+
image.save(buffer, format="JPEG", quality=ROOM_UPLOAD_JPEG_QUALITY, optimize=True)
|
| 3169 |
+
return buffer.getvalue()
|
| 3170 |
|
| 3171 |
|
| 3172 |
def contour_to_polygon(contour: np.ndarray, simplify_epsilon: float) -> np.ndarray:
|
|
|
|
| 4224 |
|
| 4225 |
|
| 4226 |
@app.post("/viz2d/convert")
|
| 4227 |
+
async def convert_to_viz2d(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
|
| 4228 |
+
if file.content_type and not file.content_type.startswith("image/"):
|
| 4229 |
+
raise HTTPException(status_code=400, detail="Upload must be a JPG or PNG image.")
|
| 4230 |
+
|
| 4231 |
+
contents = await file.read()
|
| 4232 |
+
if not contents:
|
| 4233 |
+
raise HTTPException(status_code=400, detail="Upload must not be empty.")
|
| 4234 |
+
normalized_contents = normalize_room_upload(contents)
|
| 4235 |
+
|
| 4236 |
+
job_id = uuid.uuid4().hex
|
| 4237 |
+
original_filename = file.filename
|
| 4238 |
+
content_type = "image/jpeg"
|
| 4239 |
+
ext = ".jpg"
|
| 4240 |
+
upload_path = UPLOAD_DIR / f"{job_id}{ext}"
|
| 4241 |
+
upload_path.write_bytes(normalized_contents)
|
| 4242 |
|
| 4243 |
job = {
|
| 4244 |
"id": job_id,
|