Skip to content

Commit

Permalink
GoImageFromCamera -> DecodeImageFromCamera
Browse files Browse the repository at this point in the history
  • Loading branch information
hexbabe committed Nov 14, 2024
1 parent d31b434 commit 003c379
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 17 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
4 changes: 2 additions & 2 deletions components/camera/transformpipeline/mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ type resizeConfig struct {
}

type resizeSource struct {
src camera.VideoSource
src camera.Camera
stream camera.ImageType
height int
width int
}

// newResizeTransform creates a new resize transform.
func newResizeTransform(
ctx context.Context, source camera.VideoSource, stream camera.ImageType, am utils.AttributeMap,
ctx context.Context, source camera.Camera, stream camera.ImageType, am utils.AttributeMap,
) (camera.VideoSource, camera.ImageType, error) {
conf, err := resource.TransformAttributeMap[*resizeConfig](am)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion robot/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,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 003c379

Please sign in to comment.