Hello

I am using the php sample code for testing Abbyy OCR for some days and like the features, the accurracy and the easy integration with PHP.

Now i want use the "processFields" method. i checked the documentation, but i don´t understand how use this method properly, i don´t know where add XML (i have little experience handling XML) and the paremeters or how i can catch the output

I wonder if someone can provide with a code sample of how use this method.

As suggestion , would be good if you can provide a most "ready to use" sample for each method for PHP and any other language. That will help very much a non very experience developers.

Thanks in advance.

Update

i manage found a code in this forum, but now the process never completed No display any error but also not throw any output.

The PHP code

<?php
// 1. Send image to Cloud OCR SDK using submitImage call 
// 2. Get response as xml 
// 3. Read taskId from xml 
// 4. Pass taskId to processFields OCR SDK and execute with XML file for field definitions 
// 5. Read taskID from xml 
// 6. loop until fields are processed 
// 7. download result

// Name of application you created
$applicationId = 'my_application_id'; 
// Password should be sent to your e-mail after application was created 
$password = 'my_password'; 
$fileName = 'my_file.jpg'; 
$xmlName  = 'fields.xml';

// Get path to file that we are going to recognize

$local_directory=dirname(FILE).'';

**// I replace $local_directory=dirname(FILE) for the name of the folder "images"'**
$filePath = 'images'.'/'.$fileName;
if(!file_exists($filePath)) {
   die('File '.$local_directory.$filePath.' not found.');
}

// Get path to file that we are going to use for the field definitions
**// I store the XML fields in the folder "images"**

$local_directory=dirname(FILE).''; 
$xmlPath = 'images'.'/'.$xmlName;
if(!file_exists($xmlPath)) {
  die('File '.$xmlPath.' not found.');
}

// set URL to use submitImage call
$url = 'http://cloud.ocrsdk.com/submitImage';
$url = 'http://cloud.ocrsdk.com/processDocument?taskid=$taskid';

// Send HTTP POST request and ret xml response 
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url); 
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password"); 
curl_setopt($curlHandle, CURLOPT_POST, 1); 
$post_array = array( "my_file"=>"@".$filePath, ); 
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array);
$response = curl_exec($curlHandle); 
if($response == FALSE) {
    $errorText = curl_error($curlHandle);
    curl_close($curlHandle);
    die($errorText);
}
curl_close($curlHandle);

// Parse xml response 
$xml = simplexml_load_string($response);
$arr = $xml->task[0]->attributes();
echo $response."<br/>";

// Task id 
$taskid = $arr["id"];
echo $taskid."<br/>";

// if I print taskID and status field here, I get what looks like a valid taskid
// and status = submitted

$url = 'http://cloud.ocrsdk.com/processFields'; 
$qry_str = "?taskid=$taskid";

// Send HTTP POST request and ret xml response 
$curlHandle = curl_init(); 
curl_setopt($curlHandle, CURLOPT_URL, $url.$qry_str);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password"); 
curl_setopt($curlHandle, CURLOPT_POST, 1);
$post_array = array( "my_file"=>"@".$xmlPath, );
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array);
$response = curl_exec($curlHandle);
if($response == FALSE) {
    $errorText = curl_error($curlHandle);
    curl_close($curlHandle);
    die($errorText);
}
curl_close($curlHandle);

// Parse xml response 
$xml = simplexml_load_string($response);
$arr = $xml->task[0]->attributes();

// Task id 
$taskid = $arr["id"];
echo $taskid."<br/>";

$url = 'http://cloud.ocrsdk.com/getTaskStatus';
$qry_str = "?taskid=$taskid";

// Check task status in a loop until it is finished
// TODO: support states indicating error
do 
{
    sleep(5);
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $url.$qry_str); 
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password");
    $response = curl_exec($curlHandle); 
    curl_close($curlHandle);

   // parse xml
   $xml = simplexml_load_string($response);
   $arr = $xml->task[0]->attributes();
   echo $arr["status"];
} while($arr["status"] != "Completed");

// Result is ready. Download it
$url = $arr["resultUrl"];
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url); 
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); 
// Warning! This is for easier out-of-the box usage of the sample only. 
// The URL to the result has https:
// prefix, so SSL is required to 
// download from it. For whatever reason PHP runtime fails to perform 
// a request unless SSL certificate verification is off. 
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false); 
$response = curl_exec($curlHandle);
curl_close($curlHandle);

// Let user donwload rtf result 
header('Content-type: application/xml');
header('Content-Disposition: attachment; filename="file.xml"'); 
$response;
?>

And the XML

<?xml version="1.0" encoding="utf-8"?>
<document
     xmlns="http://ocrsdk.com/schema/taskDescription-1.0.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://ocrsdk.com/schema/taskDescription-1.0.xsd http://ocrsdk.com/schema/taskDescription-1.0.xsd">
   <fieldTemplates>
       <checkmark id="defaultCheckmark">
          <type>square</type>
       </checkmark>
       <text id="Text">
          <language>spanish</language>
          <region>simpleComb</region>
          <region>combInFrame</region>
          <region>textInFrame</region>
         <textType>handprinted</textType>
      </text>
  </fieldTemplates>
  <page applyTo="0">
  </page>
 </document>
This question is marked "community wiki".

asked 28 Mar '13, 17:30

Fec's gravatar image

Fec
1112

edited 02 Apr '13, 18:55

Dmitry%20Me's gravatar image

Dmitry Me ♦♦
2387

Your program enters infinite loop because you assume the task is guaranteed to enter "Completed" state which it is not. You should expect other terminal states too as done in our updated sample https://github.com/abbyysdk/ocrsdk.com/blob/4e14be1cec6d3bda5854ff203d8c8e436d0404a5/PHP/abbyy_php_example.php - lines 70..113

(02 Apr '13, 19:03) Dmitry Me ♦♦

Hello Fec,

Unfortunately, we have not got a ready PHP sample for the processFields method, but we can recommend the following links:

  1. The PHP sample for the processImage method
  2. The Java sample for the processFields method
  3. The sample xml file for the processFields method and the corresponding image

Hope the information is helpful.

link

answered 01 Apr '13, 19:11

Anastasia%20Galimova's gravatar image

Anastasia Ga... ♦♦
790112

Thanks for the tips Anastasia, i manage found a code in this forum, but now the process never completed No display any error but also not throw any output.

Note:

I update my question with the code and the XML

If you can guide me a little i will thanks.

(01 Apr '13, 20:07) Fec

Hi, I've managed to process some fields but in Bulgarian it's not accurate.

<?php
// 1. Send image to Cloud OCR SDK using processImage call
  // 2. Get response as xml
  // 3. Read taskId from xml
  // Name of application you created
  $applicationId = '';
  // Password should be sent to your e-mail after application was created
  $password = '';
  $fileName = 'fakturi.jpg'; //image name
  $xmlName = "fields.xml"; // xml with options
  // Get path to file that we are going to recognize
  $local_directory=dirname(__FILE__);
  $filePath = $local_directory.'/'.$fileName;
  $xmlPath = $local_directory.'/'.$xmlName;
  if(!file_exists($filePath))
  {
    die('File '.$filePath.' not found.');
  }
  if(!is_readable($filePath) )
  {
     die('Access to file '.$filePath.' denied.');
  }

  // Recognizing with English language to rtf
  // You can use combination of languages like ?language=english,russian or
  // ?language=english,french,dutch
  // For details, see API reference for processImage method
  //$url = 'http://cloud.ocrsdk.com/processImage?language=english&exportFormat=rtf';
  $url = 'http://cloud.ocrsdk.com/submitImage';

  // Send HTTP POST request and ret xml response
  $curlHandle = curl_init();
  curl_setopt($curlHandle, CURLOPT_URL, $url);
  curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password");
  curl_setopt($curlHandle, CURLOPT_POST, 1);
  curl_setopt($curlHandle, CURLOPT_USERAGENT, "PHP Cloud OCR SDK Sample");
  $post_array = array(
      "my_file"=>"@".$filePath,
  );
  curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array); 
  $response = curl_exec($curlHandle);
  if($response == FALSE) {
    $errorText = curl_error($curlHandle);
    curl_close($curlHandle);
    die($errorText);
  }
  $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
  curl_close($curlHandle);

  // Parse xml response
  $xml = simplexml_load_string($response);
  if($httpCode != 200) {
    if(property_exists($xml, "message")) {
       die($xml->message);
    }
    die("unexpected response ".$response);
  }

  $arr = $xml->task[0]->attributes();
  $taskStatus = $arr["status"];

// Task id
  $taskid = $arr["id"];

// if I print taskID and status field here, I get what looks like a valid taskid
  // and status = submitted

$url = 'http://cloud.ocrsdk.com/processFields?taskId='.$taskid;

// Send HTTP POST request and ret xml response
  $curlHandle = curl_init();
  curl_setopt($curlHandle, CURLOPT_URL, $url);
  curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password");
  curl_setopt($curlHandle, CURLOPT_POST, 1);
  $post_array = array(
      "my_file"=>"@".$xmlPath,
  );
  curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array);
  $response = curl_exec($curlHandle);
  if($response == FALSE) {
    $errorText = curl_error($curlHandle);
    curl_close($curlHandle);
    die($errorText);
  }
  curl_close($curlHandle);

// Parse xml response
  $xml = simplexml_load_string($response);
  $arr = $xml->task[0]->attributes();

// Task id
  $taskid = $arr["id"];

$url = 'http://cloud.ocrsdk.com/getTaskStatus';
 $qry_str = "?taskId=$taskid";

// Check task status in a loop until it is finished
  // TODO: support states indicating error
  do
  {
    sleep(5);
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $url.$qry_str);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password");
    $response = curl_exec($curlHandle);
    curl_close($curlHandle);

// parse xml

$xml = simplexml_load_string($response);
$arr = $xml->task[0]->attributes();

}
  while($arr["status"] != "Completed");

// Result is ready. Download it

$url = $arr["resultUrl"];

  $curlHandle = curl_init();
  curl_setopt($curlHandle, CURLOPT_URL, $url);
  curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  // Warning! This is for easier out-of-the box usage of the sample only.
  // The URL to the result has https:// prefix, so SSL is required to
  // download from it. For whatever reason PHP runtime fails to perform
  // a request unless SSL certificate verification is off.
  curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  $response = curl_exec($curlHandle);
  curl_close($curlHandle);

// Let user donwload rtf result
  header('Content-type: text/xml');
  header('Content-Disposition: attachment; filename="millionaires.xml"');
  echo $response;

?>

Example XML 
<document xmlns="http://ocrsdk.com/schema/taskDescription-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/taskDescription-1.0.xsd http://ocrsdk.com/schema/taskDescription-1.0.xsd">
 <fieldTemplates>
        <text id="text" bottom="0" left="0" right="0" top="0">
                <language>Bulgarian</language>
        </text> 
</fieldTemplates>
 <page applyTo="0">
    <text template="text" id="Field!" left="331" top="167" right="300" bottom="190">
        <language>Bulgarian</language>
   </text>
</page>
</document>

Hope it helps you :).

Update: I manged to setup the settings correctly and everything is fine with the field recognition !

link

answered 02 Apr '13, 12:00

krisgosp's gravatar image

krisgosp
112

edited 10 Apr '14, 20:18

Anastasia%20Galimova's gravatar image

Anastasia Ga... ♦♦
790112

Hi krisgosp! Would you mind posting your complete code (formatted as code;) including the XML? I'm fiddling with the processFields method and cannot get it to work. I always receive a "data at the root level is invalid" (error 450) and cannot solve it. (cf. my question here http://forum.ocrsdk.com/questions/1992/php-processfields-returns-error-450-with-msg-data-at-the-root-level-is-invalid-line-1-position-1) Many thanks in advance Axel

(22 Mar '14, 11:44) hfuhruhurr
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×37
×33

Asked: 28 Mar '13, 17:30

Seen: 6,560 times

Last updated: 10 Apr '14, 20:18

© 2016 ABBYY. All rights Reserved. www.ABBYY.com | Privacy Policy | Legal