| import os |
| import numpy as np |
| import pandas as pd |
| import gradio as gr |
| from PIL import Image |
| import cv2 |
| import mediapipe as mp |
|
|
| |
| mp_pose = mp.solutions.pose |
| pose = mp_pose.Pose(static_image_mode=True, model_complexity=2) |
|
|
| |
| product_data = pd.DataFrame({ |
| 'Product': ['Dress 1', 'Dress 2', 'Dress 3'], |
| 'Size': ['S', 'M', 'L'], |
| 'Color': ['Red', 'Blue', 'Green'], |
| 'Image': ['sample_dress1.jpg', 'sample_dress2.jpg', 'sample_dress3.jpg'] |
| }) |
|
|
| def get_keypoints(image): |
| results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) |
| if not results.pose_landmarks: |
| return None |
| keypoints = {} |
| for idx, lm in enumerate(results.pose_landmarks.landmark): |
| keypoints[idx] = (int(lm.x * image.shape[1]), int(lm.y * image.shape[0])) |
| return keypoints |
|
|
| def process_image(image, product): |
| |
| customer_image = Image.fromarray(image) |
| customer_image = customer_image.convert("RGBA") |
|
|
| |
| customer_image_cv = cv2.cvtColor(np.array(customer_image), cv2.COLOR_RGBA2BGR) |
|
|
| |
| keypoints = get_keypoints(customer_image_cv) |
| |
| |
| garment_filename = product_data[product_data['Product'] == product]['Image'].values[0] |
| garment_path = os.path.join(os.getcwd(), garment_filename) |
|
|
| dress_image = Image.open(garment_path).convert("RGBA") |
|
|
| |
| if keypoints and 11 in keypoints and 12 in keypoints: |
| shoulder_width = abs(keypoints[11][0] - keypoints[12][0]) |
| scaling_factor = shoulder_width / dress_image.width |
| new_dress_height = int(dress_image.height * scaling_factor) |
| resized_dress_image = dress_image.resize((shoulder_width, new_dress_height), resample=Image.BICUBIC) |
| |
| |
| dress_position = (keypoints[11][0] - shoulder_width // 2, keypoints[11][1]) |
| |
| |
| result_image = Image.new("RGBA", customer_image.size) |
| result_image.paste(customer_image, (0, 0)) |
| result_image.paste(resized_dress_image, dress_position, resized_dress_image) |
| |
| |
| result_array = np.array(result_image) |
| else: |
| result_array = np.array(customer_image) |
| |
| |
| product_details = product_data[product_data['Product'] == product].iloc[0].to_dict() |
| |
| return result_array, product_details |
|
|
| |
| iface = gr.Interface( |
| fn=process_image, |
| inputs=[ |
| gr.Image(type="numpy", label="Upload Your Image"), |
| gr.Dropdown(choices=product_data['Product'].tolist(), label="Select Product") |
| ], |
| outputs=[ |
| gr.Image(type="numpy", label="Output Image"), |
| gr.JSON(label="Product Details") |
| ], |
| title="Virtual Dress Fitting", |
| description="Upload an image and select a product to see how it fits on you." |
| ) |
|
|
| if __name__ == "__main__": |
| iface.launch() |
|
|