sitammeur commited on
Commit
10029cc
·
verified ·
1 Parent(s): 08e82ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -45
app.py CHANGED
@@ -1,47 +1,6 @@
1
  # Importing the requirements
2
- import numpy as np
3
  import gradio as gr
4
- import torch
5
- from PIL import Image
6
- from transformers import DPTImageProcessor, DPTForDepthEstimation
7
-
8
-
9
- # Load the model and feature extractor
10
- feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-beit-large-512")
11
- model = DPTForDepthEstimation.from_pretrained("Intel/dpt-beit-large-512")
12
-
13
-
14
- def process_image(image):
15
- """
16
- Preprocesses an image, passes it through a model, and returns the formatted depth map as an image.
17
-
18
- Args:
19
- image (PIL.Image.Image): The input image.
20
-
21
- Returns:
22
- PIL.Image.Image: The formatted depth map as an image.
23
- """
24
-
25
- # Preprocess the image for the model
26
- encoding = feature_extractor(image, return_tensors="pt")
27
-
28
- # Forward pass through the model
29
- with torch.no_grad():
30
- outputs = model(**encoding)
31
- predicted_depth = outputs.predicted_depth
32
-
33
- # Interpolate the predicted depth map to the original image size
34
- prediction = torch.nn.functional.interpolate(
35
- predicted_depth.unsqueeze(1),
36
- size=image.size[::-1],
37
- mode="bicubic",
38
- align_corners=False,
39
- ).squeeze()
40
- output = prediction.cpu().numpy()
41
- formatted = (output * 255 / np.max(output)).astype("uint8")
42
-
43
- # Return the formatted depth map as an image
44
- return Image.fromarray(formatted)
45
 
46
 
47
  # Image input for the interface
@@ -52,9 +11,9 @@ answer = gr.Image(type="pil", label="Depth Map")
52
 
53
  # Examples for the interface
54
  examples = [
55
- ["cat.jpg"],
56
- ["dog.jpg"],
57
- ["bird.jpg"],
58
  ]
59
 
60
  # Title, description, and article for the interface
 
1
  # Importing the requirements
 
2
  import gradio as gr
3
+ from depth_estimation import process_image # Import the depth estimation function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
 
6
  # Image input for the interface
 
11
 
12
  # Examples for the interface
13
  examples = [
14
+ ["images/cat.jpg"],
15
+ ["images/dog.jpg"],
16
+ ["images/bird.jpg"],
17
  ]
18
 
19
  # Title, description, and article for the interface