tejani commited on
Commit
1e71b17
·
verified ·
1 Parent(s): 8566f9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -32,7 +32,7 @@ def generate_normal_map(image, strength=1.0, blur_size=5, use_bilateral=False, c
32
  normal_map[..., 1] = cv2.normalize(normal_map[..., 1], None, -strength, strength, cv2.NORM_MINMAX)
33
  normal_map[..., 2] = 1.0
34
 
35
- # Add color influence (e.g., red affects X, green affects Y)
36
  color_factor = color_influence * strength
37
  normal_map[..., 0] += (img[..., 0] / 255.0 - 0.5) * color_factor
38
  normal_map[..., 1] += (img[..., 1] / 255.0 - 0.5) * color_factor
@@ -44,18 +44,20 @@ def generate_normal_map(image, strength=1.0, blur_size=5, use_bilateral=False, c
44
  normal_map = np.clip(normal_map, 0, 255).astype(np.uint8)
45
  return Image.fromarray(normal_map)
46
 
47
- # Advanced Displacement Map with adaptive thresholding and edge enhancement
48
  def generate_displacement_map(image, contrast=1.0, add_noise=False, noise_scale=0.1, edge_boost=1.0):
49
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
50
- # Adaptive histogram equalization for better contrast
51
  clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
52
  img = clahe.apply(img)
53
  # Adjust contrast
54
  img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
55
  # Edge enhancement with Laplacian
56
  laplacian = cv2.Laplacian(img, cv2.CV_64F)
57
- img = cv2.addWeighted(img, 1.0, laplacian, edge_boost, 0)
58
- img = np.clip(img, 0, 255).astype(np.uint8)
 
 
59
  # Optional Perlin noise
60
  if add_noise:
61
  height, width = img.shape
@@ -66,10 +68,10 @@ def generate_displacement_map(image, contrast=1.0, add_noise=False, noise_scale=
66
  img = cv2.add(img, noise_map.astype(np.uint8))
67
  return Image.fromarray(img)
68
 
69
- # Advanced Roughness Map with frequency separation and texture analysis
70
  def generate_roughness_map(image, invert=True, sharpness=1.0, detail_boost=0.5, frequency_weight=0.5):
71
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
72
- # Frequency separation: Low-frequency (smooth) and high-frequency (detail)
73
  low_freq = cv2.bilateralFilter(img, 9, 75, 75)
74
  high_freq = cv2.subtract(img, low_freq)
75
  # Combine with weight
@@ -83,7 +85,7 @@ def generate_roughness_map(image, invert=True, sharpness=1.0, detail_boost=0.5,
83
  img = cv2.addWeighted(img, 1.0 + detail_boost, blurred, -detail_boost, 0)
84
  return Image.fromarray(img)
85
 
86
- # Process function with advanced parameters
87
  def process_image(input_image, normal_strength=1.0, normal_blur=5, normal_bilateral=False, normal_color=0.3,
88
  disp_contrast=1.0, disp_noise=False, disp_noise_scale=0.1, disp_edge=1.0,
89
  rough_invert=True, rough_sharpness=1.0, rough_detail=0.5, rough_freq=0.5):
@@ -95,7 +97,7 @@ def process_image(input_image, normal_strength=1.0, normal_blur=5, normal_bilate
95
  detail_boost=rough_detail, frequency_weight=rough_freq)
96
  return normal_map, displacement_map, roughness_map
97
 
98
- # Gradio Interface with advanced controls
99
  interface = gr.Interface(
100
  fn=process_image,
101
  inputs=[
 
32
  normal_map[..., 1] = cv2.normalize(normal_map[..., 1], None, -strength, strength, cv2.NORM_MINMAX)
33
  normal_map[..., 2] = 1.0
34
 
35
+ # Add color influence
36
  color_factor = color_influence * strength
37
  normal_map[..., 0] += (img[..., 0] / 255.0 - 0.5) * color_factor
38
  normal_map[..., 1] += (img[..., 1] / 255.0 - 0.5) * color_factor
 
44
  normal_map = np.clip(normal_map, 0, 255).astype(np.uint8)
45
  return Image.fromarray(normal_map)
46
 
47
+ # Advanced Displacement Map with fixed type handling
48
  def generate_displacement_map(image, contrast=1.0, add_noise=False, noise_scale=0.1, edge_boost=1.0):
49
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
50
+ # Adaptive histogram equalization
51
  clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
52
  img = clahe.apply(img)
53
  # Adjust contrast
54
  img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
55
  # Edge enhancement with Laplacian
56
  laplacian = cv2.Laplacian(img, cv2.CV_64F)
57
+ # Convert Laplacian to uint8 after scaling and clipping
58
+ laplacian = cv2.convertScaleAbs(laplacian, alpha=edge_boost, beta=0)
59
+ # Ensure both arrays are uint8 before blending
60
+ img = cv2.addWeighted(img, 1.0, laplacian, 0.5 * edge_boost, 0) # Reduced weight for stability
61
  # Optional Perlin noise
62
  if add_noise:
63
  height, width = img.shape
 
68
  img = cv2.add(img, noise_map.astype(np.uint8))
69
  return Image.fromarray(img)
70
 
71
+ # Advanced Roughness Map with frequency separation
72
  def generate_roughness_map(image, invert=True, sharpness=1.0, detail_boost=0.5, frequency_weight=0.5):
73
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
74
+ # Frequency separation
75
  low_freq = cv2.bilateralFilter(img, 9, 75, 75)
76
  high_freq = cv2.subtract(img, low_freq)
77
  # Combine with weight
 
85
  img = cv2.addWeighted(img, 1.0 + detail_boost, blurred, -detail_boost, 0)
86
  return Image.fromarray(img)
87
 
88
+ # Process function
89
  def process_image(input_image, normal_strength=1.0, normal_blur=5, normal_bilateral=False, normal_color=0.3,
90
  disp_contrast=1.0, disp_noise=False, disp_noise_scale=0.1, disp_edge=1.0,
91
  rough_invert=True, rough_sharpness=1.0, rough_detail=0.5, rough_freq=0.5):
 
97
  detail_boost=rough_detail, frequency_weight=rough_freq)
98
  return normal_map, displacement_map, roughness_map
99
 
100
+ # Gradio Interface
101
  interface = gr.Interface(
102
  fn=process_image,
103
  inputs=[