The visualization feature of the Chalo Kisaan leverages advanced AI models to transform an original farm image into a vision of the farm with agrotourism services implemented. This is achieved by first extracting a depth map from the original image, then using this depth map along with a text prompt to guide the generation of the new image that illustrates the concept of vineyard agrotourism on the farm.
!pip install transformers diffusers accelerate==0.20.3
import torch
import numpy as np
from transformers import pipeline
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, UniPCMultistepScheduler
from diffusers.utils import load_image, make_image_grid
# Load the farm image
farm_image_url = "https://thumbs.dreamstime.com/b/beautiful-indian-farm-landscape-11981336.jpg" # Replace with the your farm iamge image URL
farm_image = load_image(farm_image_url)
# Extract the depth map
depth_estimator = pipeline("depth-estimation")
def get_depth_map(image, depth_estimator):
image = depth_estimator(image)["depth"]
image = np.array(image)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
detected_map = torch.from_numpy(image).float() / 255.0
depth_map = detected_map.permute(2, 0, 1)
return depth_map
depth_map = get_depth_map(farm_image, depth_estimator).unsqueeze(0).half().to("cuda")
# Load the ControlNet model and the StableDiffusionControlNetImg2ImgPipeline
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-normal", torch_dtype=torch.float16, use_safetensors=True)
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
torch_dtype=torch.float16,
use_safetensors=True
).to("cuda")
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
prompt = "vineyard agrotourism service on the farm" # Replace with your agrotourism service name
output = pipe(
prompt,
image=farm_image,
control_image=depth_map,
).images[0]
# Display the original and generated images
make_image_grid([farm_image, output], rows=1, cols=2)