arfoundation-samples icon indicating copy to clipboard operation
arfoundation-samples copied to clipboard

[Bug] [Android] [ImageTracking] Image tracking doesn't get updated after initial detection

Open RobAEG opened this issue 6 years ago • 42 comments

Image tracking doesn't get updated after initial detection When using a MutableImageLibrary, after adding an image to the mutable image library and pointing the device camera to the tracking image, it gets detected. But subsequently it will not detect any changes in movement. This mainly happens with smaller tracking images, around the size of 3 by 3 centimeters. The tracking images themselves are QR codes. Larger tracking images, also QR codes, suffer the same problem from time to time but not as frequently.

In my code, I use ARFoundation's ARTrackedImagesChangedEventArgs struct to handle the detection, updated and removal of tracked images. The ARTrackedImagesChangedEventArgs "added" event gets received almost as soon as I have the image in view, which is great. But subsequently no ARTrackedImagesChangedEventArgs "updated" events are received unless I move the camera up and down and around the tracking image. Once the tracking image has been "updated" in this way, tracking seems to be fairly stable.

What could be the cause for this delay in "updated" events? On physically larger tracking image, also of QR codes, a slight delay sometimes happens as well, but not as frequently or annoyingly as the one for smaller tracking images. Changing the expected physical size to the physical size of the tracking image when adding it to the mutable image library doesn't seem to help alleviate this issue.

On iOS devices tracking proceeds as expected, with the "added" event being followed up with the "updated" event almost immediately even when tracking smaller tracking images. Any help or insight for this issue is appreciated!

Additional information Builds were tested on these devices:

Motorola One Macro, Android Version 9 One Plus 5, Android Version 10

Builds were made with the latest versions of the AR Foundation & AR Core packages, and build on Unity version 2019.4.6f1

RobAEG avatar Aug 21 '20 10:08 RobAEG

Important to note that it generally takes 10-20 seconds before the updated event is properly called on Android. It usually takes less than 1 second on iOS.

DominicdeGraaf avatar Aug 24 '20 14:08 DominicdeGraaf

Important to note that it generally takes 10-20 seconds before the updated event is properly called on Android. It usually takes less than 1 second on iOS.

That's not what I see in this samples repo. Adding a new image takes ~1 second or less to detect it. I imagine it depends on the quality of the image (from ARCore's standpoint). There's a command line tool you can run that will give you a "score" for how suitable the image is for ARCore.

tdmowrer avatar Aug 24 '20 17:08 tdmowrer

I just cloned this sample repo in Unity 2019.4.8f1 and added two of our own images to the DynamicLibrary.cs example script in the "BasicImageTracking" example scene.

I've also checked these two images(link and link) in the command line tool you suggested, and both return a score of 100.

When I test these images on my monitor, they are generally recognized within 1-2 seconds, when I print them out it takes ~10 seconds on average. And I have to move my phone around slowly, if I don't then they won't get recognized at all.

  • When I add one of our own images to the ReferenceImageLibrary (as static image), they get detected straight away.
  • When I remove the Unity logo example from the static ReferenceImageLibrary and add it as dynamic image, it again, takes ~10 second on average before it gets recognized.
  • Images on a monitor get recognized a LOT faster than printed versions, most notably when using dynamic images. (monitor 1-2 seconds, printed 10-20 seconds).

To reproduce:

  • Remove one of the static images from the library and add it to the DynamicLibrary.cs example script. Print it out, and notice how it takes a much longer time before it is recognized and Android (ios is fine!)
  • Add THIS image to the static library and print it out and notice it is recognized almost instantly.
  • Add THIS image to the dynamic library and print it out and notice it takes 10-20 seconds before it is recognized.

I hope we will be able to figure out why this is not working correctly!

DominicdeGraaf avatar Aug 25 '20 06:08 DominicdeGraaf

I believe the issues discussed in this thread are largely artifacts of the way ARCore performs image tracking. I will try to summarize here, but you might get a more detailed answer from the ARCore folks by posting on https://github.com/google-ar/arcore-unity-sdk/issues

ARCore treats images in the same way it treats the rest of the environment, that is, its detection and tracking is based on feature points. This works well for unique images, like a painting or photograph, but QR codes all look alike in that they have very regular features.

I have to move my phone around slowly

Even though ARCore might give your images a score of 100, because its tracking algorithm is still based on feature points, it requires that you move the device (or at least benefits greatly from it). By contrast, most QR code scanning apps require you to hold still so it can see the image clearly, so we've all been "trained" not to move our phones when scanning a QR code. This is not how ARCore's image tracking works, so holding the device still is actually a worst-case for its detection algorithm.

If all you want to do is scan a QR code, feeding the raw image pixels to a bar code reader might be a more appropriate solution. That has been discussed a few times on this issues page, e.g., https://github.com/Unity-Technologies/arfoundation-samples/issues/489

This mainly happens with smaller tracking images, around the size of 3 by 3 centimeters.

With regards to size, ARCore doesn't really care how big the image is, but it does matter how much of it fills its field of view. Smaller images mean the user must move closer to the image, and this may cause autofocus to struggle with very close objects.

When I test these images on my monitor, they are generally recognized within 1-2 seconds, when I print them out it takes ~10 seconds on average.

ARCore's image detection is based largely on contrast, so I would imagine a backlit LCD screen displaying a black and white QR code has optimal contrast, while a printed QR code would have much less.

I hope that helps provide some context. I don't believe there is anything more we can do in ARFoundation, since it is just an abstraction of ARCore in this case.

tdmowrer avatar Aug 27 '20 17:08 tdmowrer

Hi Tim,

I'd like to stress, that adding these QR codes to the static library, they are recognized almost instantly. Tracking images that are added to the mutable library take a lot longer to be recognized. Please do try it out with printed versions of the example images provided in the samples AND with the QR

If you really need to, I can prove the correct tracking of QRcodes in a static library and the not correct recognition of the sample images provided in a mutable (dynamic) library. But that will take some effort on my part on making a video. You can easily test it yourself though by testing with the images I provided in my previous post. Also, if you download the AR Scanner app (https://play.google.com/store/apps/details?id=com.stickylock.arscanner), I've developed this app using AR Core instead of AR Foundation and there the AR tracking works a lot better (we only use QR codes for recognition and they work great). See the app in working here(https://youtu.be/woC03f8qSGk), I know it looks like that QRcode isn't the tracking image, but it definately is.

I really do hope I can make you believe me in saying that there is a problem regarding tracking in AR Foundation with AR Core. We've completely rewritten our AR apps to AR Foundation because we believe that this will provide in way more stable AR performance. However for now, this bug is killing our release.

DominicdeGraaf avatar Aug 27 '20 18:08 DominicdeGraaf

Can your reproduce this delay with the images included our AR Foundation sample? Small QR codes are especially challenging for ARCore according to their engineers.

You might also try adding your QRCode image to the AR Foundation sample and checking if you see the same behavior as you are seeing in your app.

mdurand42 avatar Aug 27 '20 22:08 mdurand42

Hi guys,

Let forget for a while that I am using QR codes as tracking images.

The bug that I am reporting, while using a clone of the sample project, is that the image recognition of images in a MutableRuntimeReferenceImageLibrary takes a lot longer than images that are already added to the static reference library.

To reproduce:

  • Print of the three images from the sample project.

  • create a build of the basicimagetracking example scene.

  • notice very quick image recognition.

  • remove all three images from the static reference library and add them to the dynamiclibrary.cs example script. This will add them to the MutableRuntimeReferenceImageLibrary

  • again create a build of the basicimagetracking sample scene.

  • in de build press the “load images” onGUI button.

  • now the example images are much more difficult to get recognized.

I am seeing these results consistently using only the example project and its example images.

DominicdeGraaf avatar Aug 28 '20 05:08 DominicdeGraaf

Oh, I know what the problem is.

When you build an image library at build time, if the source images are JPG or PNG, then we feed those directly to the ARCore image database generation tool. At runtime, you can either supply either an array of bytes representing your image, or you can use a an extension method that accepts Texture2Ds. By default, a Texture2D's import settings alter the image so that the dimensions are rounded to the nearest power of 2. The rafflesia photo, for example, is a JPEG at 1024x681. However, when imported as a Texture2D into Unity, it gets rounded to 1024x512:

Screen Shot 2020-08-28 at 5 18 49 PM

You can even see how it looks "squashed" in the preview.

So if you add an image dynamically in this way, the image you end up adding to the image library is not the same as the image you are trying to find, which is probably why ARCore struggles to find it in this case. The images in our sample one.png and two.png happen to be 512x512 already, so they do not suffer from this problem.

If you want to dynamically add an image to a reference image library as a Texture2D, then you can change the import settings to not alter the image dimensions. This, however, is less optimal if you intend to ever send this texture to the GPU for rendering.

tdmowrer avatar Aug 29 '20 00:08 tdmowrer

I will definatly try if this is the issue I am having, however, I have also tried this with already square images (like the Unity logo example image), which gave the same results. I’ll report back to you when I know more.

DominicdeGraaf avatar Aug 29 '20 06:08 DominicdeGraaf

Hey guys, I've played around with some of the settings, and it seems that disabling the mipmapping speeds it up a bit, I have indeed disabled Non-Power of 2.

So what I did next: I took 3 of my QR images, gave them the exact same inport settings. I added 2 of them to the static reference library, and one to the dynamic library. I filmed the results:

https://www.dropbox.com/s/4b0byrqin68i1ls/IMG_3468.MOV?dl=0

Notice the 3 QR codes in the video, the most left and most right are added to the static library, the middle one to the dynamic library. I tried it multiple times (as seen in the video), you'll see that the static images are recognized much, much faster than the one in the dynamic library.

To give it a proper test, I also changed the images around, added the left image to the dynamic library and the middle and right one to the static library: https://www.dropbox.com/s/3ma6nksedp09au5/IMG_3469.MOV?dl=0

Any other idea's? See the texture import settings, (No Android Overrides): Capture

DominicdeGraaf avatar Aug 31 '20 10:08 DominicdeGraaf

Any new idea's guys?

DominicdeGraaf avatar Sep 09 '20 14:09 DominicdeGraaf

I will be looking into this more deeply this week.

tdmowrer avatar Sep 14 '20 23:09 tdmowrer

I just cloned this sample repo in Unity 2019.4.8f1 and added two of our own images to the DynamicLibrary.cs example script in the "BasicImageTracking" example scene.

I've also checked these two images(link and link) in the command line tool you suggested, and both return a score of 100.

When I test these images on my monitor, they are generally recognized within 1-2 seconds, when I print them out it takes ~10 seconds on average. And I have to move my phone around slowly, if I don't then they won't get recognized at all.

  • When I add one of our own images to the ReferenceImageLibrary (as static image), they get detected straight away.
  • When I remove the Unity logo example from the static ReferenceImageLibrary and add it as dynamic image, it again, takes ~10 second on average before it gets recognized.
  • Images on a monitor get recognized a LOT faster than printed versions, most notably when using dynamic images. (monitor 1-2 seconds, printed 10-20 seconds).

To reproduce:

  • Remove one of the static images from the library and add it to the DynamicLibrary.cs example script. Print it out, and notice how it takes a much longer time before it is recognized and Android (ios is fine!)
  • Add THIS image to the static library and print it out and notice it is recognized almost instantly.
  • Add THIS image to the dynamic library and print it out and notice it takes 10-20 seconds before it is recognized.

I hope we will be able to figure out why this is not working correctly!

I have followed your repro steps using your QR codes stoel and apollo.

  • I printed the images on paper and placed them side-by-side on my desk.
  • I created an XRReferenceImageLibrary containing both images.
    • Each image is recognized quickly (hard to measure exactly, but feels like less than 1 second; very snappy).
  • I then removed "apollo" from the reference image library and added it dynamically on button press.

This is where things get interesting. If the images are relatively large (16 x 16 cm), there is no noticeable difference.

However, when I shrank the QR codes to 8x8 cm (probably closer to your use case), I am able to reproduce some of what you are seeing. However, it's not that it takes a long time to detect the image -- it is detected in less than half a second (even as quickly as 0.15s) -- but its tracking state remains TrackingState.None until you "explore" the space a bit (i.e., move your phone around). In ARCore, this maps to AR_AUGMENTED_IMAGE_TRACKING_METHOD_NOT_TRACKING. During this time, the position and extents are meaningless (usually it's just all zeros). Most of our samples hide the GameObject until the tracking state is something other than None, so this would give the appearance that the image takes a long time to be detected.

On the other hand, the image that was part of the original, statically generated reference library ("stoel") is detected and tracked without having to move the device.

I've confirmed this difference at the native C level as well; ARFoundation is not misreporting the tracking state. Dynamically added images are detected almost immediately and updated pretty frequently, but they will remain at AR_AUGMENTED_IMAGE_TRACKING_METHOD_NOT_TRACKING until you explore the space a bit, while images from the prebuilt library do not seem to require this.

Note all this assumes I'm holding my phone perfectly still. As soon as I move a little, it seems to pick up enough features to move to full tracking.

In conclusion, while I acknowledge there is a difference in behavior between static and dynamic reference images, that difference seems to be an implementation detail of ARCore. You can still determine that a particular QR code was detected very quickly, you just won't have good tracking data until the user moves their device a bit. The tracking state of an ARTrackedImage is available from its trackingState property.

tdmowrer avatar Sep 16 '20 01:09 tdmowrer

Hi Tim,

Thanks for investigating! However, I have one more item to add to the investigation!

We have a live Android app, called “AR Scanner” this has been built in Unity 2017, without the AR Foundations package, but instead with the AR Core Unity Git repo that had to be used back than.

This live app detects the dynamically added images almost instantly and also updates the tracking straight away.

However, AR Foundation adds a lot better and stable tracking features we though, so we wanted to rebuild the app from the ground up to AR Foundation. For now, this has been a waste of time, because the app works can’t be updated for Android in its current state because of the very bad dynamic image tracking functionality.

You can get the free app here: https://play.google.com/store/apps/details?id=com.stickylock.arscanner&hl=en_US

Do you have any idea why the older AR Core project works so much better?

DominicdeGraaf avatar Sep 16 '20 06:09 DominicdeGraaf

Do you have any idea why the older AR Core project works so much better?

I would say that "better" is subjective, or at least use case dependent, here. In some sense, QR code detection is kind of a worse-case scenario. ARCore's image tracking, like every other feature, depends on feature point extraction from the environment, unlike a bog-standard QR code reader which is just looking for a specific kind of image.

"Google Play Services for AR" (ARCore) has gone through several updates since then. Back in 2017, your app may have still used a developer preview. I've noticed significant improvements in the image tracking feature in each update, but while the API remains backwards compatible, the algorithms have changed and won't necessarily produce the same behavior.

So I don't know specifically why you see different behavior, but it is not entirely surprising, given nearly 3 years of development and updates.

tdmowrer avatar Sep 16 '20 06:09 tdmowrer

Hi Tim,

Thanks for investigating! However, I have one more item to add to the investigation!

We have a live Android app, called “AR Scanner” this has been built in Unity 2017, without the AR Foundations package, but instead with the AR Core Unity Git repo that had to be used back than.

This live app detects the dynamically added images almost instantly and also updates the tracking straight away.

However, AR Foundation adds a lot better and stable tracking features we though, so we wanted to rebuild the app from the ground up to AR Foundation. For now, this has been a waste of time, because the app works can’t be updated for Android in its current state because of the very bad dynamic image tracking functionality.

You can get the free app here: https://play.google.com/store/apps/details?id=com.stickylock.arscanner&hl=en_US

Do you have any idea why the older AR Core project works so much better?

To add to Dominics reply, I've added a few QR codes that we are using.

Stoel Tafel Locker

StickylockMax avatar Sep 16 '20 06:09 StickylockMax

So you know who I can address best on the AR Core side of things than? If you say its in the native C level, I suppose I have to get in contact with them?

DominicdeGraaf avatar Sep 17 '20 05:09 DominicdeGraaf

So you know who I can address best on the AR Core side of things than? If you say its in the native C level, I suppose I have to get in contact with them?

https://github.com/google-ar/arcore-android-sdk/issues is their issues page.

tdmowrer avatar Sep 17 '20 17:09 tdmowrer

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 24 '20 20:09 stale[bot]

Update: We posted this on the AR Core github.

They confirmed that the dynamic library should update the same as the static library and will look into it.

Our post: https://github.com/google-ar/arcore-android-sdk/issues/1137

DominicdeGraaf avatar Sep 25 '20 19:09 DominicdeGraaf