-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
320 lines (294 loc) · 8.66 KB
/
client.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
package vision
import (
"bytes"
"context"
"fmt"
"image"
"github.com/pkg/errors"
"go.opencensus.io/trace"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/service/vision/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/gostream"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/pointcloud"
rprotoutils "go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/rimage"
"go.viam.com/rdk/utils"
"go.viam.com/rdk/vision"
"go.viam.com/rdk/vision/classification"
objdet "go.viam.com/rdk/vision/objectdetection"
"go.viam.com/rdk/vision/viscapture"
)
// client implements VisionServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
name string
client pb.VisionServiceClient
logger logging.Logger
}
// NewClientFromConn constructs a new Client from connection passed in.
func NewClientFromConn(
ctx context.Context,
conn rpc.ClientConn,
remoteName string,
name resource.Name,
logger logging.Logger,
) (Service, error) {
grpcClient := pb.NewVisionServiceClient(conn)
c := &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
client: grpcClient,
logger: logger,
}
return c, nil
}
func (c *client) DetectionsFromCamera(
ctx context.Context,
cameraName string,
extra map[string]interface{},
) ([]objdet.Detection, error) {
ctx, span := trace.StartSpan(ctx, "service::vision::client::DetectionsFromCamera")
defer span.End()
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetDetectionsFromCamera(ctx, &pb.GetDetectionsFromCameraRequest{
Name: c.name,
CameraName: cameraName,
Extra: ext,
})
if err != nil {
return nil, err
}
return protoToDets(resp.Detections)
}
func (c *client) Detections(ctx context.Context, img image.Image, extra map[string]interface{},
) ([]objdet.Detection, error) {
ctx, span := trace.StartSpan(ctx, "service::vision::client::Detections")
defer span.End()
if img == nil {
return nil, errors.New("nil image input to given client.Detections")
}
mimeType := gostream.MIMETypeHint(ctx, utils.MimeTypeJPEG)
imgBytes, err := rimage.EncodeImage(ctx, img, mimeType)
if err != nil {
return nil, err
}
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetDetections(ctx, &pb.GetDetectionsRequest{
Name: c.name,
Image: imgBytes,
Width: int64(img.Bounds().Dx()),
Height: int64(img.Bounds().Dy()),
MimeType: mimeType,
Extra: ext,
})
if err != nil {
return nil, err
}
return protoToDets(resp.Detections)
}
func protoToDets(protoDets []*pb.Detection) ([]objdet.Detection, error) {
detections := make([]objdet.Detection, 0, len(protoDets))
for _, d := range protoDets {
if d.XMin == nil || d.XMax == nil || d.YMin == nil || d.YMax == nil {
return nil, fmt.Errorf("invalid detection %+v", d)
}
box := image.Rect(int(*d.XMin), int(*d.YMin), int(*d.XMax), int(*d.YMax))
det := objdet.NewDetection(box, d.Confidence, d.ClassName)
detections = append(detections, det)
}
return detections, nil
}
func (c *client) ClassificationsFromCamera(
ctx context.Context,
cameraName string,
n int,
extra map[string]interface{},
) (classification.Classifications, error) {
ctx, span := trace.StartSpan(ctx, "service::vision::client::ClassificationsFromCamera")
defer span.End()
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetClassificationsFromCamera(ctx, &pb.GetClassificationsFromCameraRequest{
Name: c.name,
CameraName: cameraName,
N: int32(n),
Extra: ext,
})
if err != nil {
return nil, err
}
return protoToClas(resp.Classifications), nil
}
func (c *client) Classifications(ctx context.Context, img image.Image,
n int, extra map[string]interface{},
) (classification.Classifications, error) {
ctx, span := trace.StartSpan(ctx, "service::vision::client::Classifications")
defer span.End()
if img == nil {
return nil, errors.New("nil image input to given client.Classifications")
}
mimeType := gostream.MIMETypeHint(ctx, utils.MimeTypeJPEG)
imgBytes, err := rimage.EncodeImage(ctx, img, mimeType)
if err != nil {
return nil, err
}
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetClassifications(ctx, &pb.GetClassificationsRequest{
Name: c.name,
Image: imgBytes,
Width: int32(img.Bounds().Dx()),
Height: int32(img.Bounds().Dy()),
MimeType: mimeType,
N: int32(n),
Extra: ext,
})
if err != nil {
return nil, err
}
return protoToClas(resp.Classifications), nil
}
func protoToClas(protoClass []*pb.Classification) classification.Classifications {
classifications := make([]classification.Classification, 0, len(protoClass))
for _, c := range protoClass {
classif := classification.NewClassification(c.Confidence, c.ClassName)
classifications = append(classifications, classif)
}
return classifications
}
func (c *client) GetObjectPointClouds(
ctx context.Context,
cameraName string,
extra map[string]interface{},
) ([]*vision.Object, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetObjectPointClouds(ctx, &pb.GetObjectPointCloudsRequest{
Name: c.name,
CameraName: cameraName,
MimeType: utils.MimeTypePCD,
Extra: ext,
})
if err != nil {
return nil, err
}
if resp.MimeType != utils.MimeTypePCD {
return nil, fmt.Errorf("unknown pc mime type %s", resp.MimeType)
}
return protoToObjects(resp.Objects)
}
func protoToObjects(pco []*commonpb.PointCloudObject) ([]*vision.Object, error) {
objects := make([]*vision.Object, len(pco))
for i, o := range pco {
pc, err := pointcloud.ReadPCD(bytes.NewReader(o.PointCloud))
if err != nil {
return nil, err
}
// Sets the label to the first non-empty label of any geometry; defaults to the empty string.
label := func() string {
for _, g := range o.Geometries.GetGeometries() {
if g.GetLabel() != "" {
return g.GetLabel()
}
}
return ""
}()
if len(o.Geometries.Geometries) >= 1 {
objects[i], err = vision.NewObjectWithLabel(pc, label, o.Geometries.GetGeometries()[0])
} else {
objects[i], err = vision.NewObjectWithLabel(pc, label, nil)
}
if err != nil {
return nil, err
}
}
return objects, nil
}
func (c *client) GetProperties(ctx context.Context, extra map[string]interface{}) (*Properties, error) {
ctx, span := trace.StartSpan(ctx, "service::vision::client::GetProperties")
defer span.End()
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetProperties(ctx, &pb.GetPropertiesRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return nil, err
}
return &Properties{resp.ClassificationsSupported, resp.DetectionsSupported, resp.ObjectPointCloudsSupported}, nil
}
func (c *client) CaptureAllFromCamera(
ctx context.Context,
cameraName string,
captureOptions viscapture.CaptureOptions,
extra map[string]interface{},
) (viscapture.VisCapture, error) {
ctx, span := trace.StartSpan(ctx, "service::vision::client::ClassificationsFromCamera")
defer span.End()
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return viscapture.VisCapture{}, err
}
resp, err := c.client.CaptureAllFromCamera(ctx, &pb.CaptureAllFromCameraRequest{
Name: c.name,
CameraName: cameraName,
ReturnImage: captureOptions.ReturnImage,
ReturnDetections: captureOptions.ReturnDetections,
ReturnClassifications: captureOptions.ReturnClassifications,
ReturnObjectPointClouds: captureOptions.ReturnObject,
Extra: ext,
})
if err != nil {
return viscapture.VisCapture{}, err
}
dets, err := protoToDets(resp.Detections)
if err != nil {
return viscapture.VisCapture{}, err
}
class := protoToClas(resp.Classifications)
objPCD, err := protoToObjects(resp.Objects)
if err != nil {
return viscapture.VisCapture{}, err
}
var img image.Image
if resp.Image.Image != nil {
mimeType := utils.FormatToMimeType[resp.Image.GetFormat()]
img, err = rimage.DecodeImage(ctx, resp.Image.Image, mimeType)
if err != nil {
return viscapture.VisCapture{}, err
}
}
capt := viscapture.VisCapture{
Image: img,
Detections: dets,
Classifications: class,
Objects: objPCD,
}
return capt, nil
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
ctx, span := trace.StartSpan(ctx, "service::vision::client::DoCommand")
defer span.End()
return rprotoutils.DoFromResourceClient(ctx, c.client, c.name, cmd)
}