-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlive_dope_realsense.py
200 lines (154 loc) · 5.8 KB
/
live_dope_realsense.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
from cuboid import *
from detector_dope import *
import yaml
import pyrealsense2 as rs
from PIL import Image
from PIL import ImageDraw
import time
### Code to visualize the neural network output
def DrawLine(point1, point2, lineColor, lineWidth):
'''Draws line on image'''
global g_draw
if not point1 is None and point2 is not None:
g_draw.line([point1, point2], fill=lineColor, width=lineWidth)
def DrawDot(point, pointColor, pointRadius):
'''Draws dot (filled circle) on image'''
global g_draw
if point is not None:
xy = [
point[0] - pointRadius,
point[1] - pointRadius,
point[0] + pointRadius,
point[1] + pointRadius
]
g_draw.ellipse(xy,
fill=pointColor,
outline=pointColor
)
def DrawCube(points, color=(255, 0, 0)):
'''
Draws cube with a thick solid line across
the front top edge and an X on the top face.
'''
lineWidthForDrawing = 2
# draw front
DrawLine(points[0], points[1], color, lineWidthForDrawing)
DrawLine(points[1], points[2], color, lineWidthForDrawing)
DrawLine(points[3], points[2], color, lineWidthForDrawing)
DrawLine(points[3], points[0], color, lineWidthForDrawing)
# draw back
DrawLine(points[4], points[5], color, lineWidthForDrawing)
DrawLine(points[6], points[5], color, lineWidthForDrawing)
DrawLine(points[6], points[7], color, lineWidthForDrawing)
DrawLine(points[4], points[7], color, lineWidthForDrawing)
# draw sides
DrawLine(points[0], points[4], color, lineWidthForDrawing)
DrawLine(points[7], points[3], color, lineWidthForDrawing)
DrawLine(points[5], points[1], color, lineWidthForDrawing)
DrawLine(points[2], points[6], color, lineWidthForDrawing)
# draw dots
DrawDot(points[0], pointColor=color, pointRadius=4)
DrawDot(points[1], pointColor=color, pointRadius=4)
# draw x on the top
DrawLine(points[0], points[5], color, lineWidthForDrawing)
DrawLine(points[1], points[4], color, lineWidthForDrawing)
# Settings
config_name = "my_config_realsense.yaml"
exposure_val = 500
yaml_path = 'cfg/{}'.format(config_name)
with open(yaml_path, 'r') as stream:
try:
print("Loading DOPE parameters from '{}'...".format(yaml_path))
params = yaml.load(stream)
print(' Parameters loaded.')
except yaml.YAMLError as exc:
print(exc)
models = {}
pnp_solvers = {}
pub_dimension = {}
draw_colors = {}
# Initialize parameters
matrix_camera = np.zeros((3,3))
matrix_camera[0,0] = params["camera_settings"]['fx']
matrix_camera[1,1] = params["camera_settings"]['fy']
matrix_camera[0,2] = params["camera_settings"]['cx']
matrix_camera[1,2] = params["camera_settings"]['cy']
matrix_camera[2,2] = 1
dist_coeffs = np.zeros((4,1))
if "dist_coeffs" in params["camera_settings"]:
dist_coeffs = np.array(params["camera_settings"]['dist_coeffs'])
config_detect = lambda: None
config_detect.mask_edges = 1
config_detect.mask_faces = 1
config_detect.vertex = 1
config_detect.threshold = 0.5
config_detect.softmax = 1000
config_detect.thresh_angle = params['thresh_angle']
config_detect.thresh_map = params['thresh_map']
config_detect.sigma = params['sigma']
config_detect.thresh_points = params["thresh_points"]
# For each object to detect, load network model, create PNP solver, and start ROS publishers
for model in params['weights']:
models[model] = \
ModelData(
model,
"backup/dope/" + params['weights'][model]
)
models[model].load_net_model()
draw_colors[model] = tuple(params["draw_colors"][model])
pnp_solvers[model] = \
CuboidPNPSolver(
model,
matrix_camera,
Cuboid3d(params['dimensions'][model]),
dist_coeffs=dist_coeffs
)
# RealSense Start
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
profile = pipeline.start(config)
# Setting exposure
s = profile.get_device().query_sensors()[1]
s.set_option(rs.option.exposure, exposure_val)
while True:
# Reading image from camera
# t_start = time.time()
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
if not color_frame:
continue
img = np.asanyarray(color_frame.get_data())
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Copy and draw image
img_copy = img.copy()
im = Image.fromarray(img_copy)
g_draw = ImageDraw.Draw(im)
for m in models:
# Detect object
results = ObjectDetector.detect_object_in_image(
models[m].net,
pnp_solvers[m],
img,
config_detect
)
# Overlay cube on image
for i_r, result in enumerate(results):
if result["location"] is None:
continue
loc = result["location"]
ori = result["quaternion"]
# Draw the cube
if None not in result['projected_points']:
points2d = []
for pair in result['projected_points']:
points2d.append(tuple(pair))
DrawCube(points2d, draw_colors[m])
open_cv_image = np.array(im)
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR)
cv2.imshow('Open_cv_image', open_cv_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# t_end = time.time()
# print(1/(t_end-t_start))