Burman-AI commited on
Commit
7164cf5
·
verified ·
1 Parent(s): 0280495

Update main_code_script.py

Browse files
Files changed (1) hide show
  1. main_code_script.py +14 -19
main_code_script.py CHANGED
@@ -54,9 +54,9 @@ def segment_clothing(image, results): #Added result
54
  mask_img = Image.fromarray(binary_mask).convert("L")
55
  return mask_img
56
  # --- 3. Image Inpainting (Replacing Clothing - using Stable Diffusion Inpainting) ---
57
- def inpaint_clothing(image, mask_img, garment_image_path, device="cuda" if torch.cuda.is_available() else "cpu"): # Changed input
58
  """
59
- Replaces the clothing region in the image with the uploaded garment image,
60
  using Stable Diffusion Inpainting.
61
  """
62
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
@@ -64,23 +64,18 @@ def inpaint_clothing(image, mask_img, garment_image_path, device="cuda" if torch
64
  torch_dtype=torch.float16
65
  )
66
  pipe = pipe.to(device)
67
- # Resize the image and mask to the same size. Important for inpainting.
68
- image = image.resize((512, 512))
69
- mask_img = mask_img.resize((512, 512))
70
-
71
- # Load the garment image
72
- garment_image = Image.open(garment_image_path).convert("RGB")
73
- garment_image = garment_image.resize((512,512)) # Resize if necessary
74
-
75
- # Inpaint using the garment image as a guide (This part might need further refinement)
76
- # A simple approach is to use the garment image in the prompt.
77
- # More advanced techniques might involve using the garment image as
78
- # a style reference or directly manipulating the latent space.
79
- prompt = f"A photo of a person wearing the uploaded garment"
80
  image = pipe(prompt=prompt, image=image, mask_image=mask_img).images[0]
 
 
81
  return image
82
  # --- 4. Main Function (Putting it all together) ---
83
- def change_clothing(image_path, garment_image_path): # Changed input
84
  """
85
  Main function to change the clothing in an image.
86
  """
@@ -94,13 +89,13 @@ def change_clothing(image_path, garment_image_path): # Changed input
94
  # 3. Segment the clothing
95
  mask_img = segment_clothing(image, results)
96
  # 4. Inpaint the clothing
97
- modified_image = inpaint_clothing(image, mask_img, garment_image_path) # Changed input
98
  return modified_image
99
  # --- Example Usage ---
100
  if __name__ == "__main__":
101
  input_image_path = "person.jpg" # Replace with your image
102
- garment_image_path = "garment.jpg" # Replace with your garment image
103
- modified_image = change_clothing(input_image_path, garment_image_path)
104
  if modified_image:
105
  modified_image.save("modified_image.jpg")
106
  print("Clothing changed and saved to modified_image.jpg")
 
54
  mask_img = Image.fromarray(binary_mask).convert("L")
55
  return mask_img
56
  # --- 3. Image Inpainting (Replacing Clothing - using Stable Diffusion Inpainting) ---
57
+ def inpaint_clothing(image, mask_img, clothing_prompt, device="cuda" if torch.cuda.is_available() else "cpu"):
58
  """
59
+ Replaces the clothing region in the image with new clothing based on a text prompt,
60
  using Stable Diffusion Inpainting.
61
  """
62
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
 
64
  torch_dtype=torch.float16
65
  )
66
  pipe = pipe.to(device)
67
+ # Resize the image and mask to a smaller size for faster inpainting
68
+ width, height = image.size
69
+ inpainted_size = (256, 256) # Smaller size for faster inpainting
70
+ image = image.resize(inpainted_size)
71
+ mask_img = mask_img.resize(inpainted_size)
72
+ prompt = f"A photo of a person wearing {clothing_prompt}" #Add style or detail
 
 
 
 
 
 
 
73
  image = pipe(prompt=prompt, image=image, mask_image=mask_img).images[0]
74
+ # Resize back to the original size (or a desired output size)
75
+ image = image.resize((width, height)) # Or resize to a target output size
76
  return image
77
  # --- 4. Main Function (Putting it all together) ---
78
+ def change_clothing(image_path, clothing_prompt):
79
  """
80
  Main function to change the clothing in an image.
81
  """
 
89
  # 3. Segment the clothing
90
  mask_img = segment_clothing(image, results)
91
  # 4. Inpaint the clothing
92
+ modified_image = inpaint_clothing(image, mask_img, clothing_prompt)
93
  return modified_image
94
  # --- Example Usage ---
95
  if __name__ == "__main__":
96
  input_image_path = "person.jpg" # Replace with your image
97
+ clothing_description = "a red leather jacket" # Replace with desired clothing
98
+ modified_image = change_clothing(input_image_path, clothing_description)
99
  if modified_image:
100
  modified_image.save("modified_image.jpg")
101
  print("Clothing changed and saved to modified_image.jpg")