forked from gist-ailab/uoais
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k4a_demo.py
executable file
·145 lines (124 loc) · 5.4 KB
/
k4a_demo.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import argparse
import multiprocessing as mp
import os
import cv2
import pyk4a
import numpy as np
from detectron2.engine import DefaultPredictor
from pyk4a import Config, PyK4A
from utils import *
from adet.config import get_cfg
from adet.utils.visualizer import visualize_pred_amoda_occ
from adet.utils.post_process import detector_postprocess, DefaultPredictor
from foreground_segmentation.model import Context_Guided_Network
def get_parser():
parser = argparse.ArgumentParser(description="Detectron2 Demo")
parser.add_argument(
"--config-file",
default="configs/R50_rgbdconcat_mlc_occatmask_hom_concat.yaml",
metavar="FILE",
help="path to config file",
)
parser.add_argument(
"--confidence-threshold",
type=float,
default=0.7,
help="Minimum score for instance predictions to be shown",
)
parser.add_argument(
"--use-cgnet",
action="store_true",
help="Use foreground segmentation model to filter our background instances or not"
)
parser.add_argument(
"--cgnet-weight-path",
type=str,
default="./foreground_segmentation/rgbd_fg.pth",
help="path to forground segmentation weight"
)
return parser
if __name__ == "__main__":
# UOAIS-Net
args = get_parser().parse_args()
cfg = get_cfg()
cfg.merge_from_file(args.config_file)
cfg.defrost()
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold
cfg.MODEL.ROI_HEADS.NMS_THRESH_TEST = args.confidence_threshold
predictor = DefaultPredictor(cfg)
W, H = cfg.INPUT.IMG_SIZE
# CG-Net (foreground segmentation)
if args.use_cgnet:
print("Use foreground segmentation model (CG-Net) to filter out background instances")
checkpoint = torch.load(os.path.join(args.cgnet_weight_path))
fg_model = Context_Guided_Network(classes=2, in_channel=4)
fg_model.load_state_dict(checkpoint['model'])
fg_model.cuda()
fg_model.eval()
# Azure Kinect
mp.set_start_method("spawn", force=True)
k4a = PyK4A(
Config(
color_resolution=pyk4a.ColorResolution.RES_720P,
depth_mode=pyk4a.DepthMode.WFOV_UNBINNED,
synchronized_images_only=True,
camera_fps=pyk4a.FPS.FPS_5,
))
k4a.start()
while True:
# get rgb-d from azure kinect
capture = k4a.get_capture()
rgb_img = capture.color[:, :, :3]
rgb_img = cv2.resize(rgb_img, (W, H))
depth = capture.transformed_depth
depth_img = normalize_depth(depth)
depth_img = cv2.resize(depth_img, (W, H), interpolation=cv2.INTER_NEAREST)
depth_img = inpaint_depth(depth_img)
# UOAIS-Net inference
if cfg.INPUT.DEPTH and cfg.INPUT.DEPTH_ONLY:
uoais_input = depth_img
elif cfg.INPUT.DEPTH and not cfg.INPUT.DEPTH_ONLY:
uoais_input = np.concatenate([rgb_img, depth_img], -1)
outputs = predictor(uoais_input)
instances = detector_postprocess(outputs['instances'], H, W).to('cpu')
# CG-Net inference
if args.use_cgnet:
fg_rgb_input = standardize_image(cv2.resize(rgb_img, (320, 240)))
fg_rgb_input = array_to_tensor(fg_rgb_input).unsqueeze(0)
fg_depth_input = cv2.resize(depth_img, (320, 240))
fg_depth_input = array_to_tensor(fg_depth_input[:,:,0:1]).unsqueeze(0) / 255
fg_input = torch.cat([fg_rgb_input, fg_depth_input], 1)
fg_output = fg_model(fg_input.cuda())
fg_output = fg_output.cpu().data[0].numpy().transpose(1, 2, 0)
fg_output = np.asarray(np.argmax(fg_output, axis=2), dtype=np.uint8)
fg_output = cv2.resize(fg_output, (W, H), interpolation=cv2.INTER_NEAREST)
preds = instances.pred_masks.detach().cpu().numpy()
pred_visibles = instances.pred_visible_masks.detach().cpu().numpy()
bboxes = instances.pred_boxes.tensor.detach().cpu().numpy()
pred_occs = instances.pred_occlusions.detach().cpu().numpy()
# filter out the background instances
if args.use_cgnet:
remove_idxs = []
for i, pred_visible in enumerate(pred_visibles):
iou = np.sum(np.bitwise_and(pred_visible, fg_output)) / np.sum(pred_visible)
if iou < 0.5:
remove_idxs.append(i)
preds = np.delete(preds, remove_idxs, 0)
pred_visibles = np.delete(pred_visibles, remove_idxs, 0)
bboxes = np.delete(bboxes, remove_idxs, 0)
pred_occs = np.delete(pred_occs, remove_idxs, 0)
# reorder predictions for visualization
idx_shuf = np.concatenate((np.where(pred_occs==1)[0] , np.where(pred_occs==0)[0] ))
preds, pred_occs, bboxes = preds[idx_shuf], pred_occs[idx_shuf], bboxes[idx_shuf]
vis_img = visualize_pred_amoda_occ(rgb_img, preds, bboxes, pred_occs)
if args.use_cgnet:
vis_fg = np.zeros_like(rgb_img)
vis_fg[:, :, 1] = fg_output*255
vis_img = cv2.addWeighted(vis_img, 0.8, vis_fg, 0.2, 0)
vis_all_img = np.hstack([rgb_img, depth_img, vis_img])
cv2.imshow("ESC: quit" + args.config_file, vis_all_img)
k = cv2.waitKey(1)
if k == 27: # esc
cv2.destroyAllWindows()
break