Using bounding Boxes in Inpainting
I am building an application to detect an object inside an image and replace that image with a different image. I have a dataset of tables and sofas. I built a YOLO object detection model and used the segment anything model, to create the mask. Now I wish to pass that mask or bounding boxes input to inpainting to change that Image with some other image. I want to pass the coordinates and the new image instead of prompts. Below is my code: -import numpy as np
import torch
import matplotlib.pyplot as plt
import cv2
import supervision as sv
from ultralytics import YOLO
from segment_anything import sam_model_registry, SamPredictor, SamAutomaticMaskGenerator
import matplotlib.patches as patches
image_path = "C:\\Users\\spx016\\Segement Anything Meta\\furniture_data\\sofa1.jpeg"
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
model = YOLO('yolov8n.pt') # using  simple yolo model for now, I will use my trained model later. 
results = model.predict(image, conf = 0.3)
for result in results:
    boxes = result.boxes

bbox=boxes.xyxy.tolist()[0]
sam_checkpoint = "sam_vit_h_4b8939.pth"
model_type = "vit_h"
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
predictor = SamPredictor(sam)
predictor.set_image(image)
def show_mask(mask, ax, random_color=False):
    if random_color:
        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
    else:
        color = np.array([30/255, 144/255, 255/255, 0.6])
    h, w = mask.shape[-2:]
    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
    ax.imshow(mask_image)

def show_points(coords, labels, ax, marker_size=375):
    pos_points = coords[labels==1]
    neg_points = coords[labels==0]
    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)
    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)

def show_box(box, ax):
    x0, y0 = box[0], box[1]
    w, h = box[2] - box[0], box[3] - box[1]
    ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))
input_box = np.array(bbox)

masks, _, _ = predictor.predict(
    point_coords=None,
    point_labels=None,
    box=input_box[None, :],
    multimask_output=False,
)
image_path2 = "C:\\Users\\spx016\\Segement Anything Meta\\furniture_data\\sofa3.jpeg" #new image i wish to replace with origional one. 
image2 = cv2.imread(image_path2)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
image2 = cv2.resize(image2, (image.shape[1], image.shape[0]))
segmentation_mask = masks[0]
binary_mask = np.where(segmentation_mask > 0.5, 1, 0)
white_background = np.ones_like(image) * 255
new_image = white_background * (1 - binary_mask[..., np.newaxis]) + image2 * binary_mask[..., np.newaxis]this code can make changes but not properly as Inpaint. How can I do this using inpaint so that the image quality remains the same and other products in the image remain as they are?PS: For testing any furniture image can be chosen as I don’t find anything to upload the image here.