Burman-AI commited on
Commit
44f36dd
·
verified ·
1 Parent(s): 7ba99f3

Upload code.py

Browse files
Files changed (1) hide show
  1. code.py +122 -0
code.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
8
+ 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.
16
+
17
+ Args:
18
+ image_path: Path to the input image.
19
+
20
+ Returns:
21
+ A list of landmarks (x, y, visibility) or None if no pose is detected.
22
+ """
23
+ mp_drawing = mp.solutions.drawing_utils
24
+ mp_pose = mp.solutions.pose
25
+
26
+ with mp_pose.Pose(
27
+ static_image_mode=True,
28
+ model_complexity=2,
29
+ enable_segmentation=True,
30
+ min_detection_confidence=0.5) as pose:
31
+
32
+ image = cv2.imread(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
+
42
+ #cv2.imwrite("pose_annotated.jpg", annotated_image) # Save annotated image
43
+ #return results.pose_landmarks.landmark # Return the landmarks
44
+ return results, image # Return the entire result
45
+
46
+ else:
47
+ return None, None # or raise an exception
48
+
49
+ # --- 2. Clothing Segmentation (Example - using a placeholder function) ---
50
+ def segment_clothing(image, results): #Added result
51
+ """Segments the clothing region in the image.
52
+ This is a simplified example. In reality, you would use a pre-trained
53
+ segmentation model.
54
+ """
55
+
56
+ # 1. Create a mask where the person is present.
57
+ segmentation_mask = results.segmentation_mask
58
+ threshold = 0.5 # Adjust this threshold as needed.
59
+
60
+ # Threshold the segmentation mask to create a binary mask.
61
+ binary_mask = (segmentation_mask > threshold).astype(np.uint8) * 255
62
+
63
+ # Convert binary mask to a PIL Image
64
+ mask_img = Image.fromarray(binary_mask).convert("L")
65
+
66
+ return mask_img
67
+
68
+ # --- 3. Image Inpainting (Replacing Clothing - using Stable Diffusion Inpainting) ---
69
+ def inpaint_clothing(image, mask_img, clothing_prompt, device="cuda" if torch.cuda.is_available() else "cpu"):
70
+ """
71
+ Replaces the clothing region in the image with new clothing based on a text prompt,
72
+ using Stable Diffusion Inpainting.
73
+ """
74
+ pipe = StableDiffusionInpaintPipeline.from_pretrained(
75
+ "stabilityai/stable-diffusion-2-inpainting",
76
+ torch_dtype=torch.float16
77
+ )
78
+ pipe = pipe.to(device)
79
+
80
+ # Resize the image and mask to the same size. Important for inpainting.
81
+ image = image.resize((512, 512))
82
+ mask_img = mask_img.resize((512, 512))
83
+
84
+ prompt = f"A photo of a person wearing {clothing_prompt}" #Add style or detail
85
+ image = pipe(prompt=prompt, image=image, mask_image=mask_img).images[0]
86
+ return image
87
+
88
+ # --- 4. Main Function (Putting it all together) ---
89
+ def change_clothing(image_path, clothing_prompt):
90
+ """
91
+ Main function to change the clothing in an image.
92
+ """
93
+ # 1. Load the image
94
+ image = Image.open(image_path).convert("RGB")
95
+
96
+ # 2. Estimate the pose
97
+ results, cv2_image = estimate_pose(image_path)
98
+ if results is None:
99
+ print("No pose detected.")
100
+ return None
101
+
102
+ # 3. Segment the clothing
103
+ mask_img = segment_clothing(image, results)
104
+
105
+ # 4. Inpaint the clothing
106
+ modified_image = inpaint_clothing(image, mask_img, clothing_prompt)
107
+
108
+ return modified_image
109
+
110
+
111
+ # --- Example Usage ---
112
+ if __name__ == "__main__":
113
+ input_image_path = "person.jpg" # Replace with your image
114
+ clothing_description = "a red leather jacket" # Replace with desired clothing
115
+
116
+ modified_image = change_clothing(input_image_path, clothing_description)
117
+
118
+ if modified_image:
119
+ modified_image.save("modified_image.jpg")
120
+ print("Clothing changed and saved to modified_image.jpg")
121
+ else:
122
+ print("Failed to change clothing.")