1

In this SO Thread, pm0733464, says this:

we open sourced the CameraSource class, which has an auto focus method as well. This one allows you to set a specific focus mode as opposed to the "continuous video" mode that the official API defaults to:

Which was great. But it seems that the Google Vision API has moved on and the open Sourced version has not. The official API now has a new type of processor called: FocusingProcessor -- which allows the detector to only respond on the OnFocus event.

 barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.QR_CODE | Barcode.PDF417)
            .build();
 barcodeDetector.setProcessor(new BarcodeFocusingProcessor(
            barcodeDetector,
            new NullTracker()));
 CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
            .setFacing(CameraSource.CAMERA_FACING_BACK)
            .setRequestedPreviewSize(1600, 1024)
            .setAutoFocusEnabled(true)
            .setRequestedFps(24.0f);
    cameraSource = builder.build();

In my experiments this "finds" barcodes much faster than using the processor that the examples show in the Official Google Vision API Samples

Am I missing something somewhere? Or is the CameraSource in the Google.Vision libraries not the same one they are showing in the open source?

[EDIT] Sharing code by request of pm0733464:

For the record, I began with the fork of the vision api Demo which allows for automatically detecting barcode

My code makes some simple changes. First off, I add PDF417 to the scanable barcodes. Then I set the processor to an autofocus-er. I turn the tracker into a nullTracker because I don't need the graphic displaying, and I hoped that would speed some things up

in BarcodeCaptureActivity I change createCameraSource where it defined the barcode detector like to this:

  BarcodeDetector barcodeDetector =
                new BarcodeDetector.Builder(context)
                        .setBarcodeFormats(Barcode.PDF417)
                        .build();


        barcodeDetector.setProcessor(new MyCameraFocusingProcessor(
                barcodeDetector,
                new NullTracker()));


// Creates and starts the camera.  Note that this uses a higher resolution in comparison
        // to other detection examples to enable the barcode detector to detect small barcodes
        // at long distances.

        CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1600, 1024)
                .setRequestedFps(24.0f);

        // make sure that auto focus is an available option
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        {
            builder = builder.setFocusMode(
                    autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null);
        }

        mCameraSource = builder
                .setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null)
                .build();
    }

My FocusProcessor (in the same class) looks like this:

private class MyCameraFocusingProcessor implements Detector.Processor<Barcode>
    {
        public MyCameraFocusingProcessor(BarcodeDetector barcodeDetector, NullTracker emptyTracker)
        {

        }

        @Override
        public void release()
        {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections)
        {
            //  boolean chk = detections.detectorIsOperational();
            int sizeCheck = detections.getDetectedItems().size();
            if (sizeCheck > 0)
            {
                SparseArray<Barcode> codes = detections.getDetectedItems();
                for (int i = 0; i < sizeCheck; i++)
                {
                    Barcode barcode = codes.valueAt(i);
                    try
                    {
                        if (barcode.format == Barcode.PDF417)
                        {
                            Intent data = new Intent();
                            data.putExtra(BarcodeObject, barcode);
                            setResult(CommonStatusCodes.SUCCESS, data);
                            finish();
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.d("Detect", "Error: " + ex.getMessage());
                    }
                }
            }
            return;
        }
    }

    private class NullTracker
    {

    }
Community
  • 1
  • 1
MarkJoel60
  • 537
  • 2
  • 6
  • 24

1 Answers1

0

Both the official and open source versions CameraSource are current, although there are slight differences in some options.

FocusingProcessor is not new, since it was included with the initial launch of the API. This is actually not at all related to the camera's notion of focus or OnFocus. Instead, it is related to a detector's choice to "focus in" on a particular detected item. For example, there is a LargestFaceFocusingProcessor -- subclass of FocusingProcessor -- which initially detects the largest face in view and only tracks that face for as long as it is visible.

Both versions of CameraSource now support autofocus, although the specifics of how this is specified in each has diverged a little. If the official barcode sample app is slower, that probably indicates that autofocus wasn't enabled in that app.

pm0733464
  • 2,862
  • 14
  • 16
  • Thanks for the response. Is there a plan to merge the two versions? Or is the fork permanent and the Open Source version is always going to be a bit off now? Since you've seen both, is there any disadvantage to committing to using the OpenSource version for a professional project? I ask because there seems to be a bug in the official version. When I call cameraSource.release() I get an error: app passed NULL surface. I've tried several of the solutions on SO, but it always happens. App continues to run, but I'd like more granular control over CameraSource to avoid such things. – MarkJoel60 Oct 01 '16 at 12:17
  • "the official and open source versions CameraSource are current, although there are slight differences" @pm0733464 - I'd like to understand the slight differences. Because even when I add the FocussingProcessor, the openSource is still almost twice as slow as the "official" version. And both are calling autofocus. (And both throw an error on stop() when mCamera.setPreviewTexture(null) is called.) – MarkJoel60 Oct 03 '16 at 18:34
  • Sure, pm0733464. Above changed to reflect code snippets. If you want to look at the whole project, I can zip it up and email it or whatever. – MarkJoel60 Oct 10 '16 at 16:07
  • The official version uses FOCUS_MODE_CONTINUOUS_VIDEO only, but your code set the focus mode as FOCUS_MODE_CONTINUOUS_PICTURE. That probably accounts for the speed difference. I suggest trying FOCUS_MODE_CONTINUOUS_VIDEO. – pm0733464 Oct 12 '16 at 21:02
  • pm0733464 - It's still faster using the "closed" autofocus. It is not a perfect test, since there are some code changes. But let me ask you, what is the best way of measuring "recognize time?" Right now, I add a timer on Preview Start: mPreview.start(mCameraSource, mGraphicOverlay); startTime = System.currentTimeMillis(); and then I check it again in the selectFocus override Long estimatedTime = System.currentTimeMillis() - startTime;. Is this reasonable? – MarkJoel60 Oct 24 '16 at 16:18