tpatel53 commited on
Commit
1acc6e4
·
verified ·
1 Parent(s): 522126b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -4
app.py CHANGED
@@ -1,7 +1,66 @@
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello" + name + "!!"
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation, DPTFeatureExtractor, DPTForDepthEstimation
3
+ from PIL import Image, ImageFilter
4
+ import numpy as np
5
  import gradio as gr
6
 
7
+ # Load pre-trained models and feature extractors
8
+ seg_feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
9
+ seg_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
10
+ depth_feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
11
+ depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
12
 
13
+ def process_image(image):
14
+ # Preprocess the input image
15
+ image = image.resize((512, 512))
16
+
17
+ # Perform semantic segmentation
18
+ seg_inputs = seg_feature_extractor(images=image, return_tensors="pt")
19
+ with torch.no_grad():
20
+ seg_outputs = seg_model(**seg_inputs)
21
+ seg_logits = seg_outputs.logits
22
+ segmentation = torch.argmax(seg_logits, dim=1)[0].numpy()
23
+
24
+ # Create binary mask for 'person' class
25
+ person_class_index = 12
26
+ binary_mask = (segmentation == person_class_index).astype(np.uint8) * 255
27
+
28
+ # Perform depth estimation
29
+ depth_inputs = depth_feature_extractor(images=image, return_tensors="pt")
30
+ with torch.no_grad():
31
+ depth_outputs = depth_model(**depth_inputs)
32
+ predicted_depth = depth_outputs.predicted_depth[0].cpu().numpy()
33
+ normalized_depth = (predicted_depth - predicted_depth.min()) / (predicted_depth.max() - predicted_depth.min())
34
+ inverted_depth = 1 - normalized_depth
35
+ depth_weight_resized = np.array(Image.fromarray((inverted_depth * 255).astype(np.uint8)).resize((512, 512))) / 255.0
36
+ depth_weight_resized = depth_weight_resized[:, :, np.newaxis]
37
+
38
+ # Create blurred background effect
39
+ blurred_image = image.filter(ImageFilter.GaussianBlur(radius=15))
40
+ original_np = np.array(image).astype(np.float32)
41
+ blurred_np = np.array(blurred_image).astype(np.float32)
42
+ composite_np = (1 - depth_weight_resized) * original_np + depth_weight_resized * blurred_np
43
+ composite_image = Image.fromarray(np.clip(composite_np, 0, 255).astype(np.uint8))
44
+
45
+ # Return results
46
+ binary_mask_image = Image.fromarray(binary_mask)
47
+ depth_map_image = Image.fromarray((normalized_depth * 255).astype(np.uint8))
48
+ return image, binary_mask_image, depth_map_image, composite_image
49
+
50
+ # Create Gradio interface
51
+ interface = gr.Interface(
52
+ fn=process_image,
53
+ inputs=gr.inputs.Image(type="pil"),
54
+ outputs=[
55
+ gr.outputs.Image(type="pil", label="Original Image"),
56
+ gr.outputs.Image(type="pil", label="Segmentation Mask"),
57
+ gr.outputs.Image(type="pil", label="Depth Map"),
58
+ gr.outputs.Image(type="pil", label="Blurred Background Effect"),
59
+ ],
60
+ title="Semantic Segmentation and Depth Estimation",
61
+ description="Upload an image to generate a segmentation mask, depth map, and blurred background effect."
62
+ )
63
+
64
+ # Launch the interface
65
+ if __name__ == "__main__":
66
+ interface.launch()