MAnthony commited on
Commit
bfeb983
·
verified ·
1 Parent(s): 23d6f83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -2
app.py CHANGED
@@ -4,22 +4,46 @@ from utils import colorize
4
  from utils import get_min_and_max
5
  from PIL import Image
6
  import tempfile
 
7
 
8
  import json
9
 
10
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
11
  model = torch.hub.load('isl-org/ZoeDepth', "ZoeD_N", pretrained=True).to(DEVICE).eval()
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def predict(image):
14
  image.thumbnail((1024,1024))
15
  depth = model.infer_pil(image)
16
  colored_depth, vmin, vmax = colorize(depth, cmap='gray_r')
17
 
 
18
  tmp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
19
  raw_depth = Image.fromarray((depth*256).astype('uint16'))
20
  raw_depth.save(tmp.name)
 
 
 
 
 
21
 
22
- return colored_depth, tmp.name
23
 
24
- iface = gr.Interface(fn=predict, inputs=gr.Image(label="Input Image", type='pil'), outputs=["image", "file"])
25
  iface.launch()
 
4
  from utils import get_min_and_max
5
  from PIL import Image
6
  import tempfile
7
+ import numpy as np
8
 
9
  import json
10
 
11
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
12
  model = torch.hub.load('isl-org/ZoeDepth', "ZoeD_N", pretrained=True).to(DEVICE).eval()
13
 
14
+ def split_numpy_16_bit(numpy_image_16bit):
15
+ result_numpy_split = np.zeros((numpy_image_16bit.shape[0], numpy_image_16bit.shape[1], 3));
16
+
17
+ i = 0;
18
+ for row in numpy_image_16bit:
19
+ j = 0
20
+ for pixel in row:
21
+ firstPart = np.array(pixel).astype(np.uint8)
22
+ secondPart = np.array(pixel>>8).astype(np.uint8)
23
+
24
+ result_numpy_split[i, j, 0] = firstPart
25
+ result_numpy_split[i, j, 1] = secondPart
26
+ j += 1
27
+ i += 1
28
+
29
+ return result_numpy_split
30
+
31
  def predict(image):
32
  image.thumbnail((1024,1024))
33
  depth = model.infer_pil(image)
34
  colored_depth, vmin, vmax = colorize(depth, cmap='gray_r')
35
 
36
+
37
  tmp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
38
  raw_depth = Image.fromarray((depth*256).astype('uint16'))
39
  raw_depth.save(tmp.name)
40
+
41
+ depth_uint16 = (depth*256).astype('uint16')
42
+ numpy_image_split = split_numpy_16_bit(depth_uint16)
43
+
44
+ depth_image_two_components = Image.fromarray(np.uint8(numpy_image_split)).convert('RGB')
45
 
46
+ return colored_depth, tmp.name, depth_image_two_components
47
 
48
+ iface = gr.Interface(fn=predict, inputs=gr.Image(label="Input Image", type='pil'), outputs=["image", "file", "image"])
49
  iface.launch()