jonathanpalma / react-native-tesseract-ocr

Tesseract OCR wrapper for React Native
MIT License
565 stars 172 forks source link

Error: Data path don't exist, even if data path is correct #53

Closed SwagatRanjit84 closed 4 years ago

SwagatRanjit84 commented 5 years ago

OCR Error: Error: Data path does not exist! at createErrorFromErrorData (C:\reactnativeapp\ocr\node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:146) at C:\reactnativeapp\ocr\node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:95 at MessageQueue.invokeCallback (C:\reactnativeapp\ocr\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414) at C:\reactnativeapp\ocr\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:127 at MessageQueue.guard (C:\reactnativeapp\ocr\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:314) at MessageQueue.invokeCallbackAndReturnFlushedQueue (C:\reactnativeapp\ocr\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:126) at debuggerWorker.js:72

Data path is : file:///storage/emulated/0/documents/a.jpg which is absolutely correct Inside document folder there is an image called "a.jpg" still there is error

chetankotkar commented 5 years ago

same error. Any solutions ?

SwagatRanjit84 commented 5 years ago

@chetankotkar Didn't find the solution. I tried with image inside the project structure and used require('./img.png'). Also with the correct datapath of image inside phone gallery. Didn't work either

ziyafenn commented 5 years ago

@SwagatRanjit84 still nothing?

SwagatRanjit84 commented 5 years ago

@ziyafenn No idea man, whats going on? DId you solve it?

ziyafenn commented 5 years ago

@SwagatRanjit84 should post the fix tomorrow.

SwagatRanjit84 commented 5 years ago

@ziyafenn fast as possible

nasseh101 commented 5 years ago

getting the same error

SwagatRanjit84 commented 5 years ago

@ziyafenn @nasseh101 Any solutions found?

vmvini commented 5 years ago

I had this same error 'Data path does not exist'. When investigating the source code, this error is thrown by the TessBaseAPI.init(String datapath, String language) method, so its not related to the image path, its related to the tessdata path.

init is called as tessBaseApi.init(DATA_PATH, getConstants().get(lang).toString()); DATA_PATH is defined on the class RNTesseractOcrModule in this line: private static String DATA_PATH = Environment.getExternalStorageDirectory().toString() + File.separator

So, its not accessing the assets folder within our project because its impossible for a library to do it without context, its accessing directly through the external storage path, concatenating later to the project's name: Environment.getExternalStorageDirectory().toString() + File.separator + reactContext.getPackageName() + File.separator which evaluates to: /storage/emulated/0/com.projectsname/

The folder /storage/emulated/0/com.projectsname/ is there when I open on my pc, but later the TessBaseAPI code executes: if (!datapathFile.exists()) throw new IllegalArgumentException("Data path does not exist!"); And cant find the folder... So, I dont know why it didnt find the /storage/emulated/0/com.projectsname/. And inside the /storage/emulated/0/com.projectsname/, the folder 'tessdata' is not there also even though I placed the tessdata folder inside android/app/src/main/assets/

But, I could do the tesseract work changing DATA_PATH to : private static String DATA_PATH = Environment.getExternalStorageDirectory().toString() + File.separator + Environment.DIRECTORY_DOWNLOADS + File.separator;

then I commented the line that concatenates the path with the projects directory : public RNTesseractOcrModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; //if (!this.DATA_PATH.contains(reactContext.getPackageName())) // this.DATA_PATH += reactContext.getPackageName() + File.separator; } And finally, I moved my tessdata to my android downloads folder.

Its not the solution I want, but at least I could do the tesseract work, later I will try something else.

mihailpreda commented 4 years ago

The path is correct, but you need to grant read/write permissions : add in android/app/src/main/AndroidManifest.xml : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Then you need to use AndroidPermissions , so import {PermissionsAndroid} from 'react-native'; and then define something like this :

 requestRWPermissions= async ()=> {
        const checkReadExternalStorage = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE);
        const checkWriteExternalStorage = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
        if (checkReadExternalStorage === PermissionsAndroid.RESULTS.GRANTED && checkWriteExternalStorage === PermissionsAndroid.RESULTS.GRANTED ) {
            //alert("You've have read/write permission");
        } else {
            try {
                const grantedRead = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
                    {
                        'title': 'FollowUP App required read external storage permission',
                        'message': 'We required read external storage permission in order to get device location ' +
                            'Please grant us.'
                    }
                )
                const grantedWrite = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
                    {
                        'title': 'FollowUP App required write external storage permission',
                        'message': 'We required write external storage permission in order to get device location ' +
                            'Please grant us.'
                    }
                )
                if (grantedRead === PermissionsAndroid.RESULTS.GRANTED && grantedWrite === PermissionsAndroid.RESULTS.GRANTED) {
                    this.takePicture();

                } else {
                    //alert("You don't have read/write permission");
                }
            } catch (err) {
                alert(err)
            }
        }
    };

and takePicture is something like :

    takePicture = async () => {
        if (this.camera) {
            const options = { quality: 0.9, base64: true };
            const data = await this.camera.takePictureAsync(options);

            const tessOptions = {
                whitelist: null, 
                blacklist: '1234567890\'!"#$%&/()={}[]+*-_:;<>'
              };

            RNTesseractOcr.recognize(data.uri.replace('file://', ''), 'LANG_ENGLISH', tessOptions)
                .then((result) => {
                  this.setState({ ocrResult: result });
                  console.log("OCR Result: ", result);
                })
                .catch((err) => {
                  console.log("OCR Error: ", err);
                })
                .done();
        }
    }

or try after you install it on phone to give manually from file explorer the permissions

stale[bot] commented 4 years ago

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.