Spaces:
Sleeping
Sleeping
Update main_code_script.py
Browse files- main_code_script.py +36 -51
main_code_script.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
# Install necessary libraries (in your requirements.txt)
|
| 2 |
# pillow opencv-python transformers mediapipe diffusers accelerate transformers
|
| 3 |
# Example install command: pip install pillow opencv-python transformers mediapipe diffusers accelerate transformers
|
| 4 |
-
|
| 5 |
from PIL import Image
|
| 6 |
import cv2
|
| 7 |
import mediapipe as mp
|
|
@@ -9,7 +8,6 @@ import numpy as np
|
|
| 9 |
from transformers import pipeline
|
| 10 |
from diffusers import StableDiffusionInpaintPipeline
|
| 11 |
import torch
|
| 12 |
-
|
| 13 |
# --- 1. Pose Estimation (using Mediapipe) ---
|
| 14 |
def estimate_pose(image_path):
|
| 15 |
"""Detects the pose of a person in an image using Mediapipe.
|
|
@@ -21,55 +19,44 @@ def estimate_pose(image_path):
|
|
| 21 |
"""
|
| 22 |
mp_drawing = mp.solutions.drawing_utils
|
| 23 |
mp_pose = mp.solutions.pose
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
raise FileNotFoundError(f"Could not open image: {image_path}")
|
| 33 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 34 |
-
results = pose.process(image_rgb)
|
| 35 |
-
|
| 36 |
-
if results.pose_landmarks:
|
| 37 |
-
# Example: Draw the pose landmarks on the image (for visualization)
|
| 38 |
-
annotated_image = image.copy()
|
| 39 |
-
mp_drawing.draw_landmarks(
|
| 40 |
-
annotated_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 41 |
-
#cv2.imwrite("pose_annotated.jpg", annotated_image) # Save annotated image
|
| 42 |
-
#return results.pose_landmarks.landmark
|
| 43 |
-
# Return the landmarks
|
| 44 |
-
return results, image # Return the entire result
|
| 45 |
-
else:
|
| 46 |
-
raise ValueError("No pose detected in the image.")
|
| 47 |
-
except Exception as e:
|
| 48 |
-
raise RuntimeError(f"Error processing image: {e}")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# --- 2. Clothing Segmentation (Example - using a placeholder function) ---
|
| 51 |
def segment_clothing(image, results): #Added result
|
| 52 |
"""Segments the clothing region in the image.
|
| 53 |
-
|
| 54 |
segmentation model.
|
| 55 |
"""
|
| 56 |
-
|
| 57 |
# 1. Create a mask where the person is present.
|
| 58 |
segmentation_mask = results.segmentation_mask
|
| 59 |
threshold = 0.5 # Adjust this threshold as needed.
|
| 60 |
-
|
| 61 |
# Threshold the segmentation mask to create a binary mask.
|
| 62 |
binary_mask = (segmentation_mask > threshold).astype(np.uint8) * 255
|
| 63 |
-
|
| 64 |
# Convert binary mask to a PIL Image
|
| 65 |
mask_img = Image.fromarray(binary_mask).convert("L")
|
| 66 |
-
|
| 67 |
return mask_img
|
| 68 |
-
|
| 69 |
# --- 3. Image Inpainting (Replacing Clothing - using Stable Diffusion Inpainting) ---
|
| 70 |
-
def inpaint_clothing(image, mask_img,
|
| 71 |
"""
|
| 72 |
-
Replaces the clothing region in the image with
|
| 73 |
using Stable Diffusion Inpainting.
|
| 74 |
"""
|
| 75 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
|
@@ -77,45 +64,43 @@ def inpaint_clothing(image, mask_img, clothing_prompt, device="cuda" if torch.cu
|
|
| 77 |
torch_dtype=torch.float16
|
| 78 |
)
|
| 79 |
pipe = pipe.to(device)
|
| 80 |
-
|
| 81 |
# Resize the image and mask to the same size. Important for inpainting.
|
| 82 |
image = image.resize((512, 512))
|
| 83 |
mask_img = mask_img.resize((512, 512))
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
image = pipe(prompt=prompt, image=image, mask_image=mask_img).images[0]
|
| 87 |
return image
|
| 88 |
-
|
| 89 |
# --- 4. Main Function (Putting it all together) ---
|
| 90 |
-
def change_clothing(image_path,
|
| 91 |
"""
|
| 92 |
Main function to change the clothing in an image.
|
| 93 |
"""
|
| 94 |
# 1. Load the image
|
| 95 |
image = Image.open(image_path).convert("RGB")
|
| 96 |
-
|
| 97 |
# 2. Estimate the pose
|
| 98 |
results, cv2_image = estimate_pose(image_path)
|
| 99 |
if results is None:
|
| 100 |
print("No pose detected.")
|
| 101 |
return None
|
| 102 |
-
|
| 103 |
# 3. Segment the clothing
|
| 104 |
mask_img = segment_clothing(image, results)
|
| 105 |
-
|
| 106 |
# 4. Inpaint the clothing
|
| 107 |
-
modified_image = inpaint_clothing(image, mask_img,
|
| 108 |
-
|
| 109 |
return modified_image
|
| 110 |
-
|
| 111 |
-
|
| 112 |
# --- Example Usage ---
|
| 113 |
if __name__ == "__main__":
|
| 114 |
input_image_path = "person.jpg" # Replace with your image
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
modified_image = change_clothing(input_image_path, clothing_description)
|
| 118 |
-
|
| 119 |
if modified_image:
|
| 120 |
modified_image.save("modified_image.jpg")
|
| 121 |
print("Clothing changed and saved to modified_image.jpg")
|
|
|
|
| 1 |
# Install necessary libraries (in your requirements.txt)
|
| 2 |
# pillow opencv-python transformers mediapipe diffusers accelerate transformers
|
| 3 |
# Example install command: pip install pillow opencv-python transformers mediapipe diffusers accelerate transformers
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import cv2
|
| 6 |
import mediapipe as mp
|
|
|
|
| 8 |
from transformers import pipeline
|
| 9 |
from diffusers import StableDiffusionInpaintPipeline
|
| 10 |
import torch
|
|
|
|
| 11 |
# --- 1. Pose Estimation (using Mediapipe) ---
|
| 12 |
def estimate_pose(image_path):
|
| 13 |
"""Detects the pose of a person in an image using Mediapipe.
|
|
|
|
| 19 |
"""
|
| 20 |
mp_drawing = mp.solutions.drawing_utils
|
| 21 |
mp_pose = mp.solutions.pose
|
| 22 |
+
with mp_pose.Pose(
|
| 23 |
+
static_image_mode=True,
|
| 24 |
+
model_complexity=2,
|
| 25 |
+
enable_segmentation=True,
|
| 26 |
+
min_detection_confidence=0.5) as pose:
|
| 27 |
+
image = cv2.imread(image_path)
|
| 28 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 29 |
+
results = pose.process(image_rgb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
if results.pose_landmarks:
|
| 32 |
+
# Example: Draw the pose landmarks on the image (for visualization)
|
| 33 |
+
annotated_image = image.copy()
|
| 34 |
+
mp_drawing.draw_landmarks(
|
| 35 |
+
annotated_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 36 |
+
#cv2.imwrite("pose_annotated.jpg", annotated_image) # Save annotated image
|
| 37 |
+
#return results.pose_landmarks.landmark
|
| 38 |
+
# Return the landmarks
|
| 39 |
+
return results, image # Return the entire result
|
| 40 |
+
else:
|
| 41 |
+
return None, None # or raise an exception
|
| 42 |
# --- 2. Clothing Segmentation (Example - using a placeholder function) ---
|
| 43 |
def segment_clothing(image, results): #Added result
|
| 44 |
"""Segments the clothing region in the image.
|
| 45 |
+
This is a simplified example. In reality, you would use a pre-trained
|
| 46 |
segmentation model.
|
| 47 |
"""
|
|
|
|
| 48 |
# 1. Create a mask where the person is present.
|
| 49 |
segmentation_mask = results.segmentation_mask
|
| 50 |
threshold = 0.5 # Adjust this threshold as needed.
|
|
|
|
| 51 |
# Threshold the segmentation mask to create a binary mask.
|
| 52 |
binary_mask = (segmentation_mask > threshold).astype(np.uint8) * 255
|
|
|
|
| 53 |
# Convert binary mask to a PIL Image
|
| 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 |
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 |
"""
|
| 87 |
# 1. Load the image
|
| 88 |
image = Image.open(image_path).convert("RGB")
|
|
|
|
| 89 |
# 2. Estimate the pose
|
| 90 |
results, cv2_image = estimate_pose(image_path)
|
| 91 |
if results is None:
|
| 92 |
print("No pose detected.")
|
| 93 |
return None
|
|
|
|
| 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")
|