-
Notifications
You must be signed in to change notification settings - Fork 110
/
obstacles_depth.go
361 lines (326 loc) · 10.7 KB
/
obstacles_depth.go
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//go:build !no_cgo
// Package obstaclesdepth uses an underlying depth camera to fulfill GetObjectPointClouds,
// using the method outlined in (Manduchi, Roberto, et al. "Obstacle detection and terrain classification
// for autonomous off-road navigation." Autonomous robots 18 (2005): 81-102.)
package obstaclesdepth
import (
"context"
"math"
"sort"
"strconv"
"sync"
"github.com/edaniels/golog"
"github.com/golang/geo/r3"
"github.com/muesli/clusters"
"github.com/muesli/kmeans"
"github.com/pkg/errors"
"github.com/viamrobotics/gostream"
"go.opencensus.io/trace"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/rimage"
"go.viam.com/rdk/rimage/transform"
"go.viam.com/rdk/robot"
svision "go.viam.com/rdk/services/vision"
"go.viam.com/rdk/spatialmath"
"go.viam.com/rdk/utils"
vision "go.viam.com/rdk/vision"
)
var model = resource.DefaultModelFamily.WithModel("obstacles_depth")
// ObsDepthConfig specifies the parameters to be used for the obstacle depth service.
type ObsDepthConfig struct {
Hmin float64 `json:"h_min_m"`
Hmax float64 `json:"h_max_m"`
ThetaMax float64 `json:"theta_max_deg"`
ReturnPCDs bool `json:"return_pcds"`
WithGeometries *bool `json:"with_geometries"`
}
// obsDepth is the underlying struct actually used by the service.
type obsDepth struct {
dm *rimage.DepthMap
obstaclePts [][]float64
depthPts [][]float64
hMin float64
hMax float64
sinTheta float64
intrinsics *transform.PinholeCameraIntrinsics
returnPCDs bool
withGeoms bool
k int
depthStream gostream.VideoStream
}
const (
// the first 3 consts are parameters from Manduchi et al.
defaultHmin = 0.0
defaultHmax = 1.0
defaultThetamax = 45
defaultK = 10 // default number of obstacle segments to create
sampleN = 4 // we sample 1 in every sampleN depth points
)
func init() {
resource.RegisterService(svision.API, model, resource.Registration[svision.Service, *ObsDepthConfig]{
DeprecatedRobotConstructor: func(ctx context.Context, r any, c resource.Config, logger golog.Logger) (svision.Service, error) {
attrs, err := resource.NativeConfig[*ObsDepthConfig](c)
if err != nil {
return nil, err
}
actualR, err := utils.AssertType[robot.Robot](r)
if err != nil {
return nil, err
}
return registerObstaclesDepth(ctx, c.ResourceName(), attrs, actualR, logger)
},
})
}
// Validate ensures all parts of the config are valid.
func (config *ObsDepthConfig) Validate(path string) ([]string, error) {
deps := []string{}
if config.Hmin >= config.Hmax && !(config.Hmin == 0 && config.Hmax == 0) {
return nil, errors.New("Hmin should be less than Hmax")
}
if config.Hmin < 0 {
return nil, errors.New("Hmin should be greater than or equal to 0")
}
if config.Hmax < 0 {
return nil, errors.New("Hmax should be greater than or equal to 0")
}
if config.ThetaMax < 0 || config.ThetaMax > 360 {
return nil, errors.New("ThetaMax should be in degrees between 0 and 360")
}
return deps, nil
}
func registerObstaclesDepth(
ctx context.Context,
name resource.Name,
conf *ObsDepthConfig,
r robot.Robot,
logger golog.Logger,
) (svision.Service, error) {
_, span := trace.StartSpan(ctx, "service::vision::registerObstacleDepth")
defer span.End()
if conf == nil {
return nil, errors.New("config for obstacles_depth cannot be nil")
}
// Use defaults if needed
if conf.Hmax == 0 {
conf.Hmax = defaultHmax
}
if conf.ThetaMax == 0 {
conf.ThetaMax = defaultThetamax
}
if conf.WithGeometries == nil {
wg := true
conf.WithGeometries = &wg
}
sinTheta := math.Sin(conf.ThetaMax * math.Pi / 180) // sin(radians(theta))
myObsDep := obsDepth{
hMin: 1000 * conf.Hmin, hMax: 1000 * conf.Hmax, sinTheta: sinTheta,
returnPCDs: conf.ReturnPCDs, k: defaultK, withGeoms: *conf.WithGeometries,
}
segmenter := myObsDep.buildObsDepth(logger) // does the thing
return svision.NewService(name, r, nil, nil, nil, segmenter)
}
// BuildObsDepth will check for intrinsics and determine how to build based on that.
func (o *obsDepth) buildObsDepth(logger golog.Logger) func(ctx context.Context, src camera.VideoSource) ([]*vision.Object, error) {
return func(ctx context.Context, src camera.VideoSource) ([]*vision.Object, error) {
props, err := src.Properties(ctx)
if err != nil {
logger.Warnw("could not find camera properties. obstacles depth started without camera's intrinsic parameters", "error", err)
return o.obsDepthNoIntrinsics(ctx, src)
}
if props.IntrinsicParams == nil {
logger.Warn("obstacles depth started but camera did not have intrinsic parameters")
return o.obsDepthNoIntrinsics(ctx, src)
}
o.intrinsics = props.IntrinsicParams
if o.withGeoms {
return o.obsDepthWithIntrinsics(ctx, src)
}
return o.obsDepthNoIntrinsics(ctx, src)
}
}
// buildObsDepthNoIntrinsics will return the median depth in the depth map as a Geometry point.
func (o *obsDepth) obsDepthNoIntrinsics(ctx context.Context, src camera.VideoSource) ([]*vision.Object, error) {
pic, release, err := camera.ReadImage(ctx, src)
if err != nil {
return nil, errors.Errorf("could not get image from %s", src)
}
defer release()
dm, err := rimage.ConvertImageToDepthMap(ctx, pic)
if err != nil {
return nil, errors.New("could not convert image to depth map")
}
depData := dm.Data()
if len(depData) == 0 {
return nil, errors.New("could not get info from depth map")
}
// Sort the depth data [smallest...largest]
sort.Slice(depData, func(i, j int) bool {
return depData[i] < depData[j]
})
med := int(0.5 * float64(len(depData)))
pt := spatialmath.NewPoint(r3.Vector{X: 0, Y: 0, Z: float64(depData[med])}, "")
toReturn := make([]*vision.Object, 1)
toReturn[0] = &vision.Object{Geometry: pt}
return toReturn, nil
}
// buildObsDepthWithIntrinsics will use the methodology in Manduchi et al. to find obstacle points
// before clustering and projecting those points into 3D obstacles.
func (o *obsDepth) obsDepthWithIntrinsics(ctx context.Context, src camera.VideoSource) ([]*vision.Object, error) {
// Check if we have intrinsics here. If not, don't even try
if o.intrinsics == nil {
return nil, errors.New("tried to build obstacles depth with intrinsics but no instrinsics found")
}
if o.depthStream == nil {
depthStream, err := src.Stream(ctx)
if err != nil {
return nil, errors.Errorf("could not get stream from %s", src)
}
o.depthStream = depthStream
}
pic, release, err := o.depthStream.Next(ctx)
if err != nil {
return nil, errors.Errorf("could not get image from stream %s", o.depthStream)
}
defer release()
dm, err := rimage.ConvertImageToDepthMap(ctx, pic)
if err != nil {
return nil, errors.New("could not convert image to depth map")
}
w, h := dm.Width(), dm.Height()
o.dm = dm
var wg sync.WaitGroup
var lock sync.Mutex
obstaclePoints := make([][]float64, 0, w*h/sampleN)
o.makePointList()
for i := 0; i < len(o.depthPts); i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if o.isObstaclePoint(o.depthPts[i]) {
lock.Lock()
obstaclePoints = append(obstaclePoints, o.depthPts[i])
lock.Unlock()
}
}(i)
}
wg.Wait()
o.obstaclePts = obstaclePoints
// Cluster the points in 3D
boxes, outClusters, err := o.performKMeans3D(o.k)
if err != nil {
return nil, err
}
// Packaging the return depending on if they want PCDs
n := int(math.Min(float64(len(outClusters)), float64(len(boxes)))) // should be same len but for safety
toReturn := make([]*vision.Object, n)
for i := 0; i < n; i++ { // for each cluster/box make an object
if o.returnPCDs {
pcdToReturn := pointcloud.NewWithPrealloc(len(outClusters[i].Observations))
basicData := pointcloud.NewBasicData()
for _, pt := range outClusters[i].Observations {
if len(pt.Coordinates()) >= 3 {
vec := r3.Vector{X: pt.Coordinates()[0], Y: pt.Coordinates()[1], Z: pt.Coordinates()[2]}
err = pcdToReturn.Set(vec, basicData)
if err != nil {
return nil, err
}
}
}
toReturn[i] = &vision.Object{PointCloud: pcdToReturn, Geometry: boxes[i]}
} else {
toReturn[i] = &vision.Object{Geometry: boxes[i]}
}
}
return toReturn, nil
}
// isCompatible will check compatibility between 2 points.
// as defined by Manduchi et al.
func (o *obsDepth) isCompatible(p1, p2 []float64) bool {
if len(p1) < 3 || len(p2) < 3 {
return false
}
xdist, ydist := math.Abs(p1[0]-p2[0]), math.Abs(p1[1]-p2[1])
zdist := math.Abs(p1[2] - p2[2])
dist := math.Sqrt((xdist * xdist) + (ydist * ydist) + (zdist * zdist))
if ydist < o.hMin || ydist > o.hMax {
return false
}
if ydist/dist < o.sinTheta {
return false
}
return true
}
// performKMeans3D will do k-means clustering on projected obstacle points.
func (o *obsDepth) performKMeans3D(k int) ([]spatialmath.Geometry, clusters.Clusters, error) {
var observations3D clusters.Observations
for _, pt := range o.obstaclePts {
outX, outY, outZ := o.intrinsics.PixelToPoint(pt[0], pt[1], pt[2])
observations3D = append(observations3D, clusters.Coordinates{outX, outY, outZ})
}
km := kmeans.New()
clusters, err := km.Partition(observations3D, k)
if err != nil {
return nil, nil, err
}
boxes := make([]spatialmath.Geometry, 0, len(clusters))
for i, c := range clusters {
xmax, ymax, zmax := math.Inf(-1), math.Inf(-1), math.Inf(-1)
xmin, ymin, zmin := math.Inf(1), math.Inf(1), math.Inf(1)
for _, pt := range c.Observations {
x, y, z := pt.Coordinates().Coordinates()[0], pt.Coordinates().Coordinates()[1], pt.Coordinates().Coordinates()[2]
if x < xmin {
xmin = x
}
if x > xmax {
xmax = x
}
if y < ymin {
ymin = y
}
if y > ymax {
ymax = y
}
if z < zmin {
zmin = z
}
if z > zmax {
zmax = z
}
}
// Make a box from those bounds and add it in
xdiff, ydiff, zdiff := xmax-xmin, ymax-ymin, zmax-zmin
xc, yc, zc := (xmin+xmax)/2, (ymin+ymax)/2, (zmin+zmax)/2
pose := spatialmath.NewPoseFromPoint(r3.Vector{xc, yc, zc})
box, err := spatialmath.NewBox(pose, r3.Vector{xdiff, ydiff, zdiff}, strconv.Itoa(i))
if err != nil {
return nil, nil, err
}
boxes = append(boxes, box)
}
return boxes, clusters, err
}
// makePointList will populate o.depthPts with the depth data.
func (o *obsDepth) makePointList() [][]float64 {
width, height := o.dm.Width(), o.dm.Height()
out := make([][]float64, 0, width*height)
for i := 0; i < width; i += sampleN {
for j := 0; j < height; j++ {
out = append(out, []float64{float64(i), float64(j), float64(o.dm.GetDepth(i, j))})
}
}
o.depthPts = out
return out
}
func (o *obsDepth) isObstaclePoint(candidate []float64) bool {
for _, pt := range o.depthPts {
if candidate[0] == pt[0] && candidate[1] == pt[1] {
continue
}
if o.isCompatible(candidate, pt) {
return true
}
}
return false
}