besarismaili commited on
Commit
4b82956
1 Parent(s): d631554

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +47 -19
  2. requirements.txt +1 -1
app.py CHANGED
@@ -3,13 +3,13 @@ from transformers import DPTFeatureExtractor, DPTForDepthEstimation
3
  import torch
4
  import numpy as np
5
  from PIL import Image
6
-
7
- torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
8
 
9
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
10
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
11
 
12
- def process_image(image):
13
  # prepare image for the model
14
  encoding = feature_extractor(image, return_tensors="pt")
15
 
@@ -17,7 +17,7 @@ def process_image(image):
17
  with torch.no_grad():
18
  outputs = model(**encoding)
19
  predicted_depth = outputs.predicted_depth
20
-
21
  # interpolate to original size
22
  prediction = torch.nn.functional.interpolate(
23
  predicted_depth.unsqueeze(1),
@@ -29,18 +29,46 @@ def process_image(image):
29
  formatted = (output * 255 / np.max(output)).astype('uint8')
30
  img = Image.fromarray(formatted)
31
  return img
32
-
33
- return result
34
-
35
- title = "My Demo: 1zero-shot depth estimation with DPT"
36
- description = "Demo for Intel's DPT, a Dense Prediction Transformer for state-of-the-art dense prediction tasks such as semantic segmentation and depth estimation."
37
- examples =[['cats.jpg']]
38
-
39
- iface = gr.Interface(fn=process_image,
40
- inputs=gr.inputs.Image(type="pil"),
41
- outputs=gr.outputs.Image(type="pil", label="predicted depth"),
42
- title=title,
43
- description=description,
44
- examples=examples,
45
- enable_queue=True)
46
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import torch
4
  import numpy as np
5
  from PIL import Image
6
+ import os
7
+ import cv2
8
 
9
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
10
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
11
 
12
+ def get_image_depth(image):
13
  # prepare image for the model
14
  encoding = feature_extractor(image, return_tensors="pt")
15
 
 
17
  with torch.no_grad():
18
  outputs = model(**encoding)
19
  predicted_depth = outputs.predicted_depth
20
+
21
  # interpolate to original size
22
  prediction = torch.nn.functional.interpolate(
23
  predicted_depth.unsqueeze(1),
 
29
  formatted = (output * 255 / np.max(output)).astype('uint8')
30
  img = Image.fromarray(formatted)
31
  return img
32
+
33
+
34
+ def process_sequence(files):
35
+ file_paths = [file.name for file in files]
36
+ for file_path in file_paths:
37
+ image = Image.open(file_path)
38
+ depth_image = get_image_depth(image)
39
+ depth_image.save(os.path.join('output', os.path.basename(file_path)))
40
+ return file_paths, gr.Info("This is some info")
41
+
42
+ title = "# Depth estimation demo"
43
+ description = "Demo for Intel's DPT"
44
+
45
+ with gr.Blocks() as iface:
46
+
47
+ gr.Markdown(title)
48
+ gr.Markdown(description)
49
+
50
+ with gr.Row():
51
+
52
+ with gr.Column():
53
+ with gr.Tab(label='Singel image'):
54
+ image = gr.Image(type="pil")
55
+ button = gr.Button(value="Get depth", interactive=True, variant="primary")
56
+ image_output=gr.Image(type="pil", label="predicted depth")
57
+
58
+ with gr.Column():
59
+ with gr.Tab(label='Frames'):
60
+ file_output = gr.File(visible=False)
61
+ upload_button = gr.UploadButton("Select directory", file_types=["image"], file_count="directory")
62
+ upload_button.upload(process_sequence, upload_button, file_output)
63
+
64
+ #output=gr.Video(label="Predicted Depth")
65
+ message=gr.Text(value="Check output folder for the depth frames.")
66
+
67
+ button.click(
68
+ fn=get_image_depth,
69
+ inputs=[image],
70
+ outputs=[image_output]
71
+ )
72
+
73
+ iface.queue(concurrency_count=1)
74
+ iface.launch(debug=True, enable_queue=True)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  torch
2
  git+https://github.com/nielsrogge/transformers.git@add_dpt_redesign#egg=transformers
3
  numpy
4
- Pillow
 
1
  torch
2
  git+https://github.com/nielsrogge/transformers.git@add_dpt_redesign#egg=transformers
3
  numpy
4
+ Pillow