freealise commited on
Commit
88887a0
1 Parent(s): 28b9027

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -576,20 +576,25 @@ def draw_mask(l, t, v, d, evt: gr.EventData):
576
 
577
  def findNormals():
578
  global depths
579
- depth = cv2.imread(depths[frame_selected]).astype(np.float32)
580
- normals = np.zeros((depth.shape[0], depth.shape[1], 3), dtype=np.float32)
581
-
582
- for x in range(1, depth.shape[1] - 1):
583
- for y in range(1, depth.shape[0] - 1):
584
- t = np.array([x, y - 1, depth[y - 1, x]], dtype=np.float32)
585
- l = np.array([x - 1, y, depth[y, x - 1]], dtype=np.float32)
586
- c = np.array([x, y, depth[y, x]], dtype=np.float32)
587
-
588
- d = np.cross(l - c, t - c)
589
- n = d / np.linalg.norm(d)
590
- normals[y, x] = n
591
-
592
- return (normals * 255 + 127).astype(np.uint8)
 
 
 
 
 
593
 
594
 
595
  load_model="""
 
576
 
577
  def findNormals():
578
  global depths
579
+ d_im = cv2.imread(depths[frame_selected]).astype(np.uint8)
580
+ zy, zx = np.gradient(d_im)
581
+ # You may also consider using Sobel to get a joint Gaussian smoothing and differentation
582
+ # to reduce noise
583
+ #zx = cv2.Sobel(d_im, cv2.CV_64F, 1, 0, ksize=5)
584
+ #zy = cv2.Sobel(d_im, cv2.CV_64F, 0, 1, ksize=5)
585
+
586
+ normal = np.dstack((-zx, -zy, np.ones_like(d_im)))
587
+ n = np.linalg.norm(normal, axis=2)
588
+ normal[:, :, 0] /= n
589
+ normal[:, :, 1] /= n
590
+ normal[:, :, 2] /= n
591
+
592
+ # offset and rescale values to be in 0-255
593
+ normal += 1
594
+ normal /= 2
595
+ normal *= 255
596
+
597
+ return normal[:, :, ::-1]
598
 
599
 
600
  load_model="""