Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import noise
|
7 |
+
import io
|
8 |
+
import base64
|
9 |
+
from pydantic import BaseModel
|
10 |
+
|
11 |
+
app = FastAPI(title="Advanced Material Map Generator API")
|
12 |
+
|
13 |
+
# Request model for input
|
14 |
+
class MapRequest(BaseModel):
|
15 |
+
image_base64: str
|
16 |
+
normal_strength: float = 1.0
|
17 |
+
normal_blur: int = 5
|
18 |
+
normal_bilateral: bool = False
|
19 |
+
normal_color: float = 0.3
|
20 |
+
disp_contrast: float = 1.0
|
21 |
+
disp_noise: bool = False
|
22 |
+
disp_noise_scale: float = 0.1
|
23 |
+
disp_edge: float = 1.0
|
24 |
+
rough_invert: bool = True
|
25 |
+
rough_sharpness: float = 1.0
|
26 |
+
rough_detail: float = 0.5
|
27 |
+
rough_freq: float = 0.5
|
28 |
+
|
29 |
+
def generate_normal_map(image: np.ndarray, strength: float, blur_size: int, use_bilateral: bool, color_influence: float) -> Image.Image:
|
30 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
31 |
+
if use_bilateral:
|
32 |
+
gray = cv2.bilateralFilter(gray, 9, 75, 75)
|
33 |
+
else:
|
34 |
+
gray = cv2.GaussianBlur(gray, (blur_size, blur_size), 0)
|
35 |
+
|
36 |
+
levels = 3
|
37 |
+
normal_map = np.zeros((gray.shape[0], gray.shape[1], 3), dtype=np.float32)
|
38 |
+
for i in range(levels):
|
39 |
+
scale = 1 / (2 ** i)
|
40 |
+
resized = cv2.resize(gray, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
41 |
+
sobel_x = cv2.Scharr(resized, cv2.CV_64F, 1, 0)
|
42 |
+
sobel_y = cv2.Scharr(resized, cv2.CV_64F, 0, 1)
|
43 |
+
sobel_x = cv2.resize(sobel_x, (gray.shape[1], gray.shape[0]), interpolation=cv2.INTER_LINEAR)
|
44 |
+
sobel_y = cv2.resize(sobel_y, (gray.shape[1], gray.shape[0]), interpolation=cv2.INTER_LINEAR)
|
45 |
+
normal_map[..., 0] += sobel_x * (1.0 / levels)
|
46 |
+
normal_map[..., 1] += sobel_y * (1.0 / levels)
|
47 |
+
|
48 |
+
normal_map[..., 0] = cv2.normalize(normal_map[..., 0], None, -strength, strength, cv2.NORM_MINMAX)
|
49 |
+
normal_map[..., 1] = cv2.normalize(normal_map[..., 1], None, -strength, strength, cv2.NORM_MINMAX)
|
50 |
+
normal_map[..., 2] = 1.0
|
51 |
+
|
52 |
+
color_factor = color_influence * strength
|
53 |
+
normal_map[..., 0] += (image[..., 0] / 255.0 - 0.5) * color_factor
|
54 |
+
normal_map[..., 1] += (image[..., 1] / 255.0 - 0.5) * color_factor
|
55 |
+
|
56 |
+
norm = np.linalg.norm(normal_map, axis=2, keepdims=True)
|
57 |
+
normal_map = np.divide(normal_map, norm, out=np.zeros_like(normal_map), where=norm != 0)
|
58 |
+
normal_map = (normal_map + 1) * 127.5
|
59 |
+
normal_map = np.clip(normal_map, 0, 255).astype(np.uint8)
|
60 |
+
return Image.fromarray(normal_map)
|
61 |
+
|
62 |
+
def generate_displacement_map(image: np.ndarray, contrast: float, add_noise: bool, noise_scale: float, edge_boost: float) -> Image.Image:
|
63 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
64 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
65 |
+
img = clahe.apply(img)
|
66 |
+
img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
|
67 |
+
laplacian = cv2.Laplacian(img, cv2.CV_64F)
|
68 |
+
laplacian = cv2.convertScaleAbs(laplacian, alpha=edge_boost, beta=0)
|
69 |
+
img = cv2.addWeighted(img, 1.0, laplacian, 0.5 * edge_boost, 0)
|
70 |
+
if add_noise:
|
71 |
+
height, width = img.shape
|
72 |
+
noise_map = np.zeros((height, width), dtype=np.float32)
|
73 |
+
for y in range(height):
|
74 |
+
for x in range(width):
|
75 |
+
noise_map[y, x] = noise.pnoise2(x / 50.0, y / 50.0, octaves=6) * noise_scale * 255
|
76 |
+
img = cv2.add(img, noise_map.astype(np.uint8))
|
77 |
+
return Image.fromarray(img)
|
78 |
+
|
79 |
+
def generate_roughness_map(image: np.ndarray, invert: bool, sharpness: float, detail_boost: float, frequency_weight: float) -> Image.Image:
|
80 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
81 |
+
low_freq = cv2.bilateralFilter(img, 9, 75, 75)
|
82 |
+
high_freq = cv2.subtract(img, low_freq)
|
83 |
+
img = cv2.addWeighted(low_freq, 1.0 - frequency_weight, high_freq, frequency_weight, 0)
|
84 |
+
if invert:
|
85 |
+
img = 255 - img
|
86 |
+
blurred = cv2.GaussianBlur(img, (5, 5), 0)
|
87 |
+
img = cv2.addWeighted(img, 1.0 + sharpness, blurred, -sharpness, 0)
|
88 |
+
img = cv2.addWeighted(img, 1.0 + detail_boost, blurred, -detail_boost, 0)
|
89 |
+
return Image.fromarray(img)
|
90 |
+
|
91 |
+
def image_to_base64(img: Image.Image) -> str:
|
92 |
+
buffered = io.BytesIO()
|
93 |
+
img.save(buffered, format="PNG")
|
94 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
95 |
+
|
96 |
+
@app.post("/generate_maps/")
|
97 |
+
async def generate_maps(request: MapRequest):
|
98 |
+
try:
|
99 |
+
# Decode base64 image
|
100 |
+
image_bytes = base64.b64decode(request.image_base64)
|
101 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
102 |
+
img_array = np.array(image)
|
103 |
+
|
104 |
+
# Generate maps
|
105 |
+
normal_map = generate_normal_map(
|
106 |
+
img_array, request.normal_strength, request.normal_blur,
|
107 |
+
request.normal_bilateral, request.normal_color
|
108 |
+
)
|
109 |
+
displacement_map = generate_displacement_map(
|
110 |
+
img_array, request.disp_contrast, request.disp_noise,
|
111 |
+
request.disp_noise_scale, request.disp_edge
|
112 |
+
)
|
113 |
+
roughness_map = generate_roughness_map(
|
114 |
+
img_array, request.rough_invert, request.rough_sharpness,
|
115 |
+
request.rough_detail, request.rough_freq
|
116 |
+
)
|
117 |
+
|
118 |
+
# Convert to base64
|
119 |
+
normal_base64 = image_to_base64(normal_map)
|
120 |
+
displacement_base64 = image_to_base64(displacement_map)
|
121 |
+
roughness_base64 = image_to_base64(roughness_map)
|
122 |
+
|
123 |
+
return JSONResponse(content={
|
124 |
+
"status": "success",
|
125 |
+
"normal_map": normal_base64,
|
126 |
+
"displacement_map": displacement_base64,
|
127 |
+
"roughness_map": roughness_base64
|
128 |
+
})
|
129 |
+
|
130 |
+
except Exception as e:
|
131 |
+
raise HTTPException(status_code=500, detail=str(e))
|