Skip to content

Commit bf6c8cf

Browse files
authored
RSDK-9294 - add extra to captureAll response (#4592)
1 parent dd5ac3a commit bf6c8cf

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

services/vision/client.go

+7
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ func (c *client) CaptureAllFromCamera(
293293
if err != nil {
294294
return viscapture.VisCapture{}, err
295295
}
296+
296297
var img image.Image
297298
if resp.Image.Image != nil {
298299
mimeType := utils.FormatToMimeType[resp.Image.GetFormat()]
@@ -302,11 +303,17 @@ func (c *client) CaptureAllFromCamera(
302303
}
303304
}
304305

306+
vcExtra := resp.Extra.AsMap()
307+
if len(vcExtra) == 0 {
308+
vcExtra = nil
309+
}
310+
305311
capt := viscapture.VisCapture{
306312
Image: img,
307313
Detections: dets,
308314
Classifications: class,
309315
Objects: objPCD,
316+
Extra: vcExtra,
310317
}
311318

312319
return capt, nil

services/vision/client_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func TestClient(t *testing.T) {
5656
det1 := objectdetection.NewDetection(image.Rectangle{}, 0.5, "yes")
5757
return viscapture.VisCapture{
5858
Detections: []objectdetection.Detection{det1},
59+
Extra: extra,
5960
}, nil
6061
}
6162
m := map[resource.Name]vision.Service{
@@ -142,15 +143,24 @@ func TestClient(t *testing.T) {
142143
test.That(t, err, test.ShouldBeNil)
143144
opts := viscapture.CaptureOptions{
144145
true,
145-
true, true, true,
146+
true,
147+
true,
148+
true,
146149
}
147-
capt, err := client.CaptureAllFromCamera(context.Background(), "", opts, map[string]interface{}{})
150+
extra := map[string]interface{}{"foo": "captureAll"}
151+
capt, err := client.CaptureAllFromCamera(context.Background(), "", opts, extra)
148152
test.That(t, err, test.ShouldBeNil)
149153

150154
test.That(t, capt.Detections, test.ShouldHaveLength, 1)
151155
test.That(t, capt.Detections[0].Label(), test.ShouldEqual, "yes")
152156
test.That(t, capt.Detections[0].Score(), test.ShouldEqual, 0.5)
157+
test.That(t, capt.Extra, test.ShouldResemble, extra)
153158
test.That(t, client.Close(context.Background()), test.ShouldBeNil)
159+
160+
// test with 'nil' extra
161+
capt, err = client.CaptureAllFromCamera(context.Background(), "", opts, nil)
162+
test.That(t, err, test.ShouldBeNil)
163+
test.That(t, capt.Extra, test.ShouldBeNil) // not necessarily true
154164
test.That(t, conn.Close(), test.ShouldBeNil)
155165
})
156166
}

services/vision/server.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
commonpb "go.viam.com/api/common/v1"
1010
camerapb "go.viam.com/api/component/camera/v1"
1111
pb "go.viam.com/api/service/vision/v1"
12+
"go.viam.com/utils/protoutils"
1213

1314
"go.viam.com/rdk/pointcloud"
14-
"go.viam.com/rdk/protoutils"
15+
rprotoutils "go.viam.com/rdk/protoutils"
1516
"go.viam.com/rdk/resource"
1617
"go.viam.com/rdk/rimage"
1718
"go.viam.com/rdk/utils"
@@ -239,6 +240,7 @@ func (server *serviceServer) CaptureAllFromCamera(
239240
ReturnClassifications: req.ReturnClassifications,
240241
ReturnObject: req.ReturnObjectPointClouds,
241242
}
243+
242244
capt, err := svc.CaptureAllFromCamera(ctx,
243245
req.CameraName,
244246
captOptions,
@@ -257,12 +259,16 @@ func (server *serviceServer) CaptureAllFromCamera(
257259
if err != nil {
258260
return nil, err
259261
}
260-
262+
extraProto, err := protoutils.StructToStructPb(capt.Extra)
263+
if err != nil {
264+
return nil, err
265+
}
261266
return &pb.CaptureAllFromCameraResponse{
262267
Image: imgProto,
263268
Detections: detsToProto(capt.Detections),
264269
Classifications: clasToProto(capt.Classifications),
265270
Objects: objProto,
271+
Extra: extraProto,
266272
}, nil
267273
}
268274

@@ -312,5 +318,5 @@ func (server *serviceServer) DoCommand(ctx context.Context,
312318
if err != nil {
313319
return nil, err
314320
}
315-
return protoutils.DoFromResourceServer(ctx, svc, req)
321+
return rprotoutils.DoFromResourceServer(ctx, svc, req)
316322
}

services/vision/server_test.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ func TestServerCaptureAllFromCamera(t *testing.T) {
157157
test.That(t, len(getDetectionsResp.GetDetections()), test.ShouldEqual, 1)
158158
test.That(t, getDetectionsResp.GetDetections()[0].GetClassName(), test.ShouldEqual, "yes")
159159

160-
captureRequest := pb.CaptureAllFromCameraRequest{
161-
Name: testVisionServiceName,
162-
ReturnDetections: true,
163-
}
164-
165160
injectVS.CaptureAllFromCameraFunc = func(ctx context.Context,
166161
cameraName string,
167162
opts viscapture.CaptureOptions,
@@ -170,12 +165,32 @@ func TestServerCaptureAllFromCamera(t *testing.T) {
170165
det1 := objectdetection.NewDetection(image.Rectangle{}, 0.5, "yes")
171166
return viscapture.VisCapture{
172167
Detections: []objectdetection.Detection{det1},
168+
Extra: extra,
173169
}, nil
174170
}
171+
172+
captureRequest := pb.CaptureAllFromCameraRequest{
173+
Name: testVisionServiceName,
174+
ReturnDetections: true,
175+
Extra: ext,
176+
}
177+
175178
captAllResp, err := server.CaptureAllFromCamera(context.Background(), &captureRequest)
176179
test.That(t, err, test.ShouldBeNil)
177180
test.That(t, len(captAllResp.Detections), test.ShouldEqual, 1)
178-
test.That(t, getDetectionsResp.Detections[0].GetClassName(), test.ShouldEqual, "yes")
181+
test.That(t, captAllResp.Detections[0].GetClassName(), test.ShouldEqual, "yes")
182+
test.That(t, captAllResp.Extra.AsMap(), test.ShouldResemble, map[string]interface{}{"foo": "GetDetections"})
183+
184+
captureRequest = pb.CaptureAllFromCameraRequest{
185+
Name: testVisionServiceName,
186+
ReturnDetections: true,
187+
Extra: nil,
188+
}
189+
captAllResp, err = server.CaptureAllFromCamera(context.Background(), &captureRequest)
190+
test.That(t, err, test.ShouldBeNil)
191+
test.That(t, len(captAllResp.Detections), test.ShouldEqual, 1)
192+
test.That(t, captAllResp.Detections[0].GetClassName(), test.ShouldEqual, "yes")
193+
test.That(t, len(captAllResp.Extra.AsMap()), test.ShouldEqual, 0)
179194

180195
test.ShouldResemble(captAllResp.Detections, getDetectionsResp.Detections)
181196
}

vision/viscapture/viscapture.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type VisCapture struct {
1515
Detections []objectdetection.Detection
1616
Classifications classification.Classifications
1717
Objects []*vision.Object
18+
Extra map[string]interface{}
1819
}
1920

2021
// CaptureOptions is a struct to configure CaptureAllFromCamera request.s.

0 commit comments

Comments
 (0)