Rodrigo_Cobo commited on
Commit
61d09e6
1 Parent(s): a1be538

added depth estimation

Browse files
Files changed (1) hide show
  1. app.py +47 -3
app.py CHANGED
@@ -1,19 +1,63 @@
1
  import os
2
  import gradio as gr
 
 
 
 
 
 
3
 
4
  def update(slider, img):
5
 
6
  if not os.path.exists('temp'):
7
  os.system('mkdir temp')
8
 
9
- img.save("temp/image.jpg", "JPEG")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- return f'temp/image.jpg'
12
 
13
  with gr.Blocks() as demo:
14
  gr.Markdown("Start typing below and then click **Run** to see the output.")
15
  inp = [gr.Slider(1,15, default = 2, label='StepCycles',step= 1)]
16
-
17
  with gr.Row():
18
  inp.append(gr.Image(type="pil", label="Input"))
19
  out = gr.Image(type="file", label="Output")
 
1
  import os
2
  import gradio as gr
3
+ import cv2
4
+ import torch
5
+ import urllib.request
6
+ from PIL import Image
7
+
8
+ import matplotlib.pyplot as plt
9
 
10
  def update(slider, img):
11
 
12
  if not os.path.exists('temp'):
13
  os.system('mkdir temp')
14
 
15
+ filename = "temp/image.jpg"
16
+
17
+ img.save(filename, "JPEG")
18
+
19
+ model_type = "DPT_Hybrid"
20
+ midas = torch.hub.load("intel-isl/MiDaS", model_type)
21
+
22
+ device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
23
+ midas.to(device)
24
+ midas.eval()
25
+
26
+ midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
27
+
28
+ if model_type == "DPT_Large" or model_type == "DPT_Hybrid":
29
+ transform = midas_transforms.dpt_transform
30
+ else:
31
+ transform = midas_transforms.small_transform
32
+
33
+ img = cv2.imread(filename)
34
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
35
+
36
+ input_batch = transform(img).to(device)
37
+
38
+ with torch.no_grad():
39
+ prediction = midas(input_batch)
40
+
41
+ prediction = torch.nn.functional.interpolate(
42
+ prediction.unsqueeze(1),
43
+ size=img.shape[:2],
44
+ mode="bicubic",
45
+ align_corners=False,
46
+ ).squeeze()
47
+
48
+ output = prediction.cpu().numpy()
49
+
50
+ out_im = Image.fromarray(output)
51
+ out_im.save("temp/image_depth.jpg", "JPEG")
52
+
53
+ return f'temp/image_depth.jpg'
54
+
55
 
 
56
 
57
  with gr.Blocks() as demo:
58
  gr.Markdown("Start typing below and then click **Run** to see the output.")
59
  inp = [gr.Slider(1,15, default = 2, label='StepCycles',step= 1)]
60
+
61
  with gr.Row():
62
  inp.append(gr.Image(type="pil", label="Input"))
63
  out = gr.Image(type="file", label="Output")