Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DATA-535] Create an orbslam integration test that runs mono YCbCr im… #1502

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .artifact/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -50802,6 +50802,70 @@
}
}
},
"mock_mono_camera": {
"rgb": {
"0.png": {
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"1.png": {
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"10.png": {
"hash": "97cb4afc22754e0433303ae8b44d380f",
"size": 813831
},
"11.png": {
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"12.png": {
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"13.png": {
"hash": "c40ec042eb926ed5e012c0a505b0453d",
"size": 787645
},
"14.png": {
"hash": "87f3db7908d0455ffe3b2a51093abe16",
"size": 824040
},
"2.png": {
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"3.png": {
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"4.png": {
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"5.png": {
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"6.png": {
"hash": "d17c07b754a6c274915a94a2dd978c02",
"size": 819850
},
"7.png": {
"hash": "87f3db7908d0455ffe3b2a51093abe16",
"size": 824040
},
"8.png": {
"hash": "c40ec042eb926ed5e012c0a505b0453d",
"size": 787645
},
"9.png": {
"hash": "97cb4afc22754e0433303ae8b44d380f",
"size": 813831
}
}
},
"temp_mock_camera": {
"color": {
"0.png": {
Expand Down
48 changes: 48 additions & 0 deletions rimage/image_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,51 @@ func EdgeHysteresisFiltering(mag *mat.Dense, low, high float64) (*image.Gray, er
}
return edges, nil
}

// ImageToYCbCrForTesting converts an image to YCbCr. It is only to be used for testing.
func ImageToYCbCrForTesting(dst *image.YCbCr, src image.Image) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find another was to convert to YCbCr, so I exported this function. Let me know if you have another idea!

Copy link
Member

@bhaney bhaney Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably use the draw.Draw function to do this

import "image/draw"

func MakeYCbCr(pic image.Image) *image.YCbCr {
    result := image.NewYCbCr(pic.Bounds())
    draw.Draw(result, result.Bounds(), pic, pic.Bounds().Min, draw.Src)
    return result
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like I can do this, since image.YCbCr doesn't implement draw.Image (missing method Set).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow, yea it looks like YCbCr isn't pixel addressable! So, yea, this function seems like the best bet for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found an alternative - but if you would like to stick to using one set of images (the png sources) then what you have now seems like the way to go

if dst == nil {
panic("dst can't be nil")
}

yuvImg, ok := src.(*image.YCbCr)
if ok {
*dst = *yuvImg
return
}

bounds := src.Bounds()
dy := bounds.Dy()
dx := bounds.Dx()
flat := dy * dx

if len(dst.Y)+len(dst.Cb)+len(dst.Cr) < 3*flat {
i0 := 1 * flat
i1 := 2 * flat
i2 := 3 * flat
if cap(dst.Y) < i2 {
dst.Y = make([]uint8, i2)
}
dst.Y = dst.Y[:i0]
dst.Cb = dst.Y[i0:i1]
dst.Cr = dst.Y[i1:i2]
}
dst.SubsampleRatio = image.YCbCrSubsampleRatio444
dst.YStride = dx
dst.CStride = dx
dst.Rect = bounds

i := 0
for yi := 0; yi < dy; yi++ {
for xi := 0; xi < dx; xi++ {
// TODO(erh): probably try to get the alpha value with something like
// https://en.wikipedia.org/wiki/Alpha_compositing
r, g, b, _ := src.At(xi, yi).RGBA()
yy, cb, cr := color.RGBToYCbCr(uint8(r/256), uint8(g/256), uint8(b/256))
dst.Y[i] = yy
dst.Cb[i] = cb
dst.Cr[i] = cr
i++
}
}
}
52 changes: 2 additions & 50 deletions rimage/image_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rimage
import (
"fmt"
"image"
"image/color"
"math"
"testing"

Expand Down Expand Up @@ -116,59 +115,12 @@ func BenchmarkConvertImage(b *testing.B) {
}
}

func imageToYCbCr(dst *image.YCbCr, src image.Image) {
if dst == nil {
panic("dst can't be nil")
}

yuvImg, ok := src.(*image.YCbCr)
if ok {
*dst = *yuvImg
return
}

bounds := src.Bounds()
dy := bounds.Dy()
dx := bounds.Dx()
flat := dy * dx

if len(dst.Y)+len(dst.Cb)+len(dst.Cr) < 3*flat {
i0 := 1 * flat
i1 := 2 * flat
i2 := 3 * flat
if cap(dst.Y) < i2 {
dst.Y = make([]uint8, i2)
}
dst.Y = dst.Y[:i0]
dst.Cb = dst.Y[i0:i1]
dst.Cr = dst.Y[i1:i2]
}
dst.SubsampleRatio = image.YCbCrSubsampleRatio444
dst.YStride = dx
dst.CStride = dx
dst.Rect = bounds

i := 0
for yi := 0; yi < dy; yi++ {
for xi := 0; xi < dx; xi++ {
// TODO(erh): probably try to get the alpha value with something like
// https://en.wikipedia.org/wiki/Alpha_compositing
r, g, b, _ := src.At(xi, yi).RGBA()
yy, cb, cr := color.RGBToYCbCr(uint8(r/256), uint8(g/256), uint8(b/256))
dst.Y[i] = yy
dst.Cb[i] = cb
dst.Cr[i] = cr
i++
}
}
}

func TestConvertYCbCr(t *testing.T) {
orig, err := readImageFromFile(artifact.MustPath("rimage/canny1.png"))
test.That(t, err, test.ShouldBeNil)

var yuvImg image.YCbCr
imageToYCbCr(&yuvImg, orig)
ImageToYCbCrForTesting(&yuvImg, orig)

err = WriteImageToFile(outDir+"/canny1-ycbcr.png", &yuvImg)
test.That(t, err, test.ShouldBeNil)
Expand All @@ -185,7 +137,7 @@ func BenchmarkConvertImageYCbCr(b *testing.B) {
test.That(b, err, test.ShouldBeNil)

var yuvImg image.YCbCr
imageToYCbCr(&yuvImg, orig)
ImageToYCbCrForTesting(&yuvImg, orig)

b.ResetTimer()

Expand Down
6 changes: 3 additions & 3 deletions services/slam/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func (slamSvc *builtIn) StopSLAMProcess() error {
return nil
}

func (slamSvc *builtIn) getLazyPNGImage(ctx context.Context, cam camera.Camera) ([]byte, func(), error) {
func (slamSvc *builtIn) getPNGImage(ctx context.Context, cam camera.Camera) ([]byte, func(), error) {
// We will hint that we want a PNG.
// The Camera service server implementation in RDK respects this; others may not.
img, release, err := camera.ReadImage(
Expand Down Expand Up @@ -798,7 +798,7 @@ func (slamSvc *builtIn) getAndSaveDataSparse(
return nil, errors.Errorf("expected 1 camera for mono slam, found %v", len(camStreams))
}

image, release, err := slamSvc.getLazyPNGImage(ctx, cams[0])
image, release, err := slamSvc.getPNGImage(ctx, cams[0])
if err != nil {
if err.Error() == opTimeoutErrorMessage {
slamSvc.logger.Warnw("Skipping this scan due to error", "error", err)
Expand Down Expand Up @@ -893,7 +893,7 @@ func (slamSvc *builtIn) getSimultaneousColorAndDepth(
goutils.PanicCapturingGo(func() {
defer slamSvc.activeBackgroundWorkers.Done()
defer wg.Done()
images[iLoop], releaseFuncs[iLoop], errs[iLoop] = slamSvc.getLazyPNGImage(ctx, cams[iLoop])
images[iLoop], releaseFuncs[iLoop], errs[iLoop] = slamSvc.getPNGImage(ctx, cams[iLoop])
})
}
wg.Wait()
Expand Down
87 changes: 77 additions & 10 deletions services/slam/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package builtin_test

import (
"bytes"
"context"
"fmt"
"image"
Expand Down Expand Up @@ -41,16 +42,27 @@ import (
)

const (
validDataRateMS = 200
numOrbslamImages = 29
validDataRateMS = 200
)

var (
orbslamIntCameraMutex sync.Mutex
orbslamIntCameraReleaseImagesChan chan int = make(chan int, 2)
orbslamIntWebcamReleaseImageChan chan int = make(chan int, 1)
orbslamIntSynchronizeCamerasChan chan int = make(chan int)
)

func getNumOrbslamImages(mode slam.Mode) int {
switch mode {
case slam.Mono:
return 15
case slam.Rgbd:
return 29
default:
return 0
}
}

func createFakeSLAMLibraries() {
for _, s := range slam.SLAMLibraries {
slam.SLAMLibraries["fake_"+s.AlgoName] = slam.LibraryMetadata{
Expand Down Expand Up @@ -103,22 +115,39 @@ func setupInjectRobot() *inject.Robot {
distortionsA := &transform.BrownConrady{RadialK1: 0.001, RadialK2: 0.00004}
projA = intrinsicsA

var projReal transform.Projector
intrinsicsReal := &transform.PinholeCameraIntrinsics{
var projRealSense transform.Projector
intrinsicsRealSense := &transform.PinholeCameraIntrinsics{
Width: 1280,
Height: 720,
Fx: 900.538,
Fy: 900.818,
Ppx: 648.934,
Ppy: 367.736,
}
distortionsReal := &transform.BrownConrady{
distortionsRealSense := &transform.BrownConrady{
RadialK1: 0.158701,
RadialK2: -0.485405,
RadialK3: 0.435342,
TangentialP1: -0.00143327,
TangentialP2: -0.000705919}
projReal = intrinsicsReal
projRealSense = intrinsicsRealSense

var projWebcam transform.Projector
intrinsicsWebcam := &transform.PinholeCameraIntrinsics{
Width: 640,
Height: 480,
Fx: 939.2693584627577,
Fy: 940.2928257873841,
Ppx: 320.6075282958033,
Ppy: 239.14408757087756,
}
distortionsWebcam := &transform.BrownConrady{
RadialK1: 0.046535971648456166,
RadialK2: 0.8002516496932317,
RadialK3: -5.408034254951954,
TangentialP1: -8.996658362365533e-06,
TangentialP2: -0.002828504714921335}
projWebcam = intrinsicsWebcam

r.ResourceByNameFunc = func(name resource.Name) (interface{}, error) {
cam := &inject.Camera{}
Expand Down Expand Up @@ -249,10 +278,10 @@ func setupInjectRobot() *inject.Robot {
return nil, errors.New("camera not lidar")
}
cam.ProjectorFunc = func(ctx context.Context) (transform.Projector, error) {
return projReal, nil
return projRealSense, nil
}
cam.PropertiesFunc = func(ctx context.Context) (camera.Properties, error) {
return camera.Properties{IntrinsicParams: intrinsicsReal, DistortionParams: distortionsReal}, nil
return camera.Properties{IntrinsicParams: intrinsicsRealSense, DistortionParams: distortionsRealSense}, nil
}
var index uint64
cam.StreamFunc = func(ctx context.Context, errHandlers ...gostream.ErrorHandler) (gostream.VideoStream, error) {
Expand All @@ -265,7 +294,7 @@ func setupInjectRobot() *inject.Robot {
select {
case <-orbslamIntCameraReleaseImagesChan:
i := atomic.AddUint64(&index, 1) - 1
if i >= numOrbslamImages {
if i >= uint64(getNumOrbslamImages(slam.Rgbd)) {
return nil, errors.New("No more orbslam color images")
}
imgBytes, err := os.ReadFile(artifact.MustPath("slam/mock_camera_short/rgb/" + strconv.FormatUint(i, 10) + ".png"))
Expand Down Expand Up @@ -304,7 +333,7 @@ func setupInjectRobot() *inject.Robot {
select {
case <-orbslamIntCameraReleaseImagesChan:
i := atomic.AddUint64(&index, 1) - 1
if i >= numOrbslamImages {
if i >= uint64(getNumOrbslamImages(slam.Rgbd)) {
return nil, errors.New("No more orbslam depth images")
}
imgBytes, err := os.ReadFile(artifact.MustPath("slam/mock_camera_short/depth/" + strconv.FormatUint(i, 10) + ".png"))
Expand All @@ -322,6 +351,44 @@ func setupInjectRobot() *inject.Robot {
}
}
return cam, nil
case camera.Named("orbslam_int_webcam"):
cam.NextPointCloudFunc = func(ctx context.Context) (pointcloud.PointCloud, error) {
return nil, errors.New("camera not lidar")
}
cam.ProjectorFunc = func(ctx context.Context) (transform.Projector, error) {
return projWebcam, nil
}
cam.PropertiesFunc = func(ctx context.Context) (camera.Properties, error) {
return camera.Properties{IntrinsicParams: intrinsicsWebcam, DistortionParams: distortionsWebcam}, nil
}
var index uint64
cam.StreamFunc = func(ctx context.Context, errHandlers ...gostream.ErrorHandler) (gostream.VideoStream, error) {
select {
case <-orbslamIntWebcamReleaseImageChan:
i := atomic.AddUint64(&index, 1) - 1
if i >= uint64(getNumOrbslamImages(slam.Mono)) {
return nil, errors.New("No more orbslam webcam images")
}
imgBytes, err := os.ReadFile(artifact.MustPath("slam/mock_mono_camera/rgb/" + strconv.FormatUint(i, 10) + ".png"))
if err != nil {
return nil, err
}
img, _, err := image.Decode(bytes.NewReader(imgBytes))
if err != nil {
return nil, err
}
var ycbcrImg image.YCbCr
rimage.ImageToYCbCrForTesting(&ycbcrImg, img)
return gostream.NewEmbeddedVideoStreamFromReader(
gostream.VideoReaderFunc(func(ctx context.Context) (image.Image, func(), error) {
return &ycbcrImg, func() {}, nil
}),
), nil
Comment on lines +372 to +386
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you had JPEG images, you could use the github.com/pixiv/go-libjpeg package and use its Decode function, which outputs YCbCr images directly: github.com/pixiv/go-libjpeg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you need the libjpeg library to use that package - which may not be present on our CI/canon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the idea! I think I'll leave as is.

default:
return nil, errors.Errorf("Webcam not ready to return image %v", index)
}
}
return cam, nil
default:
return nil, rdkutils.NewResourceNotFoundError(name)
}
Expand Down
Loading