Artificial Intelligence and Machine Learning Discussions
Engage in AI and ML discussions. Collaborate on innovative solutions, explore SAP's AI innovations, and discuss use cases, challenges, and future possibilities.
cancel
Showing results for 
Search instead for 
Did you mean: 

Regarding usage of Optical Character Recognition (OCR) API

Saritha_K
Contributor
0 Kudos

Hi Team,

We were testing this new API (Optical Character Recognition (OCR) API) based on OCR technology and have 2 queries based on functionality/usage.

1.) Its "Try It Out" functionality works for .PDF files and correctly list out the contents as response. But it doesnt behave the same way for .jpeg or png files. The overview details state that it should be behaving the same way for .jpg or .pdf files, but it is not happening correctly.

2.) We are trying to test its functionality from the web full stack ide using the ui5 generated code but are facing some api connectivity issues. We tried to check the connection from the destination created but that too doesnt happen successfully. Is there any specific mapping required to be done so that api could be used.

Regards,

SK

6 REPLIES 6

MakotoS
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi SK,

I took screen shot and used the ".png" picture like this text.png and it worked fine.

I guess the accuracy would depend on the noises in the picture and could you try with the simple pictures with black characters and white background first?

About the destination, what kind of error did you face?

R

egards,

Makoto

0 Kudos

Hi,

I have followed steps similar to blog https://www.sap.com/india/developer/tutorials/ml-fs-sapui5-img-classification.html

and in it as per step 4 i.e destination configuration, we can add details of the sandbox url and click on check connection. When I add it in my trial account, I get below message -Failure reason: "Could not check at the moment. Please try again later"

Kindly advise.

regards,

S.K.

dextor
Explorer
0 Kudos

Hi S.K.

you can use this code for getting ocr work
getOCR: function(oEvent) {
			var sHeaders = {
				"Accept": "application/json",
				"APIKey": "Your api key"
			};
			var url  = "";
			var data = new FormData();
                       // this is file object
			data.append('file', this.getView().byId("fileUploader").getFocusDomRef().files[0]);
			data.append('lang', "en");
			data.append('output_type', 'txt');
			this.webRequest(data,sHeaders,this.onOCRDetectedSuccess.bind(this),this.onOCRDetectedFailed);


		},
		onOCRDetectedSuccess: function(data) {
			console.log(data);
		},
		onOCRDetectedFailed: function() {
		
		},
		webRequest:function(data,header,successcallback,failcallback){
			var ajaxRequest = {};
			ajaxRequest.url = /SAP_Leonardo_ML/ocr/ocr;
			ajaxRequest.method = "POST";
			ajaxRequest.headers = header;
			ajaxRequest.data = data;
			ajaxRequest.processData = false;
			ajaxRequest.contentType = false;
			ajaxRequest.cache = false;
			ajaxRequest.success  = successcallback.bind(this);
			ajaxRequest.error = failcallback.bind(this);
			jQuery.ajax(ajaxRequest);
		},


0 Kudos

Hi,

I will try this code and let you know.

thanks for your help.

regards,

SK

Former Member
0 Kudos

Hi there!

I was trying to use this API but it even doesnt work for PDF files. I tried a lot but I dont understand why it isnt working. I always get the http error 400(Bad Request). Maybe somebody of you can help me. That would be great!

That is my controller:

return Controller.extend("OCR.controller.view", {
		
		handleUploadPress: function(oEvent) {
			var oFileUploader = this.getView().byId("fileUploader");
			if (!oFileUploader.getValue()) {
				sap.m.MessageToast.show("Choose a file first");
				return;
			}
			var that = this;
			var f = document.querySelector('input[type="file"]').files[0];
			//Create form object and append file to the same
			var data = new FormData();
			data.append('files', document.getElementById("__xmlview0--fileUploader-fu").files[0], document.getElementById(
				"__xmlview0--fileUploader-fu").files[0].name);
			data.append('lang', "en");
			data.append('output_type', 'txt');
			var xhr = new XMLHttpRequest();


			xhr.addEventListener("readystatechange", function() {
				if (this.readyState === 4) {
					//var vjson = JSON.parse(this.responseText);
					that.getView().byId("JSONText").setText(this.responseText);
				
					//var label = vjson.predictions[0].results[0].label;
					//var score = vjson.predictions[0].results[0].score;
				}
			});


			xhr.open("POST", "https://sandbox.api.sap.com/ml/ocr/ocr");
		//	xhr.open("POST", "https://sandbox.api.sap.com/ml/imageclassifier/inference_sync");
			xhr.setRequestHeader("APIkey", "----------------------------");
			xhr.setRequestHeader("Accept", "application/json");
			xhr.send(data);
			
		}
	});
 

0 Kudos

Hi SK,

I am trying the same OCR concept and I get the same 400 Error. Were you able to solve this??

If so, kindly share the code.

Regards,

Ramya