I need to send the image file to the Clarifai server to classify the images, but I continue to receive "skipped frames, the application does too much work"

I am using a new stream to send a file and a bit of code to convert a bitmap to a file. Converting from a bitmap to a file is REALLY slow, and it seems that sending information to clarifai does nothing ...

//Convert bitmap to byte array
            Bitmap bitmap = mResultsBitmap;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(f);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                fos.write(bitmapdata);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            final File pFIle2 = f;
            //TODO: clarifai stuff
            //TODO: clarifai stuff
            Log.e("this:"," this is running 0");
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.e("this:", " this is running 1");
                    client = new ClarifaiBuilder("mykeyhere1234}").buildSync();
                    Log.e("this:", " this is running 2");
                    Thread th = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("this:", " this is running 3");
                            Log.e("this", client.getDefaultModels().generalModel().predict()
                                    .withInputs(
                                            ClarifaiInput.forImage(ClarifaiImage.of(pFIle2))
                                    )
                                    .executeSync().rawBody());
                            Log.e("this:", " this is running 4");
                        }
                    });


                }
            });

This bit of code snippet is in the onActivityResult method. None of the log messages print except "0"

+4
source share
3 answers

, API (.. ) . , :

  • API ,
  • Clarifai.

:

  • - ? ClarifaiBuilder/ClarifaiClient (, ).
  • , . . .
+1

, , . .

, Clarifai , , start() Thread. , Thread .

, :

final Bitmap bitmap = mResultsBitmap;

final File pFile2 = f;

ClarifaiClient client = new ClarifaiBuilder("mykeyhere1234}").buildSync();

DefaultModels defaultModels = client.getDefaultModels();

Log.e("this:"," this is running 0");
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        //Convert bitmap to byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(pFile2);
            fos.write(bitmapdata);
        } catch (FileNotFoundException e | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //TODO: clarifai stuff
        //TODO: clarifai stuff
        Log.e("this:", " this is running 1");
        client = 
        Log.e("this:", " this is running 2");
        Log.e("this:", " this is running 3");
        Log.e("this", models.generalModel().predict()
                .withInputs(
                        ClarifaiInput.forImage(ClarifaiImage.of(pFile2))
                )
                .executeSync().rawBody());
        Log.e("this:", " this is running 4");

     }
});

thread.start();
0

It is always best practice to maintain bitmap-related operations in the background thread, try this, you won’t get the log associated with dropped frames, doing too much work on the main thread . Use an asynchronous task to perform bitmap operations.

0
source

Source: https://habr.com/ru/post/1683364/


All Articles