File size: 4,199 Bytes
9869d0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import torch
import numpy as np
import cv2
from PIL import Image
from diffusers import (
    StableDiffusionControlNetPipeline, 
    ControlNetModel, 
    UniPCMultistepScheduler, 
    AutoencoderKL,
    StableDiffusionControlNetImg2ImgPipeline
)

# 1. Load both ControlNet models
controlnets = [
    ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float16),
    ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny", torch_dtype=torch.float16)
]

# 2. Load the base pipeline with the high-quality VAE
vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16)
# pipe = StableDiffusionControlNetPipeline.from_pretrained(
#     "runwayml/stable-diffusion-v1-5", 
#     controlnet=controlnets, 
#     vae=vae,
#     torch_dtype=torch.float16
# )
# pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)

pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", 
    controlnet=controlnets, 
    vae=vae,
    torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

# 3. Prepare Control Images
init_image = Image.open("input/isaacsim.jpg").convert("RGB")
# A. Depth Map Processing (Invert + CLAHE)
depth_map_np = np.load("input/depth.npy")
depth_map_np = np.clip(depth_map_np, 0, 20)
depth_map_normalized = (depth_map_np - np.min(depth_map_np)) / (np.max(depth_map_np) - np.min(depth_map_np))
depth_map_inverted = ( (1.0 - depth_map_normalized) * 255 ).astype(np.uint8)
clahe = cv2.createCLAHE(clipLimit=2.5, tileGridSize=(8,8))
depth_final = Image.fromarray(clahe.apply(depth_map_inverted)).convert("RGB")

# B. Canny Edge Processing (From your Isaac Sim RGB image)
isaac_rgb = cv2.imread("input/isaacsim.jpg")
# Detect edges (Thresholds 100, 200 are usually good for clean synthetic renders)
canny_edges = cv2.Canny(isaac_rgb, 100, 200)
canny_final = Image.fromarray(canny_edges).convert("RGB")

# 4. Prompt based on Rosario/Real images
prompt = ( 
        "A photorealistic high-angle shot of a soybean field in Brazil." 
        "real image, agriculture scene, natural lighting, high detail, 4k resolution," 
        "field image," 
        "Vibrant green leaves with detailed veins," 
        "haze scattering effect" 
        "leaves with natural aspects," 
        "dark brown cloddy soil with organic dirt textures and pebbles,"
        )
negative_prompt = "cartoon, 3d render, plastic, smooth floor, blurry, low quality"

# 5. Run Inference
# We pass a list of images to match our list of ControlNets
# conditioning_scale [0.8, 0.4] tells the AI: "Prioritize Depth structure, but use Canny for the sharp lines"
generator = torch.Generator(device="cuda").manual_seed(42)

# output_image = pipe(
#     prompt=prompt,
#     negative_prompt=negative_prompt,
#     image=[depth_final, canny_final],
#     num_inference_steps=10,
#     guidance_scale=4.2,
#     strength=0.27,
#     controlnet_conditioning_scale=[0.8, 0.4], 
#     generator=generator
# ).images[0]

output_image = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    image=init_image,           # The "Starting" image for light/color
    control_image=[depth_final, canny_final], # The "Guides" for geometry
    num_inference_steps=50,     # Higher steps = better texture
    strength=0.42,               # 0.7 allows significant texture replacement
    guidance_scale=7.7,         # Higher guidance forces it to follow the prompt
    controlnet_conditioning_scale=[0.9, 0.9],  # Both ControlNets are important for structure and details
    generator=generator
).images[0]

output_image.save("soybean_translated.png")

# Visualize the results, input, depth, canny, and output
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(20, 5))
axs[0,0].imshow(cv2.cvtColor(isaac_rgb, cv2.COLOR_BGR2RGB))
axs[0,0].set_title("Isaac Sim RGB")
axs[0,0].axis("off")
axs[1,0].imshow(depth_final)
axs[1,0].set_title("Depth Map (Inverted + CLAHE)")
axs[1,0].axis("off")
axs[1,1].imshow(canny_final)
axs[1,1].set_title("Canny Edges")
axs[1,1].axis("off")
axs[0,1].imshow(output_image)
axs[0,1].set_title("Generated Image")
axs[0,1].axis("off")
plt.show()