-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreceiveData.py
139 lines (115 loc) · 5.87 KB
/
receiveData.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
import os
import cv2
import json
import rospy
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
from std_msgs.msg import String, Bool
from datetime import datetime
class HololensUserDataSubscriber:
def __init__(self, model, initnode: bool = True):
if initnode:
rospy.init_node("HololensOutDataSubscriber")
self.dh_model = model
self.cfg = self.dh_model._cfg
self.bridge = CvBridge()
self.image = None
self.depth = None
self.label = None
self.ft_signal = False
self.imageReceived = False
self.depthReceived = False
self.labelReceived = False
self.finetuneSignalReceived = False
self.imgsub = rospy.Subscriber("hololens/out/robot_pov/raw_rgb", Image, self.imageSub)
self.depsub = rospy.Subscriber("hololens/out/user_labelled/depth", Image, self.depthSub)
self.texsub = rospy.Subscriber("hololens/out/user_labelled/labels", String, self.labelSub)
self.ftsub = rospy.Subscriber("hololens/out/finetune_signal", Bool, self.finetuneSigSub)
self.faultyRGBsub = rospy.Subscriber("/hololens/out/user_labelled/rgb", Image, self.faultyRGBSub)
self.ft_ckpt_pub = rospy.Publisher("finetune/overall_best_ckpt", String, queue_size=5)
self.ft_ack_pub = rospy.Publisher("finetune/ack_with_metrics", String, queue_size=5)
def imageSub(self, rosImage):
img = self.bridge.imgmsg_to_cv2(rosImage, desired_encoding='passthrough')
self.image = img[:, :, ::-1]
self.imageReceived = True
print("Received image")
if self.imageReceived and self.labelReceived and self.depthReceived:
self.saveResults(self.image, self.label, self.depth)
def labelSub(self, Text):
self.label = Text.data
self.labelReceived = True
print("Received label")
if self.imageReceived and self.labelReceived and self.depthReceived:
self.saveResults(self.image, self.label, self.depth)
def depthSub(self, dep):
dep = self.bridge.imgmsg_to_cv2(dep, desired_encoding='passthrough')
self.depth = dep[:, :]
self.depthReceived = True
print("Received depth")
if self.imageReceived and self.labelReceived and self.depthReceived:
self.saveResults(self.image, self.label, self.depth)
def faultyRGBSub(self, faulty_rgb_sample_msg):
img = self.bridge.imgmsg_to_cv2(faulty_rgb_sample_msg, desired_encoding='passthrough')
self.image = img[:, :, ::-1]
print("Received faulty RGB sample")
self.save_faulty_sample(self.image)
def finetuneSigSub(self, ft_sig_msg):
self.finetuneSignalReceived = ft_sig_msg.data
if self.finetuneSignalReceived:
print(f"Received finetune cmd...: {ft_sig_msg.data}")
# Implement finetuning logic here
curr_mAP50, overall_best_mAP50_info = self.dh_model.train_model()
print("Current mAP50: %f, Overall best mAP50: %f" % (curr_mAP50, overall_best_mAP50_info))
pub_data = {
'curr_ft_iter_num': self.dh_model.get_finetune_iter_num(),
'curr_mAP50': curr_mAP50,
'overall_best_mAP50_ft_iter': overall_best_mAP50_info['mAP50'],
'overall_best_mAP50': overall_best_mAP50_info['finetune_iter_num'],
}
best_ckpt = self.dh_model.select_model_ckpt()
self.ft_ckpt_pub.publish(best_ckpt)
self.ft_ack_pub.publish(self.stringify_json(pub_data))
print(f"PUB_DATA: {pub_data}")
print("Published ACK")
def stringify_json(self, data, indent=None):
try:
json_string = json.dumps(data, indent=indent)
return json_string
except TypeError as e:
raise ValueError(f"Data provided is not serializable to JSON: {str(e)}")
def saveResults(self, img, lbl, dep):
cwd = os.getcwd()
train_data_dir = os.path.abspath(os.path.expanduser(os.path.join(self.cfg.path, self.cfg.train, '..'))) # for hololens
title = datetime.now().strftime("Hololens_%d-%m-%Y_%H-%M-%S")
os.chdir(f"{train_data_dir}/images")
cv2.imwrite(title + ".png", img)
os.chdir(f"{train_data_dir}/depth")
cv2.imwrite(title + ".png", dep)
os.chdir(f"{train_data_dir}/labels")
os.chdir("./../labels")
with open(title + ".txt", 'w') as file:
# Flip YOLO labels horizontally as this comes flipped from hololens app
print("Flipping incoming labels horizontally due to Unity convention. ⚠️ (todo): change later in the hololens app source code")
flipped_labels = ""
lines = str(lbl).strip().split('\n')
for line in lines:
parts = line.strip().split()
class_idx, cx, cy, w, h = map(float, parts)
new_cx = 1.0 - cx
flipped_labels += f"{int(class_idx)} {new_cx} {cy} {w} {h}\n"
file.write(flipped_labels)
os.chdir(cwd)
self.imageReceived = False
self.depthReceived = False
self.labelReceived = False
self.finetuneSignalReceived = False
def save_faulty_sample(self, img):
cwd = os.getcwd()
train_data_dir = os.path.abspath(os.path.expanduser(os.path.join(self.cfg.path, self.cfg.train, '..'))) # for hololens
title = datetime.now().strftime("faulty_sample_collected_%d-%m-%Y_%H-%M-%S")
out_dir = f"{train_data_dir}/from_hololens_faulty_samples_with_pretrained_bboxes"
os.makedirs(out_dir)
os.chdir(out_dir) # Make sure this directory exists
cv2.imwrite(title + ".jpg", img)
print(f"Saved faulty sample to {train_data_dir}/faulty_samples/{title}.jpg")
os.chdir(cwd)