Azure-Samples / cognitive-services-quickstart-code

Code Examples used by the Quickstarts in the Cognitive Services Documentation
MIT License
356 stars 519 forks source link

OCR not working, run time error #174

Closed mohit1993 closed 3 years ago

mohit1993 commented 3 years ago

getting error: https://myaccount/cognitiveservices.azure.com/vision/v3.1/read/analyze?language=en 400 (Bad Request)

also getting consecutive error as : Uncaught (in promise) Error: The file submitted couldn't be parsed. This can be due to one of the following reasons: the file format is not supported ( Supported formats include JPEG, PNG, BMP, PDF and TIFF), the file is corrupted or password protected. at new RestError (restError.js:7) at deserializationPolicy.js:92

Kindly help... Also it would be great if typescript running code can be found for this integration as well

v-jaswel commented 3 years ago

Hello @mohit1993, I will be happy to look into this for you. If possible, can you post the source code you are running? That will help me to reproduce the issue. Also, can you tell me more about the file you are trying to submit? Is it available anywhere public so I can experiment with it?

mohit1993 commented 3 years ago

/ eslint-disable prefer-template / / eslint-disable guard-for-in / / eslint-disable no-console / / eslint-disable no-await-in-loop / import async from 'async'; import as ComputerVisionClient from '@azure/cognitiveservices-computervision'; import as ApiKeyCredentials from '@azure/ms-rest-js'; import * as createReadStream from 'fs';

/**

const computerVisionClient = new ComputerVisionClient.ComputerVisionClient( new ApiKeyCredentials.ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } }), endpoint );

export function computerVision(): void { async.series( [ async function () { const STATUS_SUCCEEDED = 'succeeded'; // eslint-disable-next-line no-path-concat const handwrittenImageLocalPath = './handwritten_text.jpg';

            console.log('computerVision');

            const handwritingResult = await readTextFromFile(computerVisionClient, handwrittenImageLocalPath);
            printRecText(handwritingResult);

            function sleep(ms: number): Promise<any> {
                // eslint-disable-next-line @typescript-eslint/tslint/config
                return new Promise((resolve) => setTimeout(resolve, ms));
            }

            async function readTextFromFile(client: any, localImagePath: any): Promise<any> {
                let result = await client.readInStream(() => createReadStream.createReadStream(localImagePath));
                const operation = result.operationLocation.split('/').slice(-1)[0];

                while (result.status !== STATUS_SUCCEEDED) {
                    await sleep(1000);
                    result = await client.getReadResult(operation);
                }
                return result.analyzeResult.readResults; // Return the first page of result. Replace [0] with the desired page if this is a multi-page file such as .pdf or .tiff.
            }

            function printRecText(readResults: any[]): void {
                console.log('Recognized text:');
                for (const page in readResults) {
                    if (readResults.length > 1) {
                        console.log(`==== Page: ${page}`);
                    }
                    const result = readResults[page];
                    if (result.lines.length) {
                        for (const line of result.lines) {
                            console.log(line.words.map((w: { text: any }) => w.text).join(' '));
                        }
                    } else {
                        console.log('No recognized text.');
                    }
                }
            }
        },
        function () {
            return new Promise((resolve) => {
                resolve();
            });
        }
    ],
    (err) => {
        throw err;
    }
);

}

computerVision();

Thanks v-jaswel for the cooperation 👍

v-jaswel commented 3 years ago

Hello @mohit1993, thank you for the source code.

I am able to run your code successfully using the following image saved as handwritten_text.jpg: https://i.pinimg.com/originals/97/ab/f8/97abf8d6dbc52cc50fc6f5c3015a18aa.jpg

Can you send me the image you are trying to submit at v-jawe@microsoft.com, or post it to a service such as https://postimages.org/? Then I can pass it on to the Computer Vision product team to analyze.

The product team also tells me they are rolling out a fix that will:

v-jaswel commented 3 years ago

Hello @mohit1993, I am going to close this thread for now, but please re-open it if you are still experiencing the issue. Thank you! v-jaswel