Burman-AI commited on
Commit
b87511a
·
verified ·
1 Parent(s): 0d150c9

Delete main_code_script.py

Browse files
Files changed (1) hide show
  1. main_code_script.py +0 -104
main_code_script.py DELETED
@@ -1,104 +0,0 @@
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
7
- import numpy as np
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.
14
- Args:
15
- image_path: Path to the input image.
16
- Returns:
17
- A list of landmarks (x, y, visibility)
18
- or None if no pose is detected.
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, clothing_prompt, device="cpu" 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(
63
- "stabilityai/stable-diffusion-2-inpainting",
64
- "SimianLuo/LCM_Dreamshaper_v7"
65
- torch_dtype=torch.float16
66
- )
67
- pipe = pipe.to(device)
68
- # Resize the image and mask to a smaller size for faster inpainting
69
- width, height = image.size
70
- inpainted_size = (128, 128) # Smaller size for faster inpainting
71
- image = image.resize(inpainted_size)
72
- mask_img = mask_img.resize(inpainted_size)
73
- prompt = f"A photo of a person wearing {clothing_prompt}" #Add style or detail
74
- image = pipe(prompt=prompt, image=image, mask_image=mask_img).images[0]
75
- # Resize back to the original size (or a desired output size)
76
- image = image.resize((width, height)) # Or resize to a target output size
77
- return image
78
- # --- 4. Main Function (Putting it all together) ---
79
- def change_clothing(image_path, clothing_prompt):
80
- """
81
- Main function to change the clothing in an image.
82
- """
83
- # 1. Load the image
84
- image = Image.open(image_path).convert("RGB")
85
- # 2. Estimate the pose
86
- results, cv2_image = estimate_pose(image_path)
87
- if results is None:
88
- print("No pose detected.")
89
- return None
90
- # 3. Segment the clothing
91
- mask_img = segment_clothing(image, results)
92
- # 4. Inpaint the clothing
93
- modified_image = inpaint_clothing(image, mask_img, clothing_prompt)
94
- return modified_image
95
- # --- Example Usage ---
96
- if __name__ == "__main__":
97
- input_image_path = "person.jpg" # Replace with your image
98
- clothing_description = "a red leather jacket" # Replace with desired clothing
99
- modified_image = change_clothing(input_image_path, clothing_description)
100
- if modified_image:
101
- modified_image.save("modified_image.jpg")
102
- print("Clothing changed and saved to modified_image.jpg")
103
- else:
104
- print("Failed to change clothing.")