-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathego_hos_wrapper.py
125 lines (107 loc) · 5.49 KB
/
ego_hos_wrapper.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
from mmseg.apis import inference_segmentor, init_segmentor
import mmcv
import glob
import os
from tqdm import tqdm
import argparse
import copy
import shutil
from PIL import Image
import numpy as np
from skimage.io import imsave
import pdb
def visualize_twohands(img, seg_result, alpha = 0.4):
seg_color = np.zeros((img.shape))
seg_color[seg_result == 0] = (0, 0, 0) # background
seg_color[seg_result == 1] = (255, 0, 0) # left_hand
seg_color[seg_result == 2] = (0, 0, 255) # right_hand
vis = img * (1 - alpha) + seg_color * alpha
return vis
def visualize_cb(img, seg_result, alpha = 0.4):
seg_color = np.zeros((img.shape))
seg_color[seg_result == 0] = (0, 0, 0) # background
seg_color[seg_result == 1] = (255, 0, 0) # contact
vis = img * (1 - alpha) + seg_color * alpha
return vis
def visualize_twohands_obj1(img, seg_result, alpha = 0.4):
seg_color = np.zeros((img.shape))
seg_color[seg_result == 0] = (0, 0, 0) # background
seg_color[seg_result == 1] = (255, 0, 0) # left_hand
seg_color[seg_result == 2] = (0, 0, 255) # right_hand
seg_color[seg_result == 3] = (255, 0, 255) # left_object1
seg_color[seg_result == 4] = (0, 255, 255) # right_object1
seg_color[seg_result == 5] = (0, 255, 0) # two_object1
vis = img * (1 - alpha) + seg_color * alpha
return vis
def visualize_twohands_obj2(img, seg_result, alpha = 0.4):
seg_color = np.zeros((img.shape))
seg_color[seg_result == 0] = (0, 0, 0) # background
seg_color[seg_result == 1] = (255, 0, 0) # left_hand
seg_color[seg_result == 2] = (0, 0, 255) # right_hand
seg_color[seg_result == 3] = (255, 0, 255) # left_object1
seg_color[seg_result == 4] = (0, 255, 255) # right_object1
seg_color[seg_result == 5] = (0, 255, 0) # two_object1
seg_color[seg_result == 6] = (255, 204, 255) # left_object2
seg_color[seg_result == 7] = (204, 255, 255) # right_object2
seg_color[seg_result == 8] = (204, 255, 204) # two_object2
vis = img * (1 - alpha) + seg_color * alpha
return vis
class EgoHOSWrapper:
def __init__(self,
cache_path,
repo_path='.',
two_hands_cfg='EgoHOS/mmsegmentation/work_dirs/seg_twohands_ccda/seg_twohands_ccda.py',
cb_cfg='EgoHOS/mmsegmentation/work_dirs/twohands_to_cb_ccda/twohands_to_cb_ccda.py',
obj2_cfg='EgoHOS/mmsegmentation/work_dirs/twohands_cb_to_obj2_ccda/twohands_cb_to_obj2_ccda.py',
two_hands_ckpt='EgoHOS/mmsegmentation/work_dirs/seg_twohands_ccda/best_mIoU_iter_56000.pth',
cb_ckpt='EgoHOS/mmsegmentation/work_dirs/twohands_to_cb_ccda/best_mIoU_iter_76000.pth',
obj2_ckpt='EgoHOS/mmsegmentation/work_dirs/twohands_cb_to_obj2_ccda/best_mIoU_iter_32000.pth',
device='cuda'
):
if not cache_path.startswith('/'):
raise ValueError("Please use the absolute file path as the cache path.")
self.cache_path = cache_path
two_hands_cfg = repo_path + '/' + two_hands_cfg
cb_cfg = repo_path + '/' + cb_cfg
obj2_cfg = repo_path + '/' + obj2_cfg
two_hands_ckpt = repo_path + '/' + two_hands_ckpt
cb_ckpt = repo_path + '/' + cb_ckpt
obj2_ckpt = repo_path + '/' + obj2_ckpt
self.two_hands_cfg = two_hands_cfg
self.two_hands_ckpt = two_hands_ckpt
self.cb_cfg = cb_cfg
self.cb_ckpt = cb_ckpt
self.obj2_cfg = obj2_cfg
self.obj2_ckpt = obj2_ckpt
self.device = device
self.hand_model = init_segmentor(self.two_hands_cfg, self.two_hands_ckpt, device=device)
self.cb_model = init_segmentor(self.cb_cfg, self.cb_ckpt, device=device)
self.obj2_model = init_segmentor(self.obj2_cfg, self.obj2_ckpt, device=device)
self.test_images_fp = os.path.join(cache_path, "test_images")
self.pred_twohands_fp = os.path.join(cache_path, "pred_twohands")
self.pred_cb_fp = os.path.join(cache_path, "pred_cb")
os.makedirs(self.test_images_fp, exist_ok=True)
os.makedirs(self.pred_twohands_fp, exist_ok=True)
os.makedirs(self.pred_cb_fp, exist_ok=True)
def segment(self, image_fp, vis=False):
filename = image_fp.replace("/", "_").replace('.jpg', '.png').replace('.jpeg', '.png')
cache_fp = os.path.join(self.test_images_fp, filename)
shutil.copyfile(image_fp, cache_fp)
seg_hands = inference_segmentor(self.hand_model, cache_fp)[0]
imsave(os.path.join(self.pred_twohands_fp, filename), seg_hands.astype(np.uint8))
seg_cb = inference_segmentor(self.cb_model, cache_fp)[0]
imsave(os.path.join(self.pred_cb_fp, filename), seg_cb.astype(np.uint8))
seg_obj2 = inference_segmentor(self.obj2_model, cache_fp)[0]
os.remove(os.path.join(self.test_images_fp, filename))
os.remove(os.path.join(self.pred_twohands_fp, filename))
os.remove(os.path.join(self.pred_cb_fp, filename))
if vis is True:
img = np.array(Image.open(image_fp))
hand_vis = visualize_twohands(img, seg_hands)
imsave("vis_hands.png", hand_vis.astype(np.uint8))
cb_vis = visualize_cb(img, seg_cb)
imsave("vis_cb.png", cb_vis.astype(np.uint8))
obj2_vis = visualize_twohands_obj2(img, seg_obj2)
imsave("vis_obj2.png", obj2_vis.astype(np.uint8))
# pdb.set_trace()
return seg_obj2