OPTICAL CHARACTER RECOGNITION FROM THE MICROSOFT COGNITIVE API

Hi Trailblazer Community,

From a long back i was planning to write blog on optical character recognition but i was not getting time for it.

So what is the Meaning of the OCR :-



OCR (optical character recognition) is the recognition of printed or written text characters by a computer. This involves photoscanning of the text character-by-character, analysis of the scanned-in image, and then translation of the character image into character codes.

I have done the OCR Reader work with Google Cloud Vision Api & Microsoft Cognitive Api.

Microsoft Cognitive Api : From Microsoft Cognitive Api i have used the image url for scanning image. We can do OCR from base64 code also.

Google Cloud Vision Api : From Google Cloud Vision Api i have done ocr using the base64 code.


For R&D purpose i have created the account for Microsoft Cognitive Api. After creating account you will get the authentication id and authentication token.

//Apex Class : 


 public with sharing class AdhaarReader{  
   public String Imageurl {get;set;}  
   public String error {get;set;}  
   public String response {get;set;}   
   public boolean flag {get;set;}  
   public String Firstname {get;set;}  
   public String Lastname {get;set;}  
   //public String Fathername {get;set;}  
   public String adharnumber {get;set;}  
   public String dob {get;set;}  
   public String addr{get;set;}  
   public String Gender{get;set;}  
   public AdhaarReader(){  
     flag = false;  
     Imageurl = null;  
   }  
   public void getJson(){  
     Firstname = '';  
     //String Strjson = '{"url":"https://4.imimg.com/data4/QD/OW/MY-28947232/aadhar-pvc-card-500x500.png"}';  
     if(Imageurl != null && Imageurl != ''){  
       String Strjson = '{"url":"'+Imageurl+'"}';  
       String auth_id = 'Your auth id';  
       String auth_token = 'Yout auth token ';  
       HttpRequest req = new HttpRequest();  
       req.setHeader('Content-Type','application/json');  
       req.setHeader('Ocp-Apim-Subscription-Key',auth_id);  
       req.setEndpoint('https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr');  
       req.setMethod('POST');  
       req.setBody(''+Strjson);  
       HttpResponse res = new Http().send(req);  
       if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {  
         flag = true;  
         error = 'Request Send Successfully....!!';  
         response = res.getBody();  
       JSONParser parser = JSON.createParser(res.getBody());  
       String grandTotal ;  
       while(parser.nextToken() != null) {  
         if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&  
           (parser.getText() == 'text')) {  
           parser.nextToken();  
           grandTotal += ' '+parser.getText();  
         }  
       }  
       //system.debug(Pan Url = http://www.charteredclub.com/wp-content/uploads/2013/04/PAN-CARD.jpg);  
       response = grandTotal;   
       String[] ListString = response.split(' ');  
       Firstname = ListString[1];  
       Lastname = ListString[2];  
       Gender = ListString[3];  
       adharnumber = ListString[4]+' '+ListString[5]+' '+ListString[6];  
       try{  
         for(Integer i=13; i<42 ; i++){  
           addr = addr +' '+ ListString[i];  
         }  
       }catch(Exception e){  
         System.debug(e.getMessage()+'');  
       }  
       //addr = ListString[13];  
       }  
       else{  
         error = 'Request Failed....!!';  
         flag = false;  
         response = res.getBody();  
       }  
     }  
     else{  
       error = 'Please Enter the valid Imageurl....!!';  
     }  
   }  
 }  

// VisualForce Page :


 <apex:page controller="AdhaarReader" tabStyle="Account" lightningStylesheets="true">  
   <apex:includeLightning />  
   <apex:form id="frm">  
     <apex:pageBlock title="Microsoft Ocr Reader Api Demo">  
        <apex:outputText value="{!error}" style="color:green;font-size:20px" rendered="{!flag == true}"/>   
        <apex:outputText value="{!error}" style="color:red;font-size:20px" rendered="{!flag == false}"/>   
       <apex:pageBlockSection columns="2" title="Demo Information" collapsible="false">  
         Image Url <apex:inputText value="{!Imageurl}" title="Image Url"/>  
         <apex:commandButton action="{!getJson}" value="Get Output" reRender="frm"/>  
         <apex:outputText value=""> </apex:outputText>  
         First Name <apex:inputText value="{!Firstname}"/>  
         Last Name <apex:inputText value="{!Lastname}"/>  
         Gender <apex:inputText value="{!Gender}"/>  
        <!-- Date Of Birth <apex:inputText value="{!dob}"/>-->  
         Adhaar Number<apex:inputText value="{!adharnumber}"/>  
         Address <apex:inputTextarea value="{!addr}" cols="26"/>  
       </apex:pageBlockSection>      
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>  


How It will look...


My trail account has been expired that's why API is not giving response...but you check from your account..😉 

If you guys get any problem please reach me out @ yogeshwer1994@gmail.com

Thanks & Happy Coading....!!😊


Comments

Popular posts from this blog

PATH IS NOT COMING IN MOBILE??

WHATSAPP INTEGRATION WITH SALESFORCE

UPLOAD FILE FROM JAVASCRIPT REMOTING