ahmetyaylalioglu commited on
Commit
0685b0e
·
verified ·
1 Parent(s): e2a5dbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -3,6 +3,7 @@ from PIL import Image
3
  import numpy as np
4
  from transformers import SamModel, SamProcessor
5
  from diffusers import AutoPipelineForInpainting
 
6
  import torch
7
 
8
  # Check if GPU is available, otherwise use CPU
@@ -15,16 +16,14 @@ model = SamModel.from_pretrained(model_name).to(device)
15
  processor = SamProcessor.from_pretrained(model_name)
16
 
17
  def mask_to_rgb(mask):
18
- """ Convert binary mask to RGB with transparency for the background. """
19
  bg_transparent = np.zeros(mask.shape + (4,), dtype=np.uint8)
20
- bg_transparent[mask == 1] = [0, 255, 0, 127] # Green mask with some transparency
21
  return bg_transparent
22
 
23
- def get_processed_inputs(image, annotation):
24
- """ Process the input image and annotated drawing using SAM model and processor. """
25
- mask = np.zeros(image.size, dtype=np.uint8)
26
- mask[annotation[:,:,3] > 128] = 1 # Assume drawing is in alpha channel of RGBA
27
- inputs = processor(images=image, return_tensors="pt").to(device)
28
  with torch.no_grad():
29
  outputs = model(**inputs)
30
  masks = processor.image_processor.post_process_masks(
@@ -36,7 +35,6 @@ def get_processed_inputs(image, annotation):
36
  return ~best_mask.cpu().numpy()
37
 
38
  def inpaint(raw_image, input_mask, prompt, negative_prompt=None, seed=74294536, cfgs=7):
39
- """ Inpaint the masked area in the image using a text prompt and an inpainting pipeline. """
40
  mask_image = Image.fromarray(input_mask)
41
  rand_gen = torch.manual_seed(seed)
42
  pipeline = AutoPipelineForInpainting.from_pretrained(
@@ -55,10 +53,9 @@ def inpaint(raw_image, input_mask, prompt, negative_prompt=None, seed=74294536,
55
  ).images[0]
56
  return image
57
 
58
- def gradio_interface(image, annotation, positive_prompt, negative_prompt):
59
- """ Gradio interface function to handle image, annotated drawing, and prompts. """
60
  raw_image = Image.fromarray(image).convert("RGB").resize((512, 512))
61
- mask = get_processed_inputs(raw_image, annotation)
62
  processed_image = inpaint(raw_image, mask, positive_prompt, negative_prompt)
63
  return processed_image, mask_to_rgb(mask)
64
 
@@ -66,7 +63,7 @@ iface = gr.Interface(
66
  fn=gradio_interface,
67
  inputs=[
68
  gr.Image(type="numpy", label="Input Image"),
69
- gr.Image(tool="editor", label="Draw on the image", output="png", shape=(512, 512)),
70
  gr.Textbox(label="Positive Prompt", placeholder="Enter positive prompt here"),
71
  gr.Textbox(label="Negative Prompt", placeholder="Enter negative prompt here")
72
  ],
@@ -75,7 +72,7 @@ iface = gr.Interface(
75
  gr.Image(label="Segmentation Mask")
76
  ],
77
  title="Interactive Image Inpainting",
78
- description="Draw on the image to select areas for segmentation, provide prompts, and see the inpainted result."
79
  )
80
 
81
  iface.launch(share=True)
 
3
  import numpy as np
4
  from transformers import SamModel, SamProcessor
5
  from diffusers import AutoPipelineForInpainting
6
+ from diffusers.models.autoencoders.vq_model import VQEncoderOutput, VQModel
7
  import torch
8
 
9
  # Check if GPU is available, otherwise use CPU
 
16
  processor = SamProcessor.from_pretrained(model_name)
17
 
18
  def mask_to_rgb(mask):
 
19
  bg_transparent = np.zeros(mask.shape + (4,), dtype=np.uint8)
20
+ bg_transparent[mask == 1] = [0, 255, 0, 127]
21
  return bg_transparent
22
 
23
+ def get_processed_inputs(image, points_str):
24
+ points = [list(map(int, point.split(','))) for point in points_str.split()]
25
+ input_points = [points]
26
+ inputs = processor(image, input_points=input_points, return_tensors="pt").to(device)
 
27
  with torch.no_grad():
28
  outputs = model(**inputs)
29
  masks = processor.image_processor.post_process_masks(
 
35
  return ~best_mask.cpu().numpy()
36
 
37
  def inpaint(raw_image, input_mask, prompt, negative_prompt=None, seed=74294536, cfgs=7):
 
38
  mask_image = Image.fromarray(input_mask)
39
  rand_gen = torch.manual_seed(seed)
40
  pipeline = AutoPipelineForInpainting.from_pretrained(
 
53
  ).images[0]
54
  return image
55
 
56
+ def gradio_interface(image, points, positive_prompt, negative_prompt):
 
57
  raw_image = Image.fromarray(image).convert("RGB").resize((512, 512))
58
+ mask = get_processed_inputs(raw_image, points)
59
  processed_image = inpaint(raw_image, mask, positive_prompt, negative_prompt)
60
  return processed_image, mask_to_rgb(mask)
61
 
 
63
  fn=gradio_interface,
64
  inputs=[
65
  gr.Image(type="numpy", label="Input Image"),
66
+ gr.Textbox(label="Points (format: x1,y1 x2,y2 ...)", placeholder="e.g., 100,100 200,200"),
67
  gr.Textbox(label="Positive Prompt", placeholder="Enter positive prompt here"),
68
  gr.Textbox(label="Negative Prompt", placeholder="Enter negative prompt here")
69
  ],
 
72
  gr.Image(label="Segmentation Mask")
73
  ],
74
  title="Interactive Image Inpainting",
75
+ description="Enter points as 'x1,y1 x2,y2 ...' for segmentation, provide prompts, and see the inpainted result."
76
  )
77
 
78
  iface.launch(share=True)