new_mmm / app.py
gaur3009's picture
Create app.py
1312174 verified
raw
history blame
1.62 kB
import gradio as gr
import torch
import cv2
import numpy as np
from torchvision import transforms
from PIL import Image
# Load MiDaS depth estimation model
midas_model = torch.hub.load("intel-isl/MiDaS", "DPT_Hybrid")
midas_model.eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
midas_model.to(device)
midas_transform = torch.hub.load("intel-isl/MiDaS", "transforms").default_transform
def estimate_depth(image):
"""Estimate depth map to identify fabric folds."""
image = image.convert("RGB")
image_tensor = midas_transform(image).to(device)
with torch.no_grad():
depth = midas_model(image_tensor).squeeze().cpu().numpy()
depth = cv2.resize(depth, (image.size[0], image.size[1]))
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
return depth.astype(np.uint8)
def detect_folds(image):
"""Apply edge detection and highlight cloth folds."""
depth_map = estimate_depth(image)
edges = cv2.Canny(depth_map, 50, 150)
# Convert edges to 3-channel image for visualization
edges_colored = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
overlay = cv2.addWeighted(np.array(image), 0.7, edges_colored, 0.3, 0)
return Image.fromarray(overlay)
def main(image):
return detect_folds(image)
iface = gr.Interface(
fn=main,
inputs=gr.Image(type="pil"),
outputs=gr.Image(type="pil"),
title="Cloth Fold Detection",
description="Upload an image of clothing to visualize folds using depth estimation and edge detection."
)
if __name__ == "__main__":
iface.launch(share=True, debug=True)