Ad

Socket Is Closed While Accessing Cloud Vision Api Using Firebase-mlkit

I am running a subset of the example given here and Firebase OCR Docs. I am getting socket is closed while running Cloud Text Recognition using Firebase ML Kit. Running this on a device with Android Pie.

I have tried clearing Google Play Services cache, Restarting the device(Makes it work 1 time), creating a Network security config to allow HTTP requests to be sent/received from here

Creating a bitmap from Asset and passing it into runCloudTextRecognition

Android Manifest

<uses-permission android:name="android.permission.INTERNET" />
<application>
<meta-data
            android:name="com.google.firebase.ml.vision.DEPENDENCIES"
            android:value="ocr" />
</application>

runCloudTextRecognition and getBitmapFromAsset

public static Bitmap getBitmapFromAsset(Context context, String filePath) {
        AssetManager assetManager = context.getAssets();
        InputStream istr;
        Bitmap bitmap = null;
        try {
            istr = assetManager.open(filePath);
            bitmap = BitmapFactory.decodeStream(istr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

private void runCloudTextRecognition(Bitmap mSelectedImage) {
        //mCloudButton.setEnabled(false);
        FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(mSelectedImage);
        final FirebaseVisionTextRecognizer recognizer = FirebaseVision.getInstance()
                .getCloudTextRecognizer();
        recognizer.processImage(image)

                .addOnSuccessListener(
                        new OnSuccessListener<FirebaseVisionText>() {
                            @Override
                            public void onSuccess(FirebaseVisionText texts) {
                                //mCloudButton.setEnabled(true);
                                Log.e("CloudOCR_Success",texts.getText());
                            }
                        })
                .addOnFailureListener(
                        new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                // Task failed with an exception
                                //mCloudButton.setEnabled(true);
                                e.printStackTrace();
                                try {
                                    recognizer.close();
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                            }
                        });
    }

LogCat

E/ImageAnnotatorTask: batchAnnotateImages call failed with exception: 
    java.net.SocketTimeoutException: timeout
        at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:212)
        at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:261)
        at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
        at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
        at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
        at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
        at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
        at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
        at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:26)
        at com.google.android.gms.internal.firebase_ml.zzgd.<init>(Unknown Source:5)
        at com.google.android.gms.internal.firebase_ml.zzga.zzfg(Unknown Source:43)
        at com.google.android.gms.internal.firebase_ml.zzfk.zzfa(Unknown Source:131)
        at com.google.android.gms.internal.firebase_ml.zzer.zzei(Unknown Source:51)
        at com.google.android.gms.internal.firebase_ml.zzqf.zza(Unknown Source:68)
        at com.google.android.gms.internal.firebase_ml.zzqg.zza(Unknown Source:23)
        at com.google.android.gms.internal.firebase_ml.zznt.call(Unknown Source:4)
        at com.google.android.gms.internal.firebase_ml.zznn.zza(Unknown Source:30)
        at com.google.android.gms.internal.firebase_ml.zznq.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.google.android.gms.internal.firebase_ml.zzf.dispatchMessage(Unknown Source:6)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)
     Caused by: java.net.SocketException: socket is closed
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:551)
        at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
        at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
        at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306) 
        at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300) 
        at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196) 
        at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186) 
        at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127) 
        at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737) 
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609) 
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471) 
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407) 
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538) 
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) 
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:26) 
        at com.google.android.gms.internal.firebase_ml.zzgd.<init>(Unknown Source:5) 
        at com.google.android.gms.internal.firebase_ml.zzga.zzfg(Unknown Source:43) 
        at com.google.android.gms.internal.firebase_ml.zzfk.zzfa(Unknown Source:131) 
        at com.google.android.gms.internal.firebase_ml.zzer.zzei(Unknown Source:51) 
        at com.google.android.gms.internal.firebase_ml.zzqf.zza(Unknown Source:68) 
        at com.google.android.gms.internal.firebase_ml.zzqg.zza(Unknown Source:23) 
        at com.google.android.gms.internal.firebase_ml.zznt.call(Unknown Source:4) 
        at com.google.android.gms.internal.firebase_ml.zznn.zza(Unknown Source:30) 
        at com.google.android.gms.internal.firebase_ml.zznq.run(Unknown Source:2) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at com.google.android.gms.internal.firebase_ml.zzf.dispatchMessage(Unknown Source:6) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.os.HandlerThread.run(HandlerThread.java:65) 
Ad

Answer

I solved my issue by reducing the size of the bitmaps to the recommended size using BitmapFactory Options. For more information on handling large bitmaps, see this or you can use a library like Glide. Updating the firebase libraries also helped.

Ad
source: stackoverflow.com
Ad