Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
# Load MiDaS depth estimation model
|
9 |
+
midas_model = torch.hub.load("intel-isl/MiDaS", "DPT_Hybrid")
|
10 |
+
midas_model.eval()
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
midas_model.to(device)
|
13 |
+
midas_transform = torch.hub.load("intel-isl/MiDaS", "transforms").default_transform
|
14 |
+
|
15 |
+
def estimate_depth(image):
|
16 |
+
"""Estimate depth map to identify fabric folds."""
|
17 |
+
image = image.convert("RGB")
|
18 |
+
image_tensor = midas_transform(image).to(device)
|
19 |
+
|
20 |
+
with torch.no_grad():
|
21 |
+
depth = midas_model(image_tensor).squeeze().cpu().numpy()
|
22 |
+
|
23 |
+
depth = cv2.resize(depth, (image.size[0], image.size[1]))
|
24 |
+
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
25 |
+
return depth.astype(np.uint8)
|
26 |
+
|
27 |
+
def detect_folds(image):
|
28 |
+
"""Apply edge detection and highlight cloth folds."""
|
29 |
+
depth_map = estimate_depth(image)
|
30 |
+
edges = cv2.Canny(depth_map, 50, 150)
|
31 |
+
|
32 |
+
# Convert edges to 3-channel image for visualization
|
33 |
+
edges_colored = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
|
34 |
+
overlay = cv2.addWeighted(np.array(image), 0.7, edges_colored, 0.3, 0)
|
35 |
+
|
36 |
+
return Image.fromarray(overlay)
|
37 |
+
|
38 |
+
def main(image):
|
39 |
+
return detect_folds(image)
|
40 |
+
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=main,
|
43 |
+
inputs=gr.Image(type="pil"),
|
44 |
+
outputs=gr.Image(type="pil"),
|
45 |
+
title="Cloth Fold Detection",
|
46 |
+
description="Upload an image of clothing to visualize folds using depth estimation and edge detection."
|
47 |
+
)
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
iface.launch(share=True, debug=True)
|