gaur3009 commited on
Commit
00b93d3
·
verified ·
1 Parent(s): fd9902d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import cv2
4
+ import numpy as np
5
+ from torchvision import transforms
6
+ from PIL import Image
7
+ from transformers import DPTForDepthEstimation, DPTFeatureExtractor
8
+
9
+ # Load depth estimation model
10
+ model_name = "Intel/dpt-large"
11
+ feature_extractor = DPTFeatureExtractor.from_pretrained(model_name)
12
+ depth_model = DPTForDepthEstimation.from_pretrained(model_name)
13
+ depth_model.eval()
14
+
15
+ def estimate_depth(image):
16
+ """Estimate depth map from image."""
17
+ inputs = feature_extractor(images=image, return_tensors="pt")
18
+ with torch.no_grad():
19
+ outputs = depth_model(**inputs)
20
+ depth = outputs.predicted_depth.squeeze().numpy()
21
+ depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
22
+ return depth.astype(np.uint8)
23
+
24
+ def warp_design(cloth_img, design_img):
25
+ """Warp the design onto the clothing while preserving folds."""
26
+ # Convert images to numpy arrays
27
+ cloth_np = np.array(cloth_img)
28
+ design_np = np.array(design_img)
29
+
30
+ # Estimate depth for fold detection
31
+ depth_map = estimate_depth(cloth_img)
32
+
33
+ # Generate displacement map based on depth
34
+ displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=5)
35
+ displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=5)
36
+
37
+ # Normalize displacement values
38
+ displacement_x = cv2.normalize(displacement_x, None, -10, 10, cv2.NORM_MINMAX)
39
+ displacement_y = cv2.normalize(displacement_y, None, -10, 10, cv2.NORM_MINMAX)
40
+
41
+ # Warp design using displacement map
42
+ h, w, _ = cloth_np.shape
43
+ map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
44
+ map_x = np.float32(map_x + displacement_x)
45
+ map_y = np.float32(map_y + displacement_y)
46
+ warped_design = cv2.remap(design_np, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE)
47
+
48
+ # Blend images
49
+ blended = cv2.addWeighted(cloth_np, 0.6, warped_design, 0.4, 0)
50
+ return Image.fromarray(blended)
51
+
52
+ def main(cloth, design):
53
+ return warp_design(cloth, design)
54
+
55
+ iface = gr.Interface(
56
+ fn=main,
57
+ inputs=[gr.Image(type="pil"), gr.Image(type="pil")],
58
+ outputs=gr.Image(type="pil"),
59
+ title="AI Cloth Design Warping",
60
+ description="Upload a clothing image and a design to blend it naturally, considering fabric folds."
61
+ )
62
+
63
+ if __name__ == "__main__":
64
+ iface.launch()