-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
36 lines (27 loc) · 955 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import cv2
import matplotlib.pyplot as plt
import numpy as np
from io import BytesIO
def read_png(res):
"""Converts string of bytes to readable image"""
nparr = np.frombuffer(res, np.uint8)
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return img_np
def read_npy(res):
"""Converts bytes to readable image"""
nparr = np.load(BytesIO(res))
return nparr
def normalize(src, max_depth=1500, min_depth=10):
"""Truncates and normalizes numpy array to display as depth map"""
src[src > max_depth] = max_depth
src[src < min_depth] = min_depth
src = src.astype('float') / max_depth * 255
src = np.rint(src)
normalized = np.uint8(src)
return normalized
def heatmap(src):
"""Turns grayscale depth map into heatmap"""
colormap = plt.get_cmap('inferno')
heatmap = (colormap(src) * 2 ** 16).astype(np.uint16)[:, :, :3]
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_RGB2BGR)
return heatmap