Skip to content

Commit

Permalink
Use imgbuf bytes directly for a robot client test rather than encode …
Browse files Browse the repository at this point in the history
…from img; Fix prev test bug
  • Loading branch information
hexbabe committed Nov 15, 2024
1 parent 9a9612f commit 7e710a4
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 19 deletions.
6 changes: 3 additions & 3 deletions components/camera/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type Camera interface {
// Or try to directly decode into an image.Image:
//
// myCamera, err := camera.FromRobot(machine, "my_camera")
// img, err = camera.ImageFromVideoSource(context.Background(), utils.MimeTypeJPEG, nil, myCamera)
// img, err = camera.DecodeImageFromCamera(context.Background(), utils.MimeTypeJPEG, nil, myCamera)
//
// Images example:
//
Expand Down Expand Up @@ -159,8 +159,8 @@ func ReadImage(ctx context.Context, src gostream.VideoSource) (image.Image, func
return gostream.ReadImage(ctx, src)
}

// GoImageFromCamera retrieves image bytes from a camera resource and serializes it as an image.Image.
func GoImageFromCamera(ctx context.Context, mimeType string, extra map[string]interface{}, cam Camera) (image.Image, error) {
// DecodeImageFromCamera retrieves image bytes from a camera resource and serializes it as an image.Image.
func DecodeImageFromCamera(ctx context.Context, mimeType string, extra map[string]interface{}, cam Camera) (image.Image, error) {
resBytes, resMetadata, err := cam.Image(ctx, mimeType, extra)
if err != nil {
return nil, fmt.Errorf("could not get image bytes from camera: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions components/camera/camera_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestCameraWithNoProjector(t *testing.T) {
_, got := pc.At(0, 0, 0)
test.That(t, got, test.ShouldBeTrue)

img, err := camera.GoImageFromCamera(context.Background(), rutils.WithLazyMIMEType(rutils.MimeTypePNG), nil, noProj2)
img, err := camera.DecodeImageFromCamera(context.Background(), rutils.WithLazyMIMEType(rutils.MimeTypePNG), nil, noProj2)
test.That(t, err, test.ShouldBeNil)

test.That(t, img.Bounds().Dx(), test.ShouldEqual, 1280)
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestCameraWithProjector(t *testing.T) {
_, got := pc.At(0, 0, 0)
test.That(t, got, test.ShouldBeTrue)

img, err := camera.GoImageFromCamera(context.Background(), rutils.MimeTypePNG, nil, cam2)
img, err := camera.DecodeImageFromCamera(context.Background(), rutils.MimeTypePNG, nil, cam2)
test.That(t, err, test.ShouldBeNil)

test.That(t, err, test.ShouldBeNil)
Expand Down
2 changes: 1 addition & 1 deletion components/camera/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (c *client) Stream(
return
}

img, err := GoImageFromCamera(streamCtx, mimeTypeFromCtx, nil, c)
img, err := DecodeImageFromCamera(streamCtx, mimeTypeFromCtx, nil, c)
if err != nil {
for _, handler := range errHandlers {
handler(streamCtx, err)
Expand Down
10 changes: 5 additions & 5 deletions components/camera/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ func TestClient(t *testing.T) {
test.That(t, err, test.ShouldBeNil)
camera1Client, err := camera.NewClientFromConn(context.Background(), conn, "", camera.Named(testCameraName), logger)
test.That(t, err, test.ShouldBeNil)
frame, err := camera.GoImageFromCamera(context.Background(), rutils.MimeTypeRawRGBA, nil, camera1Client)
frame, err := camera.DecodeImageFromCamera(context.Background(), rutils.MimeTypeRawRGBA, nil, camera1Client)
test.That(t, err, test.ShouldBeNil)
compVal, _, err := rimage.CompareImages(img, frame)
test.That(t, err, test.ShouldBeNil)
test.That(t, compVal, test.ShouldEqual, 0) // exact copy, no color conversion
_, err = camera.GoImageFromCamera(context.Background(), rutils.MimeTypeRawRGBA, map[string]interface{}{"empty": true}, camera1Client)
_, err = camera.DecodeImageFromCamera(context.Background(), rutils.MimeTypeRawRGBA, map[string]interface{}{"empty": true}, camera1Client)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "received empty bytes from Image method")

Expand Down Expand Up @@ -228,7 +228,7 @@ func TestClient(t *testing.T) {
test.That(t, err, test.ShouldBeNil)

ctx := context.Background()
frame, err := camera.GoImageFromCamera(ctx, rutils.WithLazyMIMEType(rutils.MimeTypePNG), nil, client)
frame, err := camera.DecodeImageFromCamera(ctx, rutils.WithLazyMIMEType(rutils.MimeTypePNG), nil, client)
test.That(t, err, test.ShouldBeNil)
dm, err := rimage.ConvertImageToDepthMap(context.Background(), frame)
test.That(t, err, test.ShouldBeNil)
Expand Down Expand Up @@ -469,15 +469,15 @@ func TestClientLazyImage(t *testing.T) {
test.That(t, err, test.ShouldBeNil)

ctx := context.Background()
frame, err := camera.GoImageFromCamera(ctx, rutils.MimeTypePNG, nil, camera1Client)
frame, err := camera.DecodeImageFromCamera(ctx, rutils.MimeTypePNG, nil, camera1Client)
test.That(t, err, test.ShouldBeNil)
// Should always lazily decode
test.That(t, frame, test.ShouldHaveSameTypeAs, &rimage.LazyEncodedImage{})
frameLazy := frame.(*rimage.LazyEncodedImage)
test.That(t, frameLazy.RawData(), test.ShouldResemble, imgBuf.Bytes())

ctx = context.Background()
frame, err = camera.GoImageFromCamera(ctx, rutils.WithLazyMIMEType(rutils.MimeTypePNG), nil, camera1Client)
frame, err = camera.DecodeImageFromCamera(ctx, rutils.WithLazyMIMEType(rutils.MimeTypePNG), nil, camera1Client)
test.That(t, err, test.ShouldBeNil)
test.That(t, frame, test.ShouldHaveSameTypeAs, &rimage.LazyEncodedImage{})
frameLazy = frame.(*rimage.LazyEncodedImage)
Expand Down
2 changes: 1 addition & 1 deletion components/camera/fake/camera_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestRTPPassthrough(t *testing.T) {
cam, err := NewCamera(context.Background(), nil, cfg, logger)
test.That(t, err, test.ShouldBeNil)

img, err := camera.GoImageFromCamera(context.Background(), utils.MimeTypeRawRGBA, nil, cam)
img, err := camera.DecodeImageFromCamera(context.Background(), utils.MimeTypeRawRGBA, nil, cam)
test.That(t, err, test.ShouldBeNil)
// GetImage returns the world jpeg
test.That(t, img.Bounds(), test.ShouldResemble, image.Rectangle{Max: image.Point{X: 480, Y: 270}})
Expand Down
2 changes: 1 addition & 1 deletion components/camera/fake/image_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestColorOddResolution(t *testing.T) {
cam, err := newCamera(ctx, resource.Name{API: camera.API}, cfg, logger)
test.That(t, err, test.ShouldBeNil)

strmImg, err := camera.GoImageFromCamera(ctx, utils.MimeTypeRawRGBA, nil, cam)
strmImg, err := camera.DecodeImageFromCamera(ctx, utils.MimeTypeRawRGBA, nil, cam)
test.That(t, err, test.ShouldBeNil)

expectedBounds := image.Rect(0, 0, img.Bounds().Dx()-1, img.Bounds().Dy()-1)
Expand Down
8 changes: 3 additions & 5 deletions robot/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,7 @@ func TestStatusClient(t *testing.T) {
test.That(t, png.Encode(&imgBuf, img), test.ShouldBeNil)

injectCamera.ImageFunc = func(ctx context.Context, mimeType string, extra map[string]interface{}) ([]byte, camera.ImageMetadata, error) {
resBytes, err := rimage.EncodeImage(ctx, img, mimeType)
test.That(t, err, test.ShouldBeNil)
return resBytes, camera.ImageMetadata{MimeType: mimeType}, nil
return imgBuf.Bytes(), camera.ImageMetadata{MimeType: rutils.MimeTypePNG}, nil
}

injectInputDev := &inject.InputController{}
Expand Down Expand Up @@ -510,7 +508,7 @@ func TestStatusClient(t *testing.T) {
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "not found")
test.That(t, imgBytes, test.ShouldBeNil)
test.That(t, metadata, test.ShouldEqual, camera.ImageMetadata{})
test.That(t, metadata, test.ShouldResemble, camera.ImageMetadata{})

gripper1, err := gripper.FromRobot(client, "gripper1")
test.That(t, err, test.ShouldBeNil)
Expand Down Expand Up @@ -586,7 +584,7 @@ func TestStatusClient(t *testing.T) {

camera1, err = camera.FromRobot(client, "camera1")
test.That(t, err, test.ShouldBeNil)
frame, err := camera.GoImageFromCamera(context.Background(), rutils.MimeTypeRawRGBA, nil, camera1)
frame, err := camera.DecodeImageFromCamera(context.Background(), rutils.MimeTypeRawRGBA, nil, camera1)
test.That(t, err, test.ShouldBeNil)
compVal, _, err := rimage.CompareImages(img, frame)
test.That(t, err, test.ShouldBeNil)
Expand Down
3 changes: 2 additions & 1 deletion robot/impl/local_robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/golang/geo/r3"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"

// registers all components.
commonpb "go.viam.com/api/common/v1"
armpb "go.viam.com/api/component/arm/v1"
Expand Down Expand Up @@ -93,7 +94,7 @@ func TestConfig1(t *testing.T) {
c1, err := camera.FromRobot(r, "c1")
test.That(t, err, test.ShouldBeNil)
test.That(t, c1.Name(), test.ShouldResemble, camera.Named("c1"))
pic, err := camera.GoImageFromCamera(context.Background(), rutils.MimeTypeJPEG, nil, c1)
pic, err := camera.DecodeImageFromCamera(context.Background(), rutils.MimeTypeJPEG, nil, c1)
test.That(t, err, test.ShouldBeNil)

bounds := pic.Bounds()
Expand Down

0 comments on commit 7e710a4

Please sign in to comment.