Apparence-io / CamerAwesome

📸 Embedding a camera experience within your own app shouldn't be that hard. A flutter plugin to integrate awesome Android / iOS camera experience.
https://ApparenceKit.dev
MIT License
912 stars 201 forks source link

The Analysis resolution is stuck at 1072*1072 #301

Closed dorrin-sot closed 6 months ago

dorrin-sot commented 1 year ago

Steps to Reproduce

CameraAwesomeBuilder.awesome(
        aspectRatio: CameraAspectRatios.ratio_1_1,
        sensor: Sensors.front,
        onImageForAnalysis: (img) => _analyzeImage(img),
        imageAnalysisConfig: AnalysisConfig(
          outputFormat: InputAnalysisImageFormat.nv21,
          width: 4000,
          maxFramesPerSecond: 20,
        ),
        ...
)

Expected results

The Analysis resolution (the image received in onImageForAnalysis) would be my phone's max resolution which is 1440*2560

Actual results

The Analysis resolution is 1072*1072

About your device

Brand Model OS
Samsung Galaxy A03 Android 13

Your flutter version

Run this in your command line flutter --version

Flutter 3.7.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 9944297138 (7 weeks ago) • 2023-02-08 15:46:04 -0800
Engine • revision 248290d6d5
Tools • Dart 2.19.2 • DevTools 2.20.1

apalala-dev commented 1 year ago

On Android, we rely on the CameraX API and its final resolution may vary based on device capabilities and use cases (wether you want to record video or take photo in addition to image analysis). Setting a target width will only try to find a close resolution that fits the rest of your configuration, which may explain this limitation.

Why would you want such an high resolution image for analysis?

dorrin-sot commented 1 year ago

Because this is how _analyzeImage works

_analyzeImage(AnalysisImage img) {
  final face = extract face data from img
  if (some condition for face that needs to be exact) {
    save picture
  }
}

Now when I want to save the picture the quality is too low.

I also tried using CameraState to capture the image right then but it has a few seconds of delay and so the condition may not be true after it's done.

dorrin-sot commented 1 year ago

Forgot to mention that I also need to do some Image processing that includes cropping the image. Which reduces the quality further so I need all the pixels I can get :)

apalala-dev commented 1 year ago

Having a huge quality in the image analysis processing will probably make it slow (at least on lower end devices).

You could eventually try to make a screenshot of the widget when you detect that your condition is met, but that would depend on the screen resolution.

If you lower the resolution (and eventually the maxFps), does taking a photo still takes that much time?

dorrin-sot commented 1 year ago

Lowered it to the following:

imageAnalysisConfig: AnalysisConfig(
  outputFormat: InputAnalysisImageFormat.nv21,
  width: 100,
  maxFramesPerSecond: 10,
),

Still takes a few seconds :(

apalala-dev commented 1 year ago

That's weird, your phone might be too low-end...

What resolution would be acceptable for the image analysis? I'm trying something on an other branch that might help with your use case

dorrin-sot commented 1 year ago

As high as possible :) (2160*3840 would be ideal)

dorrin-sot commented 1 year ago

Another thing is I just logged the size of the input AnalysisImage in _analyzeImage and found out that it is 720*720 despite being set to 100 in AnalysisConfig.

apalala-dev commented 1 year ago

I don't think that it will be possible with your current device, from what you tell me.

Supported resolutions are device dependent and chosen by CameraX. We only give it a hint, like "Try to find the closest resolution from (width, height)" (where width is the width you set and height is calculated based on your aspectRatio).

The branch analysis_capabilities might have a few improvements. You may take a look at analysis_native_conversion.dart in the example project.

On my device, it runs okish (it's more laggy than just displaying the preview) with 1920x1080 for example, but I can't make it run with such an high resolution as 2160*3840.

Anyway, making the analysis on a 2160x3840 will probably be really heavy on the CPU. If you crop the image before, it might help, but the cropping is an other thing to calculate for each frame, which I believe will also be heavy on the CPU.

dorrin-sot commented 1 year ago

But I have tried using a higher end phone that does normally have 4K resolution but its analysis image resolution was still around 1080*1080

g-apparence commented 1 year ago

It's not because your phone has a 4K resolution that analysis will allow you to analyze images with such a high quality. Analysis is aimed for analyzing content using AI, not taking great pictures. AI doesn't require such high quality images. (The more pixel the more power it requires).

What do you want to do with such big analysis images ?

dorrin-sot commented 1 year ago

I need to take an image of a face whose angles are as near to zero as possible and then send it to server to do some other analysis which requires a high quality photo. from what i understand there is some overlay-ing of multiple images involved which cant be done if the quality is not good or the angles are not setup correctly.

dorrin-sot commented 1 year ago

Another way that might work is raise the quality to max and cache that image, then pass a reduced version of that to the Analyzer.

urossm commented 1 year ago

I had the same issues with the resolutions, it was only giving me 1:1 aspect ratios, for example my phone, OnePlus Nord, has a maximum image size of 4000x3000 and when I create CameraAwesomeBuilder.previewOnly widget and no matter what image width and aspect ratio I set in imageAnalysisConfig, it always gives me 1:1 ratio image. For example if I set image width to 4000 and aspect ratio to 4:3 it should give me 4000x3000 image since that's what my phone supports but it gives me 3000x3000, same if I put let's say image width to 2000, it gives me 2558x2558 image.

So I started messing with the native files in the CamerAwesome and I found a bug that solves this issue. In android>src.main>kotlin.com.apparence>camerawesome>cameraX>ImageAnalisysBuilder.kt on line 66 where it builds ImageAnalysis, it uses function setTargetResolution(Size(width, height)) but what fixes this issue is if you switch width and height and write Size(height, width) it works as it should. After that in flutter if I set image width to 4000 in imageAnalysisConfig it gives me 4000x3000 image as it should have in the first place and if I switch aspect ratio to 16:9 and set image width to 3840 it gives me image with size of 3840x2160 as it should.

I don't know if that's bug with CameraX where it internally switches width and height or is something related with CamerAwesome but I hope this will be tested and implemented in some other versions because other than that, the plugin is awesome

g-apparence commented 6 months ago

Some improvements have been made for this. As this issue is a bit old and on older version I will close it but feel free to reopen if you have still this problem

spydr1 commented 5 months ago

I also have same issue and i solved @urossm's solution. Thank you so much for your solution.